MiniZinc/minizinc-python

MiniZincSyntaxError on add_string() method

sk-surya opened this issue · 2 comments

Tried running this example.

from minizinc import Instance, Model, Result, Solver, Status

gecode = Solver.lookup("gecode")
m = Model()
m.add_string(
    """
    array[1..4] of var 1..10: x;
    var int: obj;

    constraint obj = sum(x);
    output ["\\(obj)"]
    """
)
inst = Instance(gecode, m)

res: Result = inst.solve()
print(res.solution)
while res.status == Status.SATISFIED:
    with inst.branch() as child:
        child.add_string(f"constraint obj > {res['obj']}")
        res = child.solve()
        if res.solution is not None:
            print(res.solution)

Getting the following error:

MiniZincSyntaxError                       Traceback (most recent call last)
<ipython-input-1-3d132e8ee7f9> in <module>
     19     with inst.branch() as child:
     20         child.add_string(f"constraint obj > {res['obj']}")
---> 21         res = child.solve()
     22         if res.solution is not None:
     23             print(res.solution)

F:\MyPrograms\anaconda3\lib\site-packages\minizinc\instance.py in solve(self, timeout, nr_solutions, processes, random_seed, all_solutions, intermediate_solutions, free_search, optimisation_level, **kwargs)
    115             if sys.platform == "win32":
    116                 asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
--> 117             return asyncio.run(coroutine)
    118         else:
    119             if sys.platform == "win32":

F:\MyPrograms\anaconda3\lib\asyncio\runners.py in run(main, debug)
     41         events.set_event_loop(loop)
     42         loop.set_debug(debug)
---> 43         return loop.run_until_complete(main)
     44     finally:
     45         try:

F:\MyPrograms\anaconda3\lib\asyncio\base_events.py in run_until_complete(self, future)
    581             raise RuntimeError('Event loop stopped before Future completed.')
    582 
--> 583         return future.result()
    584 
    585     def stop(self):

F:\MyPrograms\anaconda3\lib\site-packages\minizinc\CLI\instance.py in solve_async(self, timeout, nr_solutions, processes, random_seed, all_solutions, intermediate_solutions, free_search, optimisation_level, **kwargs)
    423             free_search=free_search,
    424             optimisation_level=optimisation_level,
--> 425             **kwargs,
    426         ):
    427             status = result.status

F:\MyPrograms\anaconda3\lib\site-packages\minizinc\CLI\instance.py in solutions(self, timeout, nr_solutions, processes, random_seed, all_solutions, intermediate_solutions, free_search, optimisation_level, verbose, debug_output, **kwargs)
    297                 raise NotImplementedError("Solver does not support the -n flag")
    298             cmd.extend(["-n", str(nr_solutions)])
--> 299         if "-a" in self._solver.stdFlags and self.method != Method.SATISFY:
    300             cmd.append("-a")
    301         # Set number of processes to be used

F:\MyPrograms\anaconda3\lib\site-packages\minizinc\CLI\instance.py in method(self)
     99     def method(self) -> Method:
    100         if self._method is None:
--> 101             self.analyse()
    102         assert self._method is not None
    103         return self._method

F:\MyPrograms\anaconda3\lib\site-packages\minizinc\CLI\instance.py in analyse(self)
    182         with self.files() as files:
    183             assert len(files) > 0
--> 184             output = self._driver.run(["--model-interface-only"] + files, self._solver)
    185         interface = json.loads(
    186             output.stdout

F:\MyPrograms\anaconda3\lib\site-packages\minizinc\CLI\driver.py in run(self, args, solver, timeout)
    137                 )
    138         if output.returncode != 0:
--> 139             raise parse_error(output.stderr)
    140         return output
    141 

MiniZincSyntaxError: C:\Users\surya\AppData\Local\Temp\mzn_fragment2awvqo2s.mzn:2.5-9:
    array[1..4] of var 1..10: x;
    ^^^^^
Error: syntax error, unexpected array, expecting end of file
File fragment:
1: constraint obj > 4
2:     array[1..4] of var 1..10: x;
       ^^^^^
3:     var int: obj;

Thank you for posting the issue. It seems that a semi-colon is missing in the example. It should be:

from minizinc import Instance, Model, Result, Solver, Status

gecode = Solver.lookup("gecode")
m = Model()
m.add_string(
    """
    array[1..4] of var 1..10: x;
    var int: obj;

    constraint obj = sum(x);
    output ["\\(obj)"];
    """
)
inst = Instance(gecode, m)

res: Result = inst.solve()
print(res.solution)
while res.status == Status.SATISFIED:
    with inst.branch() as child:
        child.add_string(f"constraint obj > {res['obj']};")
        res = child.solve()
        if res.solution is not None:
            print(res.solution)

This issue is resolved in e27120f