Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Importing the model and data of cplex to Python

    Posted Sat September 30, 2017 12:31 PM

    Originally posted by: felycite28


    Hello everyone ,

    I have a mathematical model with the .mod ("trial.mod")extension and data file with the extension of .dat on cplex ; I want to import this model and the initial solution of it to the python to develop a sort of improvement heuristic which is based on the initial solution . To do that , I checked the prevoius examples of this forum and applied

    main { 
    thisOplModel.generate(); 
    cplex.exportModel("trial.lp")
    }
    at the end of the .mod file. My python code is :

    import cplex
    import sys
    def  sample(filename):
        c = cplex.Cplex("trial.lp")
        try:
            c.solve()
        except CplexSolverError:
            print ("Exception raised during solve")
        return
        status = c.solution.get_status()
        print ("Solution status = " , status, ":",)
        print (c.solution.status[status])
        print ("Objective value = " , c.solution.get_objective_value())

    I have two questions :

    1) When I run my python code I am getting "No module called 'cplex' error . What did I forget or what is my mistake , how can I correct my python code to reach the aim?

    2) When i add the main block at the end of the .mod file , I run it and I got "No solution " for my mathemtical model , why ? I am asking to learn

    P.S.I use Cplex 12.6

     

    Thank you

    Regards

     

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Importing the model and data of cplex to Python

    Posted Sun October 01, 2017 05:02 AM

    Hi,

    even before answering 1) and 2)

    https://www.ibm.com/developerworks/community/forums/html/topic?id=0b6cacbe-4dda-4da9-9282-f527c3464f47

    gives some options to call OPL from Python

    1) Have you followed

    https://www.ibm.com/support/knowledgecenter/SSSA5P_12.7.0/ilog.odms.cplex.help/CPLEX/GettingStarted/topics/set_up/Python_setup.html

    ?

    2) With

    main {
    thisOplModel.generate();
    cplex.exportModel("trial.lp");
    }

    you do not get any solution since you do not call solve and that is normal.

    If you wrote

    main {
    thisOplModel.generate();
    cplex.exportModel("trial.lp");
    cplex.solve();
    }

    instead

    then you would get a solution

    NB: if you only need to generate a lp file you may use command line

    oplrun -e trial.lp model.mod

    regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Importing the model and data of cplex to Python

    Posted Mon October 02, 2017 07:53 AM

    Originally posted by: felycite28


    Hello Alex ,

    I followed the directions you have said in 1) but I got the message of "cplex is compatible with python " message . I was using Python 3.5 (Cplex 12.6) and I removed Python 3.5 and downloaded Python 2.7 and tried to follow the same direction but again I am getting the same message . Is cplex compatible with both versions of python or not ? I am trying to understand if I did something wrong

    Thank you in advance

    Regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Importing the model and data of cplex to Python

    Posted Mon October 02, 2017 07:58 AM


  • 5.  Re: Importing the model and data of cplex to Python

    Posted Mon October 02, 2017 10:47 AM

    Originally posted by: felycite28


    I guess I solved it but I am not sure

    I tried a few of IBM python examples they worked but since I need the primal solution of cplex  I inspired from your admipex6. py example ; anyway  and followed the code to do that on python :

    import cplex
    import sys
    def sample1(filename):
        c=cplex.Cplex(filename)
        try:
            c.solve()
        except CplexSolverError:
            print "Exception raised during solve"
            return
        status=c.solution.get_status()
        print "Solution Status=", status,":",
        print c.solution.status[status]
        print "Objective value=",c.solution.get_objective_value()

    but here , I still can not get the result ,objective value , I should connect with . mod and .dat but I could not achieve it .

    Where I should indicate the name of the model and data to get solution ? (If it is filename part, I could not be sure about the extension of them and I am so newly learner , basically it is nightmare)

    Thank you in advance

    Regards

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Importing the model and data of cplex to Python

    Posted Mon October 02, 2017 12:43 PM

    Hi,

    take

    dvar float+ Gas;
    dvar float+ Chloride;


    maximize
      40 * Gas + 50 * Chloride;
    subject to {
      ctMaxTotal:     
        Gas + Chloride <= 50;
      ctMaxTotal2:    
        3 * Gas + 4 * Chloride <= 180;
      ctMaxChloride:  
        Chloride <= 40;
    }

    the obj you get in OPL is 2300

    Then if you generate the lp file you get volsay.lp:

    Maximize
     obj: 40 Gas + 50 Chloride
    Subject To
     ctMaxTotal:    Gas + Chloride <= 50
     ctMaxTotal2:   3 Gas + 4 Chloride <= 180
     ctMaxChloride: Chloride <= 40
    End

    and then

    import cplex
    import sys
    def sample1(filename):
        c=cplex.Cplex(filename)
        try:
            c.solve()
        except CplexSolverError:
            print ("Exception raised during solve")
            return
        status=c.solution.get_status()
        print ("Solution Status=", status),
        print (c.solution.status[status]);
        print ("Objective value=",c.solution.get_objective_value())

    sample1("volsay.lp")   

    gives

    Tried aggregator 1 time.
    LP Presolve eliminated 1 rows and 0 columns.
    Reduced LP has 2 rows, 2 columns, and 4 nonzeros.
    Presolve time = 0.02 sec. (0.00 ticks)

    Iteration log . . .
    Iteration:     1   Dual infeasibility =             2.500000
    Iteration:     3   Dual objective     =          2300.000000
    Solution Status= 1
    optimal
    Objective value= 2300.0
    >>>

    regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Importing the model and data of cplex to Python

    Posted Tue October 03, 2017 07:57 AM

    Originally posted by: felycite28


    CryingCryingCrying tears of happiness ,

    Million thanks  Alex,


    #CPLEXOptimizers
    #DecisionOptimization