Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
Expand all | Collapse all

Cplex returns duplicate solution

  • 1.  Cplex returns duplicate solution

    Posted Tue October 27, 2020 06:46 AM
    Hello,
    I have an MIP model that is built with docplex and is being solved on a Cplex solution pool in python environment. I need solutions in a large population (100,000 solutions). My problem is that when my solution pool populates 100,000 solutions a lot of them are duplicates which I don't understands why is this happening and how can I prevent my pool from generating duplicate solutions?

    ------------------------------
    Sana Nazari
    ------------------------------

    #DecisionOptimization


  • 2.  RE: Cplex returns duplicate solution

    Posted Wed November 04, 2020 06:17 AM
    Edited by System Admin Fri January 20, 2023 04:33 PM
    It seems you posted a slightly more detailed question at https://stackoverflow.com/questions/64608122/cplex-solution-pool-generates-duplicate-solutions.  Sorry for the delay here...

    It's unexpected that the solution pool is filled with identical solutions.

    Could you please specify which version of CPLEX you use?
    Is it possible for you to share the model?  You can export a SAV file from docplex, and then it's easy for you or us to load this model in the CPLEX Interactive to reproduce the issue with the following command:

    cplex -c "read myfile.sav" "set mip pool intensity 4" "set mip pool absgap 1e75" "set mip pool relgap 1.75" "set mip limits populate 20000" "opt​"


    Also, please define how exactly you compare two solutions to determine whether they are identical. 



    ------------------------------
    Xavier
    ------------------------------



  • 3.  RE: Cplex returns duplicate solution

    Posted Wed November 04, 2020 06:41 AM
    Thank you Xavier.
    My cplex version is 12.10. I recently upgraded my docplex to the latest version. 
    I attached the SAV file of my model.

    ------------------------------
    Sana Nazari
    ------------------------------



  • 4.  RE: Cplex returns duplicate solution

    Posted Wed November 04, 2020 12:12 PM
    First impressions after looking at your model: it contains only binary variables and has no objective.
    It looks like a purely combinatorial problem, which can be very hard to solve.
    At this time, I did not manage to find any solution at all;
    how much time does it take to generate the first feasible solution?

    ------------------------------
    Philippe Couronne
    ------------------------------



  • 5.  RE: Cplex returns duplicate solution

    Posted Wed November 04, 2020 12:23 PM
    Thank you Philippe.
    It usually takes around 1.5  to 2 hours.


    ------------------------------
    Sana Nazari
    ------------------------------



  • 6.  RE: Cplex returns duplicate solution

    Posted Thu November 05, 2020 04:17 AM
    The fact that your model has no objective may have an impact on the behavior on the behavior of populate.
    From https://www.ibm.com/support/knowledgecenter/SSSA5P_12.10.0/ilog.odms.cplex.help/CPLEX/UsrMan/topics/discr_optim/soln_pool/10_populate_eg.html

    You'll see that `populate` works in two phases: first, solve to optimality, then explore the neighborhood of the optimal solution by relaxing optimality constraints.
    In your case, optimality is hard to reach, and objective is -always- zero, which may negatively impact this neighborhood research. In addition, finding a solution is very hard, so it might be possible that the number of solutions is actually very small.
    I still need to run experiments with different ideas, but it might help to set a non-empty objective, at least to break symmetries.
    Let me know if you can add an objective; I will try to come up with some fake objective to see whether this helps or not.

    ------------------------------
    Philippe Couronne
    ------------------------------



  • 7.  RE: Cplex returns duplicate solution

    Posted Thu November 05, 2020 06:17 AM
    Unfortunately, I can not add any objective for now. I don't really care about optimality. As long as I get solutions fulfilling all my constraints It's okay. Maybe I should consider changing mip emphasize to only feasibility? Now it's on default.

    ------------------------------
    Sana Nazari
    ------------------------------



  • 8.  RE: Cplex returns duplicate solution

    Posted Thu November 05, 2020 04:17 PM
    You could try adding an incumbent callback and a place in user memory to store the pool solutions. When invoked, the incumbent callback would have to compare the "new" solution to all the solutions stored in memory. If it matched any of them, the callback would reject it; if not, the callback would add it to the memory store.

    ------------------------------
    Paul Rubin
    Professor Emeritus
    Michigan State University
    ------------------------------



  • 9.  RE: Cplex returns duplicate solution

    Posted Fri November 06, 2020 06:03 AM
    Wouldn't this increase the solving time? I mean my solve time is already high, I'm not sure if it can take more than that.

    ------------------------------
    Sana Nazari
    ------------------------------



  • 10.  RE: Cplex returns duplicate solution

    Posted Fri November 06, 2020 11:36 AM
    Yes, this would likely increase the solving time. If CPLEX is rediscovering the same solution repeatedly (presumably because heuristics keep landing on it), this would do nothing to stop that. It would just prevent CPLEX from stopping with a "full" pool that actually lacked the required diversity.

    If you are just looking to generate feasible solutions, and if your integer variables are all binary, another approach would be to add a lazy constraint callback. Each time CPLEX found a feasible solution, it would invoke the callback. The callback would record the solution and then add a "no-good" constraint that would cut off that solution (but no other solutions). If you achieved the desired number of distinct solutions, the callback could terminate the solver.

    ------------------------------
    Paul Rubin
    Professor Emeritus
    Michigan State University
    ------------------------------



  • 11.  RE: Cplex returns duplicate solution

    Posted Thu November 19, 2020 11:52 AM
    Thank you. Can you explain more about the idea for a lazy constraint? I have never used callbacks and I am not much familiar with it . I read about it in the CPLEX manual and checked out CPLEX examples but I am not sure how should I implement your idea. If you give me some more details or tips I would really appreciate it.

    ------------------------------
    Sana Nazari
    ------------------------------



  • 12.  RE: Cplex returns duplicate solution

    Posted Thu November 19, 2020 12:19 PM
    I do not use docplex, so I cannot speak to the mechanics of adding a lazy constraint callback (LCC). Once added, the LCC is invoked by CPLEX each time it has a candidate solution. Inside the callback, you would add the solution to a collection of solutions and then return a new constraint that prevents the solution from being found again. Let's say that your solution consists of binary variables x_i for i in 1,...,N. Let Z be the set of indices where the new solution is 0 and O the set where it is 1. The new cut is that sum_{i in Z} x_i + sum_{i in O} (1 - x_i) >= 1, which says that any future solution must differ from this one in at least one variable. Note that this approach requires either that all variables be binary or that you consider two solutions to be the same if they match on the binary variables, regardless of any differences in non-binary variables.

    ------------------------------
    Paul Rubin
    Professor Emeritus
    Michigan State University
    ------------------------------



  • 13.  RE: Cplex returns duplicate solution

    Posted Fri November 06, 2020 06:09 AM
    Edited by System Admin Fri January 20, 2023 04:12 PM
    Sorry, I missed you last question. I export all the solutions to csv files and compare them. Here is my  method of export:
    numsol = cpx.solution.pool.get_num()
    for i in range(numsol):
    sol_pool.append([j for j, a in enumerate(cpx.solution.pool.get_values(i)) if a > 0.5])
    ii=0
    import pandas as pd
    for element in tqdm(sol_pool):

    solution=np.zeros((n,n1),dtype=np.int)
    for j in element:
    v = mdl.get_var_by_index(j)
    if v.name.startswith('x'):
    i1 = v.name.split('_')[1]
    i2 = v.name.split('_')[2]
    schedule[int(i1)][int(i2)]=1
    pd.DataFrame(solution).to_csv("solution%s.csv" %ii,index=False)
    ii+=1​
    Then I use python to compare these csv files. Also, I checked by hand to make sure everything works fine.

    ------------------------------
    Sana Nazari
    ------------------------------



  • 14.  RE: Cplex returns duplicate solution

    Posted Mon November 09, 2020 10:02 AM
    I did some experiments on your dataset. with the cplex interactive optimizer.
    To summarize, I added a (dummy) objective to ease  the behavior of populate, and tried various settings.

    More precisely:
    - I compute the sum of binary variables, not used in any indicator constraints (I wanted to avoid too many side-effects) and use it
    as a minimization objective
    _ I set a large mip gap, say 35% to stop populate phase 1 early and switch to phase 2.
    - After some experiments, the best emphasis setting I found is `feasibility` (1)
    - I set `lpmethod` to 2 and all cuts to 'aggressive' (2)

    With a 2-hour time limit, populate finds 3 solutions only. This confirms the problem is very hard. If this is not your expectation, then maybe something is wrong in the model formulation itself.

    I am attaching the log from CPLEX inetractive

    Lastly, docplex 2.16 is out today, containing native support for solution pools (see method `Model.populate_solution_pool`.
    I suggest you update your docplex version to 2.16 (a simple pip install --update should be enough from 2.15)





    ------------------------------
    Philippe Couronne
    ------------------------------



  • 15.  RE: Cplex returns duplicate solution

    Posted Tue November 10, 2020 04:31 AM
    Thank you Philippe.
    So you added these new setting to a model with an objective. Right?
    And in the limit of 2h you did not get any duplicate solutions? The model is okay, the only important thing now is to avoid duplicates and see how many actual solutions we can get. If I add these new settings to my model (no objective) will it avoid duplicates?
    And the last question, is there any documentation for the docplex pool?

    ------------------------------
    Sana Nazari
    ------------------------------



  • 16.  RE: Cplex returns duplicate solution

    Posted Tue November 10, 2020 05:24 AM
    Yes, I added both the objective and the settings.
    The settings are present to find a solution faster, I'm not sure they will work in a similar manner without any objective.
     I introduced a dummy objective from my understanding of how populate works: from a solution, it explores neighboring solutions
    by relaxing gap. This dummy objective is only present to help populate find neighboring solutions.
    Again, the fact that this model has so few solutions might indicate a problem in its formulation, which
    go well beyond my support duties.

    As for documentation, `Model.populate` returns an instance of class `docplex.mp.SolutionPool`,  which is documented.

    ------------------------------
    Philippe Couronne
    ------------------------------



  • 17.  RE: Cplex returns duplicate solution

    Posted Thu November 19, 2020 02:19 PM
    Edited by System Admin Fri January 20, 2023 04:48 PM

    I do have the exactly same issue with CP. I need to generate as many feasible solutions as possible without objective. In my example, CP generated 1000 solutions, but there were only 165 unique solutions. When I put objectives based on the above discussion, my model could not find multiple feasible solutions, but only one within a given time.

    Cplex version: 20.1.0.0 Early Release

    cp.startNewSearch();
    while(cp.next()) { ...} 





  • 18.  RE: Cplex returns duplicate solution

    Posted Thu November 19, 2020 02:35 PM
    For the particular case of CP, to get list of unique solutions, you just have to set the following solver parameters:

     - SearchType=DepthFirst
     - Workers=1

    Syntax depends on the language you are using. Please refer to the documentation for details.

    ------------------------------
    Olivier Oudot
    ------------------------------



  • 19.  RE: Cplex returns duplicate solution

    Posted Thu November 19, 2020 02:46 PM
    Edited by System Admin Fri January 20, 2023 04:37 PM

    Thanks for the info, but that setting makes my model slow. CP was able to generate hundreds of feasible solutions (most are duplicated though) within 60 s with default setting. With the new setting, no solution was found within 60 s.

    ! --------------------------------------------------- CP Optimizer 20.1.0.0 --
    ! Satisfiability problem - 1,482 variables, 7,795 constraints, 1 phase
    ! Presolve : 1,667 extractables eliminated
    ! TimeLimit = 60
    ! LogVerbosity = Terse
    ! Initial process time : 0.12s (0.12s extraction + 0.01s propagation)
    ! . Log search space : 1,270.5 (before), 1,270.5 (after)
    ! . Memory usage : 3.6 MB (before), 3.6 MB (after)
    ! Using parallel search with 8 workers.
    ! ----------------------------------------------------------------------------
    ! Branches Non-fixed W Branch decision
    * 725 0.19s 8 3 = CPx#0#180
    * 1,510 0.21s 3 1 = k#0#15
    * 16,885 0.41s 8 1 = k#0#14
    * 22,409 0.47s 1 1 = k#0#19
    * 25,804 0.58s 1 1 = k#0#19
    * 25,805 0.58s 1 1 != k#0#19
    * 25,806 0.59s 1 1 != k#0#10
    * 25,807 0.59s 1 1 != k#0#9
    * 25,808 0.59s 1 1 != k#0#8
    * 25,809 0.59s 1 1 != k#0#7
    * 25,810 0.60s 1 1 != k#0#4



    ! --------------------------------------------------- CP Optimizer 20.1.0.0 --
    ! Satisfiability problem - 1,482 variables, 7,795 constraints, 1 phase
    ! Presolve : 1,667 extractables eliminated
    ! TimeLimit = 60
    ! Workers = 1
    ! LogVerbosity = Terse
    ! SearchType = DepthFirst
    ! Initial process time : 0.13s (0.12s extraction + 0.01s propagation)
    ! . Log search space : 1,270.5 (before), 1,270.5 (after)
    ! . Memory usage : 3.6 MB (before), 3.6 MB (after)
    ! Using sequential search.
    ! ----------------------------------------------------------------------------
    ! Branches Non-fixed Branch decision





  • 20.  RE: Cplex returns duplicate solution

    Posted Thu November 19, 2020 02:58 PM
    An approach I have used (with CPLEX -- should also work with CPO) to generate multiple feasible solutions is the following.
    1. Maximize a randomly generated linear objective function, capturing intermediate (suboptimal solutions) along the way (via the solution pool in CPLEX; hopefully there is something similar in CPO).
    2. Minimize the same function, again capturing intermediate solutions.
    3. Repeat with new random objectives until either a time limit is hit or I go a certain number of iterations without generating a new solution.

    This requires an efficient method to detect duplicate solutions, and the method does not necessarily capture all feasible solutions (even without time and failure limits).

    ------------------------------
    Paul Rubin
    Professor Emeritus
    Michigan State University
    ------------------------------



  • 21.  RE: Cplex returns duplicate solution

    Posted Thu November 19, 2020 03:15 PM
    Edited by System Admin Fri January 20, 2023 04:37 PM
    Thanks! I wish Cplex automatically does the tasks for user ^^




  • 22.  RE: Cplex returns duplicate solution

    Posted Thu November 19, 2020 03:26 PM
    This is kind of a "one in ten thousand users" thing, so I'm not surprised that it is not a built-in feature. (I've not heard of it as a feature in any other MILP solver, for that matter.) Also, for what it's worth, it is known that even when you have an objective function and want only optimal solutions, not every optimal solution can be found by a branch-and-cut MIP solver, because some of the optima may not be extreme points of the integer hull.

    ------------------------------
    Paul Rubin
    Professor Emeritus
    Michigan State University
    ------------------------------