如何解决Python中有关Minizing包的此错误?

2024-06-16 08:47:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

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;

我的代码有什么问题


Tags: oftheinstanceinanforoutputdata