前文:
编程实现线性规划——单纯形法(上)
def Iteration2(self): #迭代函数
lim=100 #最大迭代次数(防止无解时无限迭代)
while(lim>0):
self.check=[]
for i in range(self.X_num): #计算检验数
temp=0
for j in range(self.B_num):
temp+=self.bound[j][i]*self.z0[self.Xbase[j]]
self.check.append(self.z0[i]-temp)
self.IsEnd() #判断迭代是否结束
if self.flag>0: #有解,则结束迭代
break
else: #否则,进行基变换
self.BaseChange(2)
lim-=1
def IsEnd2(self):
self.flag=1
for i in range(self.B_num):
if self.bound[i][self.X_num]<0: #若有约束条件右端常数为负,继续迭代
self.flag=0
break
def FindMain2(self): #寻找主元素
iB=[]
for i in range(self.B_num):
iB.append(self.bound[i][self.X_num])
iout=iB.index(min(iB)) #获取b中最小值序号
theta=[]
for j in range(self.X_num):
if self.bound[iout][j]>=0:
th=1000
elif self.check[j]==0:
th=1000
else:
th=self.check[j]/self.bound[iout][j]
theta.append(th)
iin=theta.index(min(theta)) #获取检验数中最小值序号
main=self.bound[iout][iin]
return [iout,iin,main]
# -*- coding: utf-8 -*-
import LPTable
from tkinter import *
from tkinter import ttk
def Start():
model=comboxlis.get() #获取单纯形法类型
if model=="单纯形法":
model1()
elif model=="对偶单纯形法":
model2()
else:
text31.insert("end","请选择一种计算模式")
def model2():
Table=ReadData2()
Table.Iteration2() #进入迭代
if Table.flag>0: #判断是否通过迭代求出了最优解
z=0 #最优值
Best=[] #最优解
for j in range(Table.X_num-Table.B_num):
Best.append(0)
for i in range(Table.B_num):
X=Table.Xbase[i]
c=Table.z0[X]
num=Table.bound[i][Table.X_num]
Best[Table.Xbase[i]]=num #记录最优解
z+=num*c #整理最优值
text41.insert("end",Best)
z=-z #最小值转换
text51.insert("end",str(z))
if Table.flag==1:
text52.insert("end","<唯一最优解>")
else:
text52.insert("end","<无穷多最优解>")
else:
text52.insert("end","<无最优解>")
def model1():
[Table,Mtype]=ReadData() #获得数据
Table.Iteration() #进入迭代
if Table.flag>0: #判断是否通过迭代求出了最优解
z=0 #最优值
Best=[] #最优解
for j in range(Table.X_num-Table.B_num):
Best.append(0)
for i in range(Table.B_num):
X=Table.Xbase[i]
c=Table.z0[X]
num=Table.bound[i][Table.X_num]
Best[Table.Xbase[i]]=num #记录最优解
z+=num*c #整理最优值
text41.insert("end",Best)
if Mtype=="min": #最小值转换
z=-z
text51.insert("end",str(z))
if Table.flag==1:
text52.insert("end","<唯一最优解>")
else:
text52.insert("end","<无穷多最优解>")
else:
text52.insert("end","<无最优解>")
def StrChange(sstr): #字符串转化为列表储存
lst=sstr.split(' ')
new=[]
length=len(lst)
for i in range(length):
new.append(float(lst[i]))
return [new,length]
def ReadData2(): #读取数据
text41.delete("1.0","end")
text51.delete("1.0","end")
text52.delete("1.0","end")
[z0,X_num]=StrChange(entry21.get())
for i in range(X_num): #变换目标函数
z0[i]=-z0[i]
IB=text31.get("1.0","end").split('\n')
B_num=len(IB)-1
bound=[] #约束条件
for i in range(B_num):
bound.append(StrChange(IB[i])[0])
for j in range(X_num+1):
bound[i][j]=-bound[i][j]
Xbase=[]
for i in range(B_num): #添加人工变量
Xbase.append(X_num+i)
z0.append(0)
b=bound[i].pop(-1)
for k in range(B_num):
if k==i:
bound[i].append(1.0)
else:
bound[i].append(0.0)
bound[i].append(b)
X_num+=B_num
Itable=LPTable.Table(X_num,B_num,z0,Xbase,bound)
return Itable
def ReadData(): #读取数据
text41.delete("1.0","end")
text51.delete("1.0","end")
text52.delete("1.0","end")
[z0,X_num]=StrChange(entry21.get())
Mtype=comboxlist.get()
if Mtype=="min": #最小值计算
for j in range(X_num):
z0[j]=-z0[j]
IB=text31.get("1.0","end").split('\n')
B_num=len(IB)-1
bound=[] #约束条件
for i in range(B_num):
bound.append(StrChange(IB[i])[0])
Xbase=[]
for i in range(B_num): #添加人工变量
Xbase.append(X_num+i)
z0.append(-1000)
b=bound[i].pop(-1)
for k in range(B_num):
if k==i:
bound[i].append(1.0)
else:
bound[i].append(0.0)
bound[i].append(b)
X_num+=B_num
Itable=LPTable.Table(X_num,B_num,z0,Xbase,bound)
return [Itable,Mtype]
if __name__ == "__main__":
root = Tk()
root.geometry('300x320')
root.title("线性规划实验1")
between='- - - - - - - - - - - - - - - - - - - - - - - - - - -'
Btn1 = Button(root, text="开始计算", width=6,command=Start)
Btn1.grid(row=0, column=0)
comvalue=StringVar()#窗体自带的文本,新建一个值
comboxlis=ttk.Combobox(root,textvariable=comvalue,width=15) #初始化
comboxlis["values"]=("单纯形法","对偶单纯形法")
comboxlis.current(0) #选择第一个
comboxlis.grid(row=0, column=1,columnspan=2)
label21 = Label(root, text="目标函数:")
label21.grid(row=1, column=0)
comvalue=StringVar()#窗体自带的文本,新建一个值
comboxlist=ttk.Combobox(root,textvariable=comvalue,width=4) #初始化
comboxlist["values"]=("max","min")
comboxlist.current(0) #选择第一个
comboxlist.grid(row=1, column=1)
entry21 = Entry(root,width=14)
entry21.grid(row=1, column=2,columnspan=2)
label31 = Label(root, text="约束条件:")
label31.grid(row=2, column=0)
text31=Text(root,width=28,height=8)
text31.grid(row=2, column=1,columnspan=3)
be1=Label(root,text=between)
be1.grid(row=3,column=0,columnspan=4)
label41 = Label(root, text="最优解:")
label41.grid(row=4, column=0)
text41 = Text(root,width=31,height=1)
text41.grid(row=4, column=1,columnspan=3)
label51 = Label(root, text="最优值:")
label51.grid(row=5, column=0)
text51 = Text(root,width=10,height=1)
text51.grid(row=5, column=1)
text52 = Text(root,width=13,height=1)
text52.grid(row=5, column=2)
root.mainloop()