Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

Need an advice for the first feasible solution on Python API

  • 1.  Need an advice for the first feasible solution on Python API

    Posted Sat November 04, 2017 04:56 PM

    Originally posted by: felycite28


    Hello everyone ,

    I have a small question and I need your advice . I coded my MIP model on python and solving it Cplex . So far , everything is perfect. Now , I am developing a heuristic algorithm for it . Firtsly I have to solve the problem partially , let me explain shortly my Python code and model .

    #here is the data

    T=5;

    x=[7, 19, 9, 12, 11];

    y=[8, 8, 7, 9, 6];

    c=cplex.Cplex()
    def setupproblem(c):

    #variables and constraints are here

    def objective (c):

    #code of objective funct;

    model.solve() everything is perfect so far.

    Here I started to code my heuristc and said :

    def myheuristic()

    My time period was T=5,  solve the above math model for time period T=2 (so all the indices of variables and data must be  2, basically take

    x=[7, 19];

    y=[8, 8];

    and solve the same model and give me the result. 

    How can I do that easily ?

    I would be very happy if you can give me an small example

    Thank you so much in advance

     

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Need an advice for the first feasible solution on Python API

    Posted Mon November 06, 2017 01:50 AM

    I can see two simple options:

    1. Make T, x, y input parameters of setupproblem() and objective() so that you can create different problems depending on the data. In one case you pass T=5, in the other T=2.
    2. Just take the T=5 problem and fix all variables to later periods to 0 (or delete them).

    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Need an advice for the first feasible solution on Python API

    Posted Mon November 06, 2017 05:20 AM

    Originally posted by: felycite28


    Hello Daniel,

    I had tried second option and realized it is not that much suitable for my data and the the result I had expected.

    For the first option , I have  a question. Let's say I my global T=20, and I am splittig into 2 by 2 . So, should I create 10 subproblem  if I understand what you offered correctly  ?If so, can you tell me an example in IBM python library which can be helpful for me?2) Is not it possible to do the same job by adding constraint or something easier and more pratic ?

    Thanks


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Need an advice for the first feasible solution on Python API

    Posted Tue November 07, 2017 06:21 AM

    Originally posted by: felycite28


    Hello Daniel, thanks for advices .  I guess I achieved to slice the data.

    Just one pythonic question for generating the model. In the original model as you can see,  I coded the original model with the defs

    def setupproblem(c), def objective (c). (c is the model)

    They take only one argument  so when I make x, y, T as input of functions , I am getting error naturally because my functions  can take only one element; 

    How can I fix this case, how can I make the sliced data ( x,y there are 5 lines like that , sliced data )  one element or is there any other way to save my model ; sorry, I am newly learner but I am learner .

    thank you so much in advance

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Need an advice for the first feasible solution on Python API

    Posted Tue November 07, 2017 09:41 AM

    You can change your functions to take more than one argument and return more than one value. If I recall correctly then previously you had something like this:

    T = 5
    z = [ ... ]
    x = [ ... ]
    y = [ ... ]
    model = cplex.Cplex()
    def setupproblem(model):
       global x
       global y
       for t in range(T):
          ...
       x = ...
       y = ...
    setupproblem(model)

    Now you would have something like this:

    def setupproblem(model, T, z):
       for t in range(T):
          ...
       x = ...
       y = ...
       return x, y
    cpx = cplex.Cplex()
    x, y = setupproblem(cpx, 5, [ ... ])

     


    #CPLEXOptimizers
    #DecisionOptimization