Decision Optimization

 View Only
Expand all | Collapse all

How to remove defined variables from the docplex model

  • 1.  How to remove defined variables from the docplex model

    Posted Wed November 23, 2022 10:10 AM
    Let 'I' is the set of machines and 'P' is the set of periods. In different periods there are different numbers of jobs to process (let's say 5 jobs for each period). I want to solve this problem for each i in I and p in P in the loop and also obtain the extreme ray. After solving the problem for a particular i and p, when I call extreme ray I am getting extreme rays for all variables while I want extreme rays for that particular i and p.

    One possible solution in my mind is to keep variables related to only that i and p in the model and remove (or delete) all other variables from the model. 

    In cplex module there is a method: model.variables.delete() to delete variables. But it is not working with docplex. 

    Any alternate or other methods to get rid of the above concern?

    Thanks!

    ------------------------------
    MOHAMMAD SAMIULLAH
    ------------------------------

    #DecisionOptimization


  • 2.  RE: How to remove defined variables from the docplex model

    Posted Wed November 23, 2022 10:42 AM
    There's no delete/remove method in docplex. If you want to get rid of some specific variables in a model, I see 2 options:
    * Build the model with docplex and all variables, then get access to the underlying cplex object with the `get_cplex` method. All docplex objects have a `_index` field which correspond to the cplex index. Delete the variables with `cplex.variables.delete` method. At this point, only use the cplex module: some docplex objects will become dandling pointers.
    * Another way: the variable object in docplex have a `iter_constraints` method. So collect all the constraints for all the variables to remove. Then iterate on those constraints, and on the left and right expression, call the `set_coefficient` method to set a 0 coef to remove the var?​

    ------------------------------
    Vincent Beraudier
    ------------------------------



  • 3.  RE: How to remove defined variables from the docplex model

    Posted Mon November 28, 2022 07:59 AM
    To your question <<Let's have three constraints C1, C2, and C3. In one run I want to deactivate constraint second 'C2'. In the other run, I want to keep C2 activated but deactivate C3.>>, I would simply do something like this:

    def desactivate_and_solve(mdl, ct):
    #remove the constraint
    mdl.remove(ct)
    s = mdl.solve()
    if s:
    print(mdl.objective_value)
    else:
    print("* model has no solution")
    # put the constraint back
    mdl.add(ct)


    ------------------------------
    Vincent Beraudier
    ------------------------------