Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: output of objective value using gurobi solver with pymzn.minizinc() function #47

Open
DomHaw opened this issue Oct 5, 2020 · 3 comments

Comments

@DomHaw
Copy link

DomHaw commented Oct 5, 2020

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];`

@paolodragone
Copy link
Owner

Hello Dominic,

Regarding your first question, the function pymzn.minizinc should return a Solutions object regardless of the solver (see here). This object behaves like a list of solutions, so the optimal solution should be the last one:

s = pymzn.minizinc(...)
opt = s[-1]

Regarding the --relGap parameter, unfortunately it's not supported directly, but an alternative that you have is to define your own solver class by subclassing the Gurobi solver class and adding the parameter to the list of returned by args function. Then you can pass an instance of this class to the minizinc function via the solver argument.

Let me know if this solves the issue or if you have further questions.

Cheers,
Paolo

@DomHaw
Copy link
Author

DomHaw commented Oct 10, 2020

Hi Paolo,

thanks a lot for your very fast reply. Unfortunately, opt = s[-1] delivers the optimal solution, in my case the optimal set of items for the knapsack problem. But it does not give me the objective value I am optimizing for, here: to maximize profit

The --relGap was not needed yet, because speed performance is satisfying good with gurobi solver. As I could not figure out above mentioned issue, I instead used a command workaround.

cmd_str = 'minizinc --solver gurobi -s ' + '"' + mzn_file + '" "' + dzn_file + '"'
cmd = subprocess.check_output(cmd_str, shell=True)

This obviously does not make use of your nice library. :-(
For me, this actually performed even faster, despite the fact I have to parse the result string into desired variables.

I keep you and others posted, if I figure out an more elegant way. For now: Thanks again
Best regards
Dominic

@paolodragone
Copy link
Owner

Unfortunately, opt = s[-1] delivers the optimal solution, in my case the optimal set of items for the knapsack problem. But it does not give me the objective value I am optimizing for, here: to maximize profit

For that you can try using the output_vars argument, like:

s = pymzn.minizinc(mzn_file, dzn_file, solver=pymzn.gurobi, output_vars=['knapsack', 'profit'])
opt = s[-1]

As I could not figure out above mentioned issue, I instead used a command workaround.

I meant something like this:

class GurobiWithArgs(pymzn.solvers.Gurobi):
    def __init__(self, rel_gap):
        self.rel_gap = rel_gap

    def args(self, *args, **kwargs):
        args = super().args(*args, **kwargs)
        args += ['relGap', self.rel_gap]
       return args

Then calling pymzn.minizinc with an instance of this class:

pymzn.minizinc(..., solver=pymzn.GurobiWithArgs())

Hope this helps.

Paolo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants