我有以下代码:
from minizinc import Instance, Model, Solver
MODEL = "model1.mzn"
DATA = "ins-1.dzn"
# Loading the model
model = Model(MODEL)
model.add_file(DATA)
# Find the MiniZinc solver configuration for Gecode
gecode = Solver.lookup("gecode")
# Create an Instance of the model for Gecode
instance = Instance(gecode,model)
# solution and creation of an output file
result = instance.solve()
# loading solution in an ouptput file
with open(DATA[:-4] + "_output.txt","w") as file:
file.write(instance["w"] + " " + result["l"] + "\n")
num_circuits = instance["n"]
file.write(num_circuits + "\n")
for i in num_circuits:
file.write(instance["x"][i] + " " + instance["y"][i] + " ")
file.write(result["p"][i][0] + " " + result["p"][i][1] + "\n")
执行后,我出现以下错误:
Traceback (most recent call last):
File "C:/Users/boezi/PycharmProjects/pythonProject/output_solution.py", line 23, in <module>
file.write(instance["w"] + " " + result["l"] + "\n")
File "C:\Users\boezi\AppData\Local\Programs\Python\Python37\lib\site-packages\minizinc\model.py", line 145, in __getitem__
return self._data.__getitem__(key)
KeyError: 'w'
似乎关键字“w”不存在,但我已在我的mzn文件中定义了它:
% width of the plate
int: w;
我的代码有什么问题
我对这个图书馆不熟悉,但我猜能帮你吗
https://minizinc-python.readthedocs.io/en/latest/getting_started.html
看起来你需要给“w”赋值
前
instance["w"] = 4
相关问题 更多 >
编程相关推荐