paolodragone / pymzn Goto Github PK
View Code? Open in Web Editor NEWA Python wrapper for the MiniZinc tool pipeline.
License: MIT License
A Python wrapper for the MiniZinc tool pipeline.
License: MIT License
Hi,
I was trying to run pymzn on my windows machine and it got stuck on a Linux command. This is the traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\jver\AppData\Local\Programs\Python\Python35\lib\site-packages\pymzn\_mzn\_minizinc.py", line 161, in minizinc stdlib_dir=stdlib_dir)
File "C:\Users\jver\AppData\Local\Programs\Python\Python35\lib\site-packages\pymzn\_mzn\_minizinc.py", line 295, in mzn2fzn run(args)
File "C:\Users\jver\AppData\Local\Programs\Python\Python35\lib\site-packages\pymzn\bin.py", line 105, in run preexec_fn=os.setsid) as process:
AttributeError: module 'os' has no attribute 'setsid'
I am running my code on python 3.6 and have installed the pymzn though pip install
When I tried to run below code it threw an error.
```
import pymzn
solution = pymzn.minizinc('seq_disjunctive.mzn','Disjunctive.dzn')
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType
I'm new to pymzn. Just running a fairly simple MiniZinc file and I get:
Error: syntax error, unexpected output, expecting end of file
Maybe the solution is too long?
This is the content of the .mzn file:
enum spy = {American, Belgian, Cuban, Dane};
enum place = {Egypt, France, Greece, Haiti};
enum level = {retired, on_run, new, full};
var place: location_American;
var place: location_Belgian;
var place: location_Cuban;
var place: location_Dane;
var level: status_American;
var level: status_Belgian;
var level: status_Cuban;
var level: status_Dane;
constraint location_American != location_Belgian;
constraint location_American != location_Cuban;
constraint location_American != location_Dane;
constraint location_Belgian != location_Cuban;
constraint location_Belgian != location_Dane;
constraint location_Cuban != location_Dane;
constraint status_American != status_Belgian;
constraint status_American != status_Cuban;
constraint status_American != status_Dane;
constraint status_Belgian != status_Cuban;
constraint status_Belgian != status_Dane;
constraint status_Cuban != status_Dane;
% problem constraints
constraint status_Cuban > status_Belgian;
constraint location_Dane = France;
constraint status_American = on_run;
constraint (status_American = retired) = (location_American = Greece);
constraint (status_Belgian = retired) = (location_Belgian = Greece);
constraint (status_Cuban = retired) = (location_Cuban = Greece);
constraint (status_Dane = retired) = (location_Dane = Greece);
constraint (location_American = Haiti) = (status_American = new);
constraint (location_Belgian = Haiti) = (status_Belgian = new);
constraint (location_Cuban = Haiti) = (status_Cuban = new);
constraint (location_Dane = Haiti) = (status_Dane = new);
solve satisfy
Any help would be appreciated.
G'day @paolodragone,
It seems like the latest version (0.18.1
) is incorrectly preprocessing MZN files and treats them like PMZN file.
More specifically, while processing .mzn
files, it is removing all characters following the first %
character it encounters in a line, even if that character is within a string literal.
For instance, the following sample .mzn
file:
% Use a large domain to simulate a long solving process
var 0 .. 100000: x;
solve satisfy;
%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% Result %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%
output [
"\n%%%%%%%%%%%%% RESULTS %%%%%%%%%%%%%\n",
"filled_x = \(x);\n",
"\n%%%%%%%%%%%%%% END %%%%%%%%%%%%%%%\n",
];
gets processed into following:
var 0 .. 100000: x;
solve satisfy;
output [
"\n
"filled_x = \(x);\n",
"\n
];
Note the empty lines above, which are basically lines where the first character was %
. Though what is more important is that the processed file is now syntactically incorrectly (note the string literals in output
statement); which when executed results in error (of course).
The error then thrown by that call to pymzn.minizinc('rel/path/to/sample.mzn')
is:
An error occurred while executing minizinc on file
var 0 .. 100000: x;
solve satisfy;
output [
"\n
"filled_x = \(x);\n",
"\n
];
with command line arguments: ['--model-check-only', '-']
stdin:[line].[col]:
"\n
^
Error: syntax error, unexpected end of line inside string literal
[line]
and [col]
are just placeholders I have put.
This is likely because pymzn.mzn.minizinc._minizinc_preliminaries()
is calling pymzn.mzn.minizinc.preprocess_model()
irrespective of the file extension.
System Setup:
Why the output sequence does not match the original input?
import pymzn
a_string = {'e1', 'e2', 'e3', 'e4'}
pymzn.dict2dzn({'out':a_string}, fout="test.dzn")
##out = {e2, e4, e3, e1};
I am not sure the index of the paramter is valid or not.
Was trying to run pymzn on minizinc 2.5.0. To reproduce, we can use the default example. Looks like the new version of minizinc is adding extra information in the output that pymzn is not able to parse the output properly.
pymzn minizinc knapsack.mzn knapsack.dzn --data '{"capacity": 20}'
Traceback (most recent call last):
File "/usr/local/bin/pymzn", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.8/dist-packages/pymzn/__init__.py", line 222, in main
args['func'](**args)
File "/usr/local/lib/python3.8/dist-packages/pymzn/__init__.py", line 43, in _minizinc
solns = minizinc(
File "/usr/local/lib/python3.8/dist-packages/pymzn/mzn/minizinc.py", line 687, in minizinc
solns = parser.parse(proc)
File "/usr/local/lib/python3.8/dist-packages/pymzn/mzn/output.py", line 245, in parse
self._collect(proc, solns)
File "/usr/local/lib/python3.8/dist-packages/pymzn/mzn/output.py", line 249, in _collect
for soln in self._parse(proc):
File "/usr/local/lib/python3.8/dist-packages/pymzn/mzn/output.py", line 263, in _parse
soln = parse_lines.send(line)
File "/usr/local/lib/python3.8/dist-packages/pymzn/mzn/output.py", line 279, in _parse_lines
soln = dzn2dict(
File "/usr/local/lib/python3.8/dist-packages/pymzn/dzn/parse.py", line 552, in dzn2dict
raise ValueError(
ValueError: Unsupported parsing for statement:
'MiniZinc to FlatZinc converter, version 2.5.0, build 198486215\nCopyright (C) 2014-2020 Monash University, NICTA, Data61\nx = {3,5}'
When a model contains variables whose value are of type enum, pymzn includes the enum in the output with a show
function which fails on enumerated enums (that are not ranges like 1..10
).
Here is a small example coming from the minizinc coursera course (temperature.mzn
):
var int: cost;
array[int] of int: readings = [35,35,20,20,20];
int: start = 25;
int: n = length(readings);
set of int: HOUR = 1..n;
set of int: HOUR0 = 0..n;
array[HOUR] of var ACTION: choice;
enum ACTION = {heat, strong_heat, cool, strongly_cool, do_nothing};
array[HOUR0] of var int: temp;
array[ACTION] of int: adjust = [+1, 4, -2, -5, 0];
array[ACTION] of int: adjust_cost = [1, 5, 3, 9, 0];
constraint temp[0] = start;
constraint forall(h in HOUR) (temp[h] = (temp[h-1] + readings[h]) div 2 + adjust[choice[h]]);
constraint cost = sum(t in HOUR)(adjust_cost[choice[t]]);
constraint forall(h in HOUR) (temp[h] >= 25);
constraint forall(h in HOUR) (temp[h] <= 30);
solve minimize cost;
minizinc temperature.mzn
leads to a solution while the following script fails with INCOMPLETE
:
import pymzn
pymzn.minizinc("temperature.mzn", keep=True)
You can note the show(ACTION)
in the output
of the generated mzn file. When removing that part of the line, the model works fine.
Hi, developers of pymzn.
These is an error occurred when I using pymzn.minizinc with gurobi solver.
s = pymzn.minizinc('placement.mzn', timeout=30, parallel=4, solver=pymzn.gurobi, data=data)
The error is:
`Traceback (most recent call last):
File "D:/Work Space/Pycharm/TensorflowTrainning/vnf_placement_optimization_rl-master/solver.py", line 81, in
env)
File "D:/Work Space/Pycharm/TensorflowTrainning/vnf_placement_optimization_rl-master/solver.py", line 42, in solver
'p_slot': env.p_slot
File "D:\Anaconda3\envs\py36tf180\lib\site-packages\pymzn\mzn\minizinc.py", line 667, in minizinc
**solver_args
File "D:\Anaconda3\envs\py36tf180\lib\site-packages\pymzn\mzn\minizinc.py", line 819, in solve
proc = _run_minizinc_proc(*args, input=input)
File "D:\Anaconda3\envs\py36tf180\lib\site-packages\pymzn\mzn\minizinc.py", line 56, in _run_minizinc_proc
return run_process(*args, input=input)
File "D:\Anaconda3\envs\py36tf180\lib\site-packages\pymzn\mzn\process.py", line 63, in run_process
shell=shell, bufsize=1, universal_newlines=True
File "D:\Anaconda3\envs\py36tf180\lib\subprocess.py", line 423, in run
with Popen(*popenargs, **kwargs) as process:
File "D:\Anaconda3\envs\py36tf180\lib\subprocess.py", line 729, in init
restore_signals, start_new_session)
File "D:\Anaconda3\envs\py36tf180\lib\subprocess.py", line 991, in _execute_child
args = list2cmdline(args)
File "D:\Anaconda3\envs\py36tf180\lib\subprocess.py", line 481, in list2cmdline
needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: argument of type 'int' is not iterable
Process finished with exit code 1
`
But, It works when I remove solver=pymzn.gurobi
Also, It run correctly on minizinc IDE with gurobi solver.
The Minizinc IDE version is 2.5.5
Gurobi version is 9.1.2
pymzn version is 0.18.3
Hope you can help me
Thanks.
Hi,
I think that it would make sense to enable gecode's parallel execution by default (it is set to single-threaded right now). This way users would get the maximum possible performance automatically, without having to mess with the arguments of fzn_gecode().
Thanks!
While trying to call pymzn.minizinc
with a timeout argument, I get a type error:
Traceback (most recent call last):
File "test.py", line 3, in <module>
sol = pymzn.minizinc("cumul_sched.mzn", "cumul_sched.dzn", timeout=5)
File "/usr/local/lib/python3.7/site-packages/pymzn/mzn/minizinc.py", line 649, in minizinc
**solver_args
File "/usr/local/lib/python3.7/site-packages/pymzn/mzn/minizinc.py", line 794, in solve
proc = _run_minizinc_proc(*args, input=input)
File "/usr/local/lib/python3.7/site-packages/pymzn/mzn/minizinc.py", line 56, in _run_minizinc_proc
return run_process(*args, input=input)
File "/usr/local/lib/python3.7/site-packages/pymzn/mzn/process.py", line 63, in run_process
shell=shell, bufsize=1, universal_newlines=True
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 472, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 1453, in _execute_child
restore_signals, start_new_session, preexec_fn)
TypeError: expected str, bytes or os.PathLike object, not int
This is due to this line in mzn/minizinc.py which should be
args += ['--time-limit', str(timeout * 1000)]
I will shortly send a pull-request.
Hey,
thanks for creating this project!
When running with wait=False, I ran into a "NameError: name 'solver_proces' is not defined". The reason are typos in minizinc.py at lines 340 +341 (see
Line 340 in a5de87f
Best,
-Matthias
I think the subject is self explanatory. You see, the Minizinc tutorial uses always "mzn-g12fd" or "mzn-g12mip" and not "fzn-gecode", the problem is that with "fzn-gecode" is taking a long time to solve the problem, I would like to be able to change it.
Hello,
I have been using your package recently (a nice one, btw ๐ ) and just discovered something that looks to like a tiny bug. While passing timeout
to pymzn.minizinc()
function with the default gecode
solver, I get:
Traceback (most recent call last):
File "main.py", line 39, in <module>
main()
File "main.py", line 36, in main
run_test_cases()
File "main.py", line 28, in run_test_cases
schedule = run_solver(**test_case, debug=True)
File "/Users/me/my_project/minizinc_service.py", line 74, in run_solver
timeout=1000)
File "/usr/local/lib/python3.5/site-packages/pymzn/_mzn/_minizinc.py", line 169, in minizinc
all_solutions=all_solutions, **solver_args)
File "/usr/local/lib/python3.5/site-packages/pymzn/_mzn/_solvers.py", line 212, in solve
process = run(args)
File "/usr/local/lib/python3.5/site-packages/pymzn/bin.py", line 105, in run
preexec_fn=os.setsid) as process:
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1490, in _execute_child
restore_signals, start_new_session, preexec_fn)
TypeError: Can't convert 'int' object to str implicitly
As it seemed like there was a typo here, I did a quick fix, and tried it locally. It did the job for me so I'll leave a PR in few minutes in case you find it useful :) Cheers!
Hi,
I am trying to use the cbc
solver through pymzn
. I have two files LP.mzn, Fourier.dzn
. These files are running properly when I use the Minizinc IDE but the error ValueError: Value '1' is not a float.
is raised with pymzn.
Here is the full error stacktrace:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-ab7f17e63e19> in <module>
----> 1 solution=inf555.minizinc('LP.mzn','Fourier.dzn', solver=inf555.cbc, output_node='item')
2
3 solution
~/Documents/lX/2019-2020/s1/INF555-ConDes/TD9/inf555.py in minizinc(mzn, include, *dzn_files, **kwargs)
14 def minizinc(mzn, *dzn_files, include=".", **kwargs):
15 """Solve using minizinc."""
---> 16 return minizn(mzn, *dzn_files, include=include, **kwargs)
17
18
~/opt/anaconda3/envs/dh/lib/python3.7/site-packages/pymzn/mzn/minizinc.py in minizinc(mzn, args, data, include, stdlib_dir, globals_dir, declare_enums, allow_multiple_assignments, keep, output_vars, output_base, output_mode, solver, timeout, two_pass, pre_passes, output_objective, non_unique, all_solutions, num_solutions, free_search, parallel, seed, rebase_arrays, keep_solutions, return_enums, *dzn_files, **kwargs)
685 types=types, keep_solutions=keep_solutions, return_enums=return_enums
686 )
--> 687 solns = parser.parse(proc)
688 return solns
689
~/opt/anaconda3/envs/dh/lib/python3.7/site-packages/pymzn/mzn/output.py in parse(self, proc)
243 logger.info('Started parsing solver output.')
244 solns = Solutions(Queue(), keep=self.keep_solutions)
--> 245 self._collect(proc, solns)
246 return solns
247
~/opt/anaconda3/envs/dh/lib/python3.7/site-packages/pymzn/mzn/output.py in _collect(self, proc, solns)
247
248 def _collect(self, proc, solns):
--> 249 for soln in self._parse(proc):
250 solns._queue.put(soln)
251 logger.info('Solutions parsed: {}'.format(solns._queue.qsize()))
~/opt/anaconda3/envs/dh/lib/python3.7/site-packages/pymzn/mzn/output.py in _parse(self, proc)
261 parse_lines.send(None)
262 for line in proc.readlines():
--> 263 soln = parse_lines.send(line)
264 if soln is not None:
265 yield soln
~/opt/anaconda3/envs/dh/lib/python3.7/site-packages/pymzn/mzn/output.py in _parse_lines(self)
279 soln = dzn2dict(
280 soln, rebase_arrays=self.rebase_arrays,
--> 281 types=self.types, return_enums=self.return_enums
282 )
283 line = yield soln
~/opt/anaconda3/envs/dh/lib/python3.7/site-packages/pymzn/dzn/parse.py in dzn2dict(dzn, rebase_arrays, types, return_enums)
585 var_type = var_types.get(var, None)
586 assign[var] = parse_value(
--> 587 val, var_type=var_type, enums=enums, rebase_arrays=rebase_arrays
588 )
589
~/opt/anaconda3/envs/dh/lib/python3.7/site-packages/pymzn/dzn/parse.py in parse_value(val, var_type, enums, rebase_arrays)
433 if 'dims' in var_type:
434 return _parse_array(
--> 435 val, rebase_arrays=rebase_arrays, var_type=var_type, enums=enums
436 )
437
~/opt/anaconda3/envs/dh/lib/python3.7/site-packages/pymzn/dzn/parse.py in _parse_array(val, rebase_arrays, var_type, enums, raise_errors)
335 p_val = _parse_array_vals(
336 indices, vals, rebase_arrays=rebase_arrays, vals_type=vals_type,
--> 337 enums=enums, raise_errors=raise_errors
338 )
339 return p_val
~/opt/anaconda3/envs/dh/lib/python3.7/site-packages/pymzn/dzn/parse.py in _parse_array_vals(indices, vals, rebase_arrays, vals_type, enums, raise_errors)
360 indices[1:], vals, rebase_arrays=rebase_arrays, vals_type=vals_type,
361 enums=enums, raise_errors=raise_errors
--> 362 ) for i in idx_set}
363
364 if rebase_arrays and list(idx_set)[0] == 1:
~/opt/anaconda3/envs/dh/lib/python3.7/site-packages/pymzn/dzn/parse.py in <dictcomp>(.0)
360 indices[1:], vals, rebase_arrays=rebase_arrays, vals_type=vals_type,
361 enums=enums, raise_errors=raise_errors
--> 362 ) for i in idx_set}
363
364 if rebase_arrays and list(idx_set)[0] == 1:
~/opt/anaconda3/envs/dh/lib/python3.7/site-packages/pymzn/dzn/parse.py in _parse_array_vals(indices, vals, rebase_arrays, vals_type, enums, raise_errors)
355 vals.pop(0), var_type=vals_type, enums=enums,
356 raise_errors=raise_errors
--> 357 ) for i in idx_set}
358 else:
359 arr = {i: _parse_array_vals(
~/opt/anaconda3/envs/dh/lib/python3.7/site-packages/pymzn/dzn/parse.py in <dictcomp>(.0)
355 vals.pop(0), var_type=vals_type, enums=enums,
356 raise_errors=raise_errors
--> 357 ) for i in idx_set}
358 else:
359 arr = {i: _parse_array_vals(
~/opt/anaconda3/envs/dh/lib/python3.7/site-packages/pymzn/dzn/parse.py in _parse_val(val, var_type, enums, raise_errors)
272
273 return _parse_val_basic_type(
--> 274 val, var_type=var_type, enums=enums, raise_errors=raise_errors
275 )
276
~/opt/anaconda3/envs/dh/lib/python3.7/site-packages/pymzn/dzn/parse.py in _parse_val_basic_type(val, var_type, enums, raise_errors)
154
155 if var_type['type'] == 'float':
--> 156 return _parse_float(val, raise_errors=raise_errors)
157
158 if raise_errors:
~/opt/anaconda3/envs/dh/lib/python3.7/site-packages/pymzn/dzn/parse.py in _parse_float(val, raise_errors)
105 return float(val)
106 if raise_errors:
--> 107 raise ValueError('Value \'{}\' is not a float.'.format(val))
108 return None
109
ValueError: Value '1' is not a float.
The function inf555.minizinc
is just a simple wrapper calling pymzn.minizinc
:
def minizinc(mzn, *dzn_files, include=".", **kwargs):
"""Solve using minizinc."""
return minizn(mzn, *dzn_files, include=include, **kwargs)
I call the solver with:
solution=inf555.minizinc('LP.mzn','Fourier.dzn', solver=inf555.cbc)
My LP.mzn
file is:
include "matrix.mzn";
int: m;
int: n;
array[1..1, 1..n] of float: c;
array[1..m, 1..n] of float: A;
array[1..m, 1..1] of float: b;
array[1..n, 1..1] of var float: x;
array[1..m, 1..1] of var float: Ax;
constraint forall(i in 1..n)(x[i,1] >= 0.0);
constraint matrix_mult(A, x, Ax);
constraint matrix_geq(Ax, b);
var float: f = sum(i in 1..n)(c[1,i]*x[i,1]);
solve minimize f;
and finally my Fourier.dzn
file is:
m = 9;
n = 5;
A = [|-1.0, 0.0, 0.0, 0.0, 0.0
|0.0, -1.0, 0.0, 0.0, 0.0
|0.0, 0.0, -1.0, 0.0, 0.0
|1.0, 1.0, 1.0, 0.0, 0.0
|-1.0, -1.0, -1.0, 0.0, 0.0
|0.0, 20.0, 0.0, -2.0, 0.0
|0.0, -20.0, 0.0, 2.0, 0.0
|0.0, 0.0, 20.0, 0.0, -2.0
|0.0, 0.0, -20.0, 0.0, 2.0
|];
b = [|-1.0
|-1.0
|-1.0
|2.0
|-2.0
|0.0
|0.0
|0.0
|0.0
|];
c = [|0, 0, 0, -1, 0|];
Thank you for your help!
Hello Paolo and other pymzn experts,
there is a problem that I hope someone could help me
when I use pymzn.minizinc() this function,the Traceback would tell me FileNotFoundError: [Errno 2] No such file or directory: 'mzn2fzn': 'mzn2fzn'
but in minizinc ide,there is not any problem
thank u!!!!
Hello,
I'm on ubuntu 14, and use python2.7 (yeah I know, we have an old-ish system). I installed pymzn by using "pip install git+https://github.com/paolodragone/pymzn.git@python2", and for minizinc, I cloned the official github repo and ran the instructions in INSTALL.txt. If I run the following code:
import pymzn
import pandas as pd
import os
MINIZINC_PATH = "/home/hristo/libminizinc/build"
TIMEOUT = 300
skills = pd.read_csv('data/skills.csv')
people = skills.name
skills = skills.loc[:, skills.columns != 'name']
demand_forecast = pd.read_csv('data/demand_forecast.csv')
hours_num = 24
days_num = 3
people_num = 4
in_data = {
'days_num': days_num,
'D' : list(range(days_num)),
'hours_num': hours_num,
'H': list(range(hours_num)),
'skills_num': skills.shape[1],
'positions_num': skills.shape[1], # same as skills,
'Pskl': skills.values,
'Ppattern': list(range(people_num)),
'people_pattern_num': people_num,
'Fct_reduced': demand_forecast.values,
'skill_pattern_counts': [15,15,15,15],
'P_salary': list(range(people_num)) + [3,2,3,2]
}
pymzn.dict2dzn(in_data, fout='data/in_data.dzn')
solver = pymzn.Chuffed(os.path.join(MINIZINC_PATH, 'mzn-chuffed'),
os.path.join(MINIZINC_PATH, 'fzn-chuffed'))
res = pymzn.minizinc('simple_model_warehouse_no_crews.mzn',
data=in_data,
solver=solver, timeout=TIMEOUT)
I get the following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-1-13a2d44180ca> in <module>()
38 res = pymzn.minizinc('simple_model_warehouse_no_crews.mzn',
39 data=in_data,
---> 40 solver=solver, timeout=TIMEOUT)
/home/hristo/mlenv/local/lib/python2.7/site-packages/pymzn/mzn/minizinc.pyc in minizinc(mzn, *dzn_files, **solver_args)
230 out = solver.solve(mzn_file, *dzn_files, data=data,
231 include=include, timeout=timeout, output_mode=u'item',
--> 232 all_solutions=all_solutions, **solver_args)
233 solns, complete = split_solns(out)
234 solns = list(imap(dzn2dict, solns))
/home/hristo/mlenv/local/lib/python2.7/site-packages/pymzn/mzn/solvers.pyc in solve(self, mzn_file, *dzn_files, **kwargs)
486 raise RuntimeError(process.stderr)
487 except CalledProcessError, err:
--> 488 log.exception(err.stderr)
489 raise RuntimeError(err.stderr)
490 return out
AttributeError: 'CalledProcessError' object has no attribute 'stderr'
The command line version of minizinc has an option (-s
/ --statistics
) that outputs solver related statistics for every solution found. I didn't find any way to obtain this data when using pymzn.minizinc
. Would it be possible to add an option and return this data with the resulting dictionary?
When attempting to use pyconstruct, the following line failed
pymzn.templates.add_package('pyconstruct.domains', 'share')
with error
AttributeError: module 'pymzn' has no attribute 'templates
It seems
from .templates import *
is missing from this file
https://github.com/paolodragone/pymzn/blob/master/pymzn/__init__.py
It might have gone missing in this commit:
df5aca6#diff-21aa6139d6400b1aa70e461a45c6cf63
Hello Paolo and other pymzn experts,
this is not an issue but more a question on usability of arguments in pymzn.minizinc() function.
As I could not find a forum, I hope this post here is okay.
When I use following code, I get the objective value printed out:
pymzn.minizinc(mzn_file,dzn_file,solver=pymzn.gecode,output_mode='item')
When I use Gurobi solver instead, I only get informed that the solving is "COMPLETE":
pymzn.minizinc(mzn_file,dzn_file,solver=pymzn.gurobi,output_mode='item')
I have tried several things, but do not get the desired output. For example, I failed to hand over arguments:
pymzn.minizinc(mzn_file,dzn_file,args={'arg1':'-s'},solver=pymzn.gurobi)
In fact, I really just want to have the objective value, ideally including the already outputted items of the optimal solution.
Below you can find my mzn and dzn file.
Furthermore, I might want to make use of --relGap argument. If this is not a hassle, could you please present an example usage of pymzn.minizinc() with the use of gurobi and --relGap argument?
Just equivilant to the windows command line, e.g.:
minizinc --solver gurobi -a --statistics knapsack_1.mzn knapsack_9.dzn
I would be so thankful for a hint as I could not figure out a solution since last thursday.
Info: MiniZinc Version 2.4.3., pymzn Version 18.3, Visual Studio Code Version 1.49.2, Python 3.8.5 x64, Windows 10.0.18363 x64,
Best regards
Dominic
mzn file:
`enum ITEM;
int: wcapacity;
int: vcapacity;
array[ITEM] of int: profits;
array[ITEM] of int: weights;
array[ITEM] of int: volumes;
var set of ITEM: knapsack;
var int: weight = sum (i in knapsack) (weights[i]);
constraint weight <= wcapacity;
var int: volume = sum (i in knapsack) (volumes[i]);
constraint volume <= vcapacity;
var int: profit = sum (i in knapsack) (profits[i]);
solve maximize profit ;
%regardless of using below output line or not
output [show(profit)];`
dzn file:
`wcapacity = 3500;
vcapacity = 20;
ITEM = { AAA, AAB, AAC, AAD, AAE};
profits = [195,150,63,183,20];
weights = [982,124,1144,1507,1009];
volumes = [5,1,8,8,6];`
Hi,
when I try running any model, for instance the 'knapsack' model, I get the error " 'NoneType' object has no attribute 'endswith' " which appears for the mzn_file variable in the args function being called from the _solve function.
I could backtrack the problem to the mzn2fzn function where the process is run but during that no fzn or ozn file is generated. This leads to the fzn_file and ozn_file variables being assigned the None object which then is handed over to the _solve function and causing the problem.
Has anyone ever had the same problem or knows how to deal with it?
Best regards,
Julian
When generating a MiniZinc model with output variables, pymzn adds output annotations, such as output_array([1..52])
. When using Chuffed as a solver, this leads to an error FlatZinc::AST::TypeError
.
Using the attached files, running chuffed fails, while mzn-gecode works:
$ mzn-chuffed pymzn_lo34fh_5.mzn pymzn_lo34fh_5.dzn
terminate called after throwing an instance of 'FlatZinc::AST::TypeError'
The error is issued by the annotations in lines 5-6 in the mzn file:
array[1..52] of var 1..52: x :: output_array([1..52]);
array[1..52] of var 1..52: y :: output_array([1..52]);
In pymzn, they are added in _redefine_output_vars
in model.py.
pymzn was invoked via:
pymzn.minizinc('black-hole.mzn' , data={'layout': [[10, 11, 12], [30, 7, 4], [20, 36, 22], [37, 18, 9], [33, 19, 29], [26, 2, 51], [31, 44, 25], [50, 52, 43], [38, 48, 41], [27, 49, 42], [34, 3, 46], [5, 47, 24], [28, 21, 16], [45, 14, 15], [32, 23, 40], [39, 6, 35], [8, 17, 13]]}, all_solutions=False, solver=pymzn.chuffed, output_vars=['x', 'y'], output_mode='dict', force_flatten=True, keep=False)
force_flatten=True
is not relevant for the problem, but a workaround for this issue.
I'm not entirely sure in which cases the output annotations might be necessary. For now, I've removed the call to this method locally, but that's only a workaround.
pymzn_lo34fh_5.dzn.txt
pymzn_lo34fh_5.mzn.txt
black-hole.mzn.txt
In minizinc.py, lien 294, there is:
args =+ ['-D', data]
I guess it should be
args += ['-D', data]
Hi,
I am using Anaconda Python 2.7.12 in Windows 8.1. I installed pymzn with pip as instructed. When I tried to import the library, it throws me this error:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
import pymzn
File "C:\Users\Indrajit\Anaconda2\lib\site-packages\pymzn\__init__.py", line 114
args.func(**{**vars(args), **args.solver_args})
^
SyntaxError: invalid syntax
Please look into it.
Regards,
Indrajit
The subject says it all :)
I want to pass to the compiler the option '-O5' to set a higher optimization level. I can't find a way to do it using pymzn. Am I missing something?
I've run in a fairly similar bug like #35 - this time for solver argument parallel
. Might probably also be an issue for seed
and num_solutions
as they are not converted to a string.
Solved it for the time being by setting
if num_solutions is not None:
args += ['-n', str(num_solutions)]
if free_search:
args.append('-f')
if parallel is not None:
args += ['-p', str(parallel)]
if seed is not None:
args += ['-r', str(seed)]
in the Solver
class.
If I got time this weekend I could try to propose a PR.
When trying the async mode on windows 10 i get the following error
stderr=stderr, **kwds)
File "C:\Users\tamirg\Anaconda3\lib\asyncio\base_events.py", line 1540, in subprocess_exec
bufsize, **kwargs)
File "C:\Users\tamirg\Anaconda3\lib\asyncio\base_events.py", line 462, in _make_subprocess_transport
raise NotImplementedError
NotImplementedError
my understanding is the windows 10 is using another event loop called
ProactorEventLoop
Hey again,
In my mzn I have a type definition like this:
enum JOB;
array[JOB] of int: job_durations;
Now when I feed my data to minizinc(data={"job_durations": [1,2,3], ...)
then it will result in a data file like this:
task_durations = array1d(1..3, [1, 2, 3]);
This results in an error in MiniZinc:
MiniZinc: type error: initialisation value for 'job_durations' has invalid type-inst: expected 'array[JOB] of int', actual 'array[int] of int'
Looking at the code, especially _dzn_array_nd
and _index_set
in marsh.py
it seems that this use case is not supported.
Is there a way to make this work?
When I do things manually I do it like this:
job_durations = [ 1, 2, 3 ];
job_flags =
[| false, false, false,
| false, false, false,
| false, false, false
|];
for 1- and 2d-arrays respectively. That is, I just omit the dimensions and arrayNd stuff.
Hi,
I tried to launch a simple .py file like the example proposed, but i got this error on macOS High Sierra:
Traceback (most recent call last):
File "/Users/Username/Desktop/Test.py", line 3, in <module>
pymzn.minizinc("test.mzn", "test.dzn")
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pymzn/mzn/minizinc.py", line 202, in minizinc output_mode=_output_mode)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pymzn/mzn/minizinc.py", line 308, in mzn2fzn run(args)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-
packages/pymzn/mzn/utils.py", line 50, in run stderr=subprocess.PIPE) as process:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py",
line 676, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py",
line 1289, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: "mzn2fzn"
and this one on Windows 10
Traceback (most recent call last):
File "C:/Users/Username/Desktop/pymzn/Test.py", line 3, in <module>
pymzn.minizinc('test.mzn', 'test.dzn')
File "C:\Program Files\Python35\lib\site-packages\pymzn\mzn\minizinc.py", line 202, in minizinc
output_mode=_output_mode)
File "C:\Program Files\Python35\lib\site-packages\pymzn\mzn\minizinc.py", line 308, in mzn2fzn
run(args)
File "C:\Program Files\Python35\lib\site-packages\pymzn\utils.py", line 50, in run
stderr=subprocess.PIPE) as process:
File "C:\Program Files\Python35\lib\subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "C:\Program Files\Python35\lib\subprocess.py", line 1224, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] Impossibile trovare il file specificato
I've already installed MiniZinc. How can I solve this?
Hi,
The named temporary files that are created never seem to get deleted (delete=False). This causes the files to pile up in my /tmp dir. I've found the _cleanup
function, but it doesn't seem to get invoked. One reason could be exception raising and the stream never being iterated at all. I think there should be some kind of context manager to prevent this. I'm also not sure how this should work with the wait=False mode.
Hi,
When the model or the data has a problem, pymzn.minizinc() returns INCOMPLETE
instead a ERROR
. Let's see in the terminal:
$ cat model.mzn
set of 0..10: values;
array[values] of var 0..1: x;
solve minimize sum(x);
values = {1, 3, 5};
$ minizinc --solver COIN-BC model.mzn
MiniZinc: evaluation error:
...../model.mzn:2:
in variable declaration for 'x'
array index set must be contiguous range
Which is OK calling minizinc directly. Now, let's see using pymzn:
In [1]: import pymzn
In [2]: result = pymzn.minizinc(mzn="model.mzn", solver=pymzn.cbc)
In [3]: result
Out[3]: <Solutions: INCOMPLETE>
In [4]: result.status == pymzn.Status.ERROR
Out[4]: False
The documentation is a little bit vague, but it says that status INCOMPLETE
represents an incomplete solution stream. I don't know what this means precisely. Assuming that we have status COMPLETE
(that assumes that an optimum solution is found) and an ERROR
saying that a "Generic error in the execution of the solver," I would tell that incomplete means that the search is incomplete, i.e., we have no errors (since erros are supposed to be reported by ERROR
flag), but only a situation where we could not complete the search (maybe do not find a feasible solution).
However, note that models errors should be treated as ERROR
since we have an actual error, not an incomplete search, or special condition in the search.
In pymzn 0.18.3, Python sets are correctly translated to the dzn format, even when empty, except when they are empty and in a list. For example:
pymzn.dict2dzn({'non_empty_set': set([1,2,3,8]),
'empty_set': set(),
'list_with_empty_set': [set()],
'list_with_mixed_sets': [set([1,3]), set(), set([4,5])]})
returns
['non_empty_set = {8, 1, 2, 3};',
'empty_set = {};',
'list_with_empty_set = array1d(1..1, [set()]);',
'list_with_mixed_sets = array1d(1..3, [{1, 3}, set(), {4, 5}]);']
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.