使用Python调用MiniZinc+OR-Tools求解器时JSON解析报错
MiniZinc Python调用OR-Tools求解器时JSON解码错误问题
我通过MiniZinc Python库在Python中调用MiniZinc,代码如下:
model = Model() _solver = Solver.lookup(solver) inst = Instance(_solver, model) if len(model_file) > 0: for f in model_file: inst.add_file(f) inst.add_string("\n") if len(model_text) > 0: text = "" for t in model_text: text += t + "\n" inst.add_string(text) result = inst.solve(timeout=timedelta(seconds=timeout))
使用Gecode求解器时运行正常,但切换为OR-Tools求解器后,执行inst.solve(...)时抛出错误:
File "/Users/gaby/Github/simulations/.venv/lib/python3.12/site-packages/minizinc/instance.py", line 235, in solve_async async for result in self.solutions( File "/Users/gaby/Github/simulations/.venv/lib/python3.12/site-packages/minizinc/instance.py", line 373, in solutions async for obj in decode_async_json_stream( File "/Users/gaby/Github/simulations/.venv/lib/python3.12/site-packages/minizinc/json.py", line 103, in decode_async_json_stream obj = loads(buffer, cls=cls, **kw) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/json/__init__.py", line 359, in loads return cls(**kw).decode(s) ^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
已尝试以下操作:
- 使用MiniZinc IDE搭配OR-Tools求解器运行同一模型,可正常运行,说明配置无问题。
- 在Python中使用Gecode求解器调用
instance.solve(),可正常求解。 - 确认Python能找到OR-Tools求解器(
Solver.lookup(solver)可成功查找)。
期望能在Python中使用OR-Tools作为求解器运行MiniZinc(尤其需要在pytest中使用),求解决方案。
内容的提问来源于stack exchange,提问作者GabyLP
相关产品推荐
相关产品推荐

