我已经建立了一个pyomo模型,并通过以下代码编写了该模型的lp文件:
# write LP file
filename = os.path.join(os.path.dirname(__file__), 'model.lp')
model.write(filename, io_options={'symbolic_solver_labels': True})
我在文件夹中得到model.lp
文件。看起来像这样:
\* Source Pyomo model name=urbs *\
min
obj:
+1 costs(Environmental)
+1 costs(Fixed)
+1 costs(Fuel)
+1 costs(Invest)
+1 costs(Variable)
s.t.
c_e_res_vertex(1_Mid_Biomass_Stock)_:
+1 e_co_stock(1_Mid_Biomass_Stock)
-1 e_pro_in(1_Mid_Biomass_plant_Biomass)
= 0
c_e_res_vertex(1_Mid_Coal_Stock)_:
+1 e_co_stock(1_Mid_Coal_Stock)
-1 e_pro_in(1_Mid_Coal_plant_Coal)
= 0
我的问题是,我还想保存模型的变量值。
是否有一种方法可以强制求解器将模型变量的值写入lp文件?
还是其他东西,以不同的方式做相同的事情?
答案 0 :(得分:0)
我想到两种方法。
使用您的LP文件进行搜索并替换。例如,具有索引x
和2
(在Pyomo:'productA'
中的索引model.x[2,'productA']
的变量x(2_productA)
被写为LP文件lpFileContent
。知道这一点后,对于每个变量及其每个索引,以LP格式生成其名称,并在LP文件中搜索所有出现的这些变量,以它们的值替换它们。
如果for v in model.component_objects(Var, active=True):
varName = str(v)
varObject = getattr(model, varName)
for index in varObject:
indexStr = str(index)
# Convert your index string:
indexStr.replace(",","_")
indexStr.replace(" ","_")
indexStr.replace("'","") # Add more as you need.
#Replace by value:
lpFileContent.replace(varName + "(" + indexStr + ")", varObject[index].value)
with open("output.txt", "w") as outputFile
outputFile.write(lpFileContent)
是您的LP文件中包含的字符串,则它将类似于:
model.A = RangeSet(1,10)
model.a = Param(model.A, within=PositiveReals)
model.ToBuy = Var(model.A)
def bud_rule(model, i):
return model.a[i]*model.ToBuy[i] <= i
aBudget = Constraint(model.A, rule=bud_rule)
在定义约束时,通常以这种方式来实现(来自Pyomo doc):
model.A = RangeSet(1,10)
model.a = Param(model.A, within=PositiveReals)
model.ToBuy = Var(model.A)
def bud_rule(model, i):
print(str(model.a[i]*model.ToBuy[i]) + " <= " + str(i))
return model.a[i]*model.ToBuy[i] <= i
aBudget = Constraint(model.A, rule=bud_rule)
然后,总是可以通过执行以下小技巧来检索此约束的表达式:
1 * model.ToBuy[1] <= 1
2 * model.ToBuy[2] <= 2
3 * model.ToBuy[3] <= 3
4 * model.ToBuy[4] <= 4
... #It goes on
10 * model.ToBuy[10] <= 10
将会产生类似
import tkinter as tk
from tkinter.filedialog
import askopenfilename
import shutil
import os
from PIL import Image, ImageTk
window = tk.Tk()
window.title(" ")
window.geometry("500x510")
window.configure(background ="lightgreen")
title = tk.Label(text="Click below to choose picture for testing disease....", background = "lightgreen", fg="Brown", font=("", 15))
title.grid()
def feature():
window.destroy()
window1 = tk.Tk()
window1.title(" ")
window1.geometry("650x510")
window1.configure(background="lightgreen")
def exit():
window1.destroy()
#i want to print some features of image e.g. Mean, variance,s.d. Etc.
button = tk.Button(text="Exit", command=exit)
button.grid(column=0, row=9, padx=20, pady=20)
window1.mainloop()
def openphoto():
import cv2
import numpy as np
dirPath = " "
fileList = os.listdir(dirPath)
for fileName in fileList:
os.remove(dirPath + "/" + fileName)
fileName = askopenfilename(initialdir='', title='Select image for analysis ',
filetypes=[('image files', '.jpg')])
dst = " "
shutil.copy(fileName, dst)
#this is the image
Photo = Image.open(fileName)
render = ImageTk.PhotoImage(photo)
img = tk.Label(image=render, height="250", width="500")
img.image = render
img.place(x=0, y=0)
img.grid(column=0, row=1, padx=10, pady = 10)
title.destroy()
button1.destroy()
button2 = tk.Button(text="Analyse Image", command=feature)
button2.grid(column=0, row=2, padx=10, pady = 10)
button1 = tk.Button(text="Get Photo", command = openphoto)
button1.grid(column=0, row=1, padx=10, pady = 10)
window.mainloop()
我相信这也是您可以使用的方法(通过搜索和替换,或者在解决之后再建立约束时打印变量值)。它赋予了自定义输出更多的功能,易于调试,但是如果表达式很长(例如对数千个元素求和的话)将非常慢。