for {k in 1..Total_Cost.npool} { solution ("savesol" & k & ".sol"); display Trans; };
Thanks a lot, it works well in AMPL. Is there a link to show how to do that in python as I am using amplpy?
On Fri, Sep 8, 2023 at 3:47 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
CPLEX organizes the solutions as follows:To read a solution, use a command of the form
- The solution files are savesol1.sol, savesol2.sol, . . .
- The number of solution files saved is <objname>.npool -- where <objname> is replaced by the name of your model's objective function.
solution savesol<n>.sol
where <n> is replaced by the solution number -- for example, "solution savesol2.sol;" to read the second solution file. Here is an example of a loop that reads through all of the solutions:for {k in 1..Total_Cost.npool} { solution ("savesol" & k & ".sol"); display Trans; };
(Replace Total_Cost by the name of your objective, and "display Trans;" by the commands you want to use to view or process each solution.)
--
Robert Fourer
We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
Thanks a lot for your reply. I changed the name of the objective function from profit to cost, I tried the following :
for i in range(1, ampl.get_value("cost.npool") + 1):
ampl.eval(f"solution savesol{i}.sol;")
print(i, ampl.get_data(r"x").to_dict())
But it gives me this error
File "C:\Users\enhan\PycharmProjects\strategiesHM\scripts\get_AMPL_solution.py", line 53, in main
for i in range(1, ampl.get_value("cost.npool") + 1):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\enhan\AppData\Local\Programs\Python\Python311\Lib\site-packages\amplpy\ampl.py", line 437, in get_value
return self._impl.getValue(scalar_expression)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: file -
line 1 offset 33
Bad suffix .npool for cost
Any idea about resolving this error? thanks
On Sun, Sep 10, 2023 at 6:14 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
Using amplpy, you can do that as follows:
from amplpy import AMPL
ampl = AMPL()
ampl.eval(
r"""
set I := 1..1000000;
var x{I} integer >= 0 <= 1;
param v{i in I} := 10 + Irand224() mod 200;
param w{i in I} := v[i] + Irand224() mod 200;
maximize profit: sum{i in I} v[i] * x[i];
s.t. capacity: sum{i in I} x[i] <= 200;
"""
)
ampl.option["solver"] = "cplex"
ampl.option["cplex_options"] = "poolstub savesol"
ampl.solve()
for i in range(1, ampl.get_value("profit.npool") + 1):
ampl.eval(f"solution savesol{i}.sol;") # load the solution
print(i, ampl.get_data(r"{i in I: x[i] > 0} x[i]").to_dict()) # display the non-zero variables
--
Filipe Brandão
We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
On Tue, Sep 12, 2023 at 7:37 AM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
Did you set CPLEX options with ampl.option["cplex_options"] = "poolstub savesol"? Please send us the output from cplex, it should indicate that it exported that suffix if poolstub was set.
--
Filipe Brandão
We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
On Thu, Sep 14, 2023 at 9:03 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Could you please send us the line where you set Gurobi options?
It should be something like ampl.option["gurobi_options"] = "solstub=savesol" but the solver output only shows "sol:poolgapabs = 150". You can combine the two options as follows:
...
ampl.option["gurobi_options"] = "solstub=savesol sol:poolgapabs = 150"
...
On Thu, Sep 14, 2023 at 8:59 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
Could you please send us the line where you set Gurobi options?
It should be something like ampl.option["gurobi_options"] = "solstub=savesol" but the solver output only shows "sol:poolgapabs = 150". You can combine the two options as follows:
...
ampl.option["gurobi_options"] = "solstub=savesol sol:poolgapabs = 150"
...
On Thu, Sep 14, 2023 at 7:51 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:Could you please send us the line where you set Gurobi options? It should be something like ampl.option["gurobi_options"] = "solstub=savesol" but the solver output shows "sol:poolgapabs = 150". You can combine the two options as follows:
...
ampl.option["gurobi_options"] = "solstub=savesol sol:poolgapabs = 150"
...
On Thu, Sep 14, 2023 at 7:50 PM UTC, Mail Delivery Subsystem <mailer...@googlemail.com> wrote:
Message not delivered
There was a problem delivering your message to am...@googlegroups.com. See the technical details below.
LEARN MORE
This link will take you to a third-party site
The response was:
Message rejected by Google Groups. Please visit http://mail.google.com/support/answer/188131?hl=en to review our Bulk Email Senders Guidelines.
On Thu, Sep 14, 2023 at 7:50 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:Could you please send us the line where you set Gurobi options? It should be something like ampl.option["gurobi_options"] = "solstub=savesol" but the solver output shows "sol:poolgapabs = 150". You can combine the two options as follows:
ampl.option["gurobi_options"] = "solstub=savesol sol:poolgapabs = 150"
On Thu, Sep 14, 2023 at 8:31 AM UTC, AMPL Google Group <am...@googlegroups.com> wrote:You have Gurobi 5.6.3 and CPLEX 12.6.0.1, which are extremely old versions of both solvers (release in 2014). By upgrading to the latest version, with Gurobi 10, you can do that as follows:
from amplpy import AMPL
ampl = AMPL()
ampl.eval(
r"""
set I := 1..10000;
var x{I} integer >= 0 <= 1;
param v{i in I} := 10 + Irand224() mod 200;
param w{i in I} := v[i] + Irand224() mod 200;
maximize profit: sum{i in I} v[i] * x[i];
s.t. capacity: sum{i in I} x[i] <= 200;
"""
)
ampl.option["solver"] = "gurobi"
ampl.option["gurobi_options"] = "solstub=savesol"
ampl.solve()
for i in range(1, ampl.get_value("profit.npool") + 1):
ampl.eval(f"solution savesol{i}.sol;")
print(
ampl.get_data(r"{i in 1.._nvars: _var[i] != 0} (_varname[i], _var[i])").to_list(
skip_index=True
),
)
In the code example from the previous message I retrieved the values for variable x, but you need to adjust the code to the variable names in your models. In the code example above, it retrieves the values for all non-zero variables in a list of pairs (variable, value).
Also, how to add a new seed to this option ampl.setOption('cplex_options','poolstub savesol mipsearch = 2 nodesel = 3')
I tried this, but doesn't work
random_num = round((random.uniform(0.0, 10000)),0)
seed_value = f'seed=random_num'
ampl.setOption('cplex_options',"poolstub savesol mipsearch = 2 nodesel = 3 seed_value")
On Fri, Sep 15, 2023 at 9:25 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
it works perfect now, thanks a lot.
I have another issue, I set this option ampl.setOption('cplex_options','poolstub savesol mipsearch = 2 nodesel = 3') , and solved the model 10 times using looping. The pool of the first iteration contains 28 solutions, but afterwards the pool of at any other iteration contains only two solutions. Is there a reason for that? I am trying to generate as much feasible solutions as passible
On Fri, Sep 15, 2023 at 9:43 AM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
When you set the same option multiple times only the last value is kept. For instance:
ampl.setOption('cplex_options','poolstub savesol')
ampl.setOption('cplex_options', "mipsearch = 2")
is equivalent to just:
ampl.setOption('cplex_options', "mipsearch = 2")
In order to set both options you need to set them as follows:
ampl.setOption('cplex_options','poolstub savesol mipsearch = 2')
--
Filipe Brandão
We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
Thanks,
I used this
random_num = round((random.uniform(0, 10000)), 0)
ampl.setOption("cplex_options", f"poolstub savesol mipsearch = 2 nodesel = 3 seed={random_num}")
still gives an error, I do not know where ".0" comes from
CPLEX 22.1.1.0: poolstub savesol
mipsearch = 2
nodesel = 3
seed=4629
Unknown keyword ".0"
CPLEX 22.1.1.0: Error in $cplex_options.
file -
line 1 offset 33
Bad suffix .npool for cost
On Sat, Sep 16, 2023 at 10:24 AM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
Regarding the number of solutions found, there are options like poolintensity that may help (see all options for CPLEX at https://dev.ampl.com/solvers/cplex/options.html).
Regarding seed, you can do it as follows:
random_num = round((random.uniform(0.0, 10000)),0)
ampl.setOption('cplex_options", f"poolstub savesol mipsearch = 2 nodesel = 3 seed={random_num}")
--
Filipe Brandão
We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
Even after rounding random_num is still a float so there is still a ".0" in formated string . You can cast it it int or format the f-string with 0 decimal points as follows:
ampl.setOption("cplex_options", f"poolstub savesol mipsearch = 2 nodesel = 3 seed={random_num:.0}")
On Sat, Sep 16, 2023 at 4:41 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Thanks,
I used this
random_num = round((random.uniform(0, 10000)), 0)
ampl.setOption("cplex_options", f"poolstub savesol mipsearch = 2 nodesel = 3 seed={random_num}")
still gives an error, I do not know where ".0" comes from
CPLEX 22.1.1.0: poolstub savesol
mipsearch = 2
nodesel = 3
seed=4629
Unknown keyword ".0"
CPLEX 22.1.1.0: Error in $cplex_options.
file -
line 1 offset 33
Bad suffix .npool for cost
On Sat, Sep 16, 2023 at 10:24 AM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
Regarding the number of solutions found, there are options like poolintensity that may help (see all options for CPLEX at https://dev.ampl.com/solvers/cplex/options.html).
Regarding seed, you can do it as follows:
random_num = round((random.uniform(0.0, 10000)),0)
ampl.setOption('cplex_options", f"poolstub savesol mipsearch = 2 nodesel = 3 seed={random_num}")
--
Filipe Brandão
We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.