Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  docplex for CPO modelling

    Posted 05/20/19 05:26 AM

    Originally posted by: ShaCplex


    Hi Alex/Daniel,

    I have this model(kindly refer the attachment) to write in docplex. I have a good exposure in docplex MP modelling. the attached model just has a non-linearity in its first constraint and there by I am forced to use CPO. (In OPL we just have to write Using CP at the top). 

     

    when I checked the docplex CP examples the methods are all different from that of docplex MP modelling. All that in my attached model (variables,constraints,objective) can be easily created using an docplex.mp.model object, except the non-linear  constraint (product of two decision variables).

     

    what can I do in this case to make this happen? Could u pls help with some similar and relevant exmples?

     

    Thanks,

    Shahul


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: docplex for CPO modelling

    Posted 05/20/19 04:29 PM

    Hi,

    as you can see in https://www.linkedin.com/pulse/making-optimization-simple-python-alex-fleischer/

    CPO docplex and CPLEX docplex are a bit different from one another than adding using CP;

    data in a list

    the same but with constraint programming

    So you could either translate all your constraints from docplex cplex to docplex cpo.

    Or you could use OPL and call OPL from Python : https://www.ibm.com/developerworks/community/forums/html/topic?id=51d2c61e-cedc-470e-8f61-fe2ff2c35a1a

    regards

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: docplex for CPO modelling

    Posted 05/21/19 05:38 AM

    Originally posted by: ShaCplex


    Hi Alex,

     

    As there is no method of linear_exp() in CPO docplex, how can I write constraints like below having an expression is getting added up based on a special condition?

     

    for i in range(len(flights)):
                    expr = mdl.linear_expr()
                    for j in range(len(odroutes)):
                        expr.add_term(varFlightAssignment[j,i], getWeightRatio(odroutes[j]))
                    mdl.add_constraint(expr <= flights[i]['effectivePayload']*getloadfactor(flights[i]))
     


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: docplex for CPO modelling

    Posted 05/21/19 07:12 AM

    Originally posted by: ShaCplex


    Hi Alex,

    basically my question is how to create a linear expression in CPO docplex?

     

     

     

     

    Thanks,

    Shahul


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: docplex for CPO modelling

    Posted 05/22/19 12:01 PM

    Originally posted by: Olivier OUDOT


    Hello Shahul,

    If I have well understood, your docplex.mp code can be written in docplex.cp as:
     

    for i in range(len(flights)):
        lexpr = []
        for j in range(len(odroutes)):
            lexpr.append(varFlightAssignment[j,i] * getWeightRatio(odroutes[j]))
        mdl.add_constraint(sum(lexpr) <= flights[i]['effectivePayload']*getloadfactor(flights[i]))

     

    or in a more condensed form:

    for i in range(len(flights)):

        expr = sum([varFlightAssignment[j,i] * getWeightRatio(odroutes[j]) for j in range(len(odroutes))])
        mdl.add_constraint(expr <= flights[i]['effectivePayload']*getloadfactor(flights[i]))

     

    Please note that I have not executed this, there may be some typo.

     

    Hope this helps


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: docplex for CPO modelling

    Posted 05/27/19 07:40 AM

    Originally posted by: ShaCplex


    Hi,

    Thanks for the previous reply. its working for me.

     

    Can I ask you how to access the solution value of an integer / binary variable in CPO docplex?

    The methods get_value( ) and get_var_solution ( )  from the documentation is not working giving an error 

    AttributeError: 'CpoIntVar' object has no attribute 'get_value'
    

    Am I doing something wrong?

    varAssign[i,j] is a binary variable

    varJobSize is an integer variable

    how can I access their solution values after solving?

     

    Thanks,

    Regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: docplex for CPO modelling

    Posted 05/27/19 10:43 AM

    Originally posted by: Olivier OUDOT


    Hello Shahul,

     

    In docplex.cp, model and results are strictly separated.

    Everytime you call mdl.solve(), you get back an object of class docplex.cp.solution.CpoSolveResult that contains all information on the result. You can find more info on this object here: https://rawgit.com/IBMDecisionOptimization/docplex-doc/master/docs/cp/docplex.cp.solution.py.html

     

    I recommend you read the modeling documentation here: https://rawgit.com/IBMDecisionOptimization/docplex-doc/master/docs/cp/creating_model.html

    and consult some examples here: https://github.com/IBMDecisionOptimization/docplex-examples


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: docplex for CPO modelling

    Posted 05/28/19 04:32 AM

    Originally posted by: ShaCplex


    Thank you Olivier....It really helps...


    #CPLEXOptimizers
    #DecisionOptimization