Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  How do I change the objective function in Cplex MATLAB API?

    Posted 04/23/12 09:13 AM

    Originally posted by: cgaray


    Hello,

    I am using the CPLEX MATLAB API and I am running an analysis that requires a change in the objective function coefficients after each iteration. Ideally, I'd like to use the previous solution from this MILP problem as a start for the next solution. I am also changing the bound on one of the rows after each iteration. The problem here is that after I make these changes to an existing problem, it seems that CPLEX no longer recognizes the existence of the objective function. After invoking cplex.solve() on the instance of Cplex containing the modified model (and after the initial problem was successfully solved) I get the following error:

    
    Error using cplexlink123 No Cplex.Model.obj found   Error in C:\Program Files\IBM\ILOG\CPLEX_Studio_Academic123\cplex\matlab\x64_win64\@Cplex\Cplex.p>Cplex.solve (line 1474)     Error in C:\Program Files\IBM\ILOG\CPLEX_Studio_Academic123\cplex\matlab\x64_win64\@Cplex\Cplex.p>Cplex.subsref (line 3716)
    


    The following is cplex.Model structure, showing that the objective function is indeed present:

    
    K>> cplex.Model   ans =   sense: 
    'minimization' obj: [2284x1 
    
    double] lb: [2284x1 
    
    double] ub: [2284x1 
    
    double] A: [2496x2284 
    
    double] lhs: [2496x1 
    
    double] rhs: [2496x1 
    
    double] name: 
    'shlomi' ctype: [2284x1 
    
    char] rowname: [2496x7 
    
    char] colname: [2284x5 
    
    char]
    


    Is there a way to modify the model so that I don't have to create a new instance of Cplex each time?

    Thanks,

    Chris
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: How do I change the objective function in Cplex MATLAB API?

    Posted 04/23/12 10:17 AM

    Originally posted by: Eumpfenbach


    Matlab is case sensitive. You are showing Cplex and cplex.

    Look up the documentation for MIP starts, if you want the next problem to start with the previous solution.
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: How do I change the objective function in Cplex MATLAB API?

    Posted 04/23/12 10:41 AM

    Originally posted by: cgaray


    Thanks for your response.

    cplex in this case is (probably confusingly) an instance of the Cplex class. While working on this script I just used the style of the API documentation.
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: How do I change the objective function in Cplex MATLAB API?

    Posted 04/23/12 10:47 AM

    Originally posted by: cgaray


    I should also add that I did re-check my scripts and that error results from calling cplex.solve(). It works fine for the initial problem but I can't generate a new solution after I modify the objective. MIP starts seem to work fine when I try them with a new model. I'd like to avoid re-creating the model every iteration, though.
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: How do I change the objective function in Cplex MATLAB API?

    Posted 04/24/12 12:06 AM

    Originally posted by: Eumpfenbach


    I'm just a user like you. I can't explain the problem. You might be able to skirt around it easily with something like this:

    problem = Cplex()

    ... build your initial problem ...

    problem.solve()

    for i = 1:n
    {

    old_problem = problem;
    problem = Cplex();
    problem.Model.A = old_problem.Model.A;
    problem.Model.rhs = old_problem.Model.rhs;
    .
    .
    .
    problem.Model.obj = {new objective function};
    problem.solve()
    }

    It's not really efficient coding, but it's a quick fix.
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: How do I change the objective function in Cplex MATLAB API?

    Posted 04/24/12 08:31 AM

    Originally posted by: cgaray


    Thanks, it seems like that may be the best way to go for now. I'll probably move this analysis into a MEX file. It looks like the modification of existing problems may be limited to the Java, C++ and C APIs: http://www.iro.umontreal.ca/~gendron/IFT6551/CPLEX/HTML/overviewcplex/modify.html. I wonder if a dev can confirm this? Is it possible that the MATLAB API could include this functionality? Maybe there are technical limitations.

    Chris
    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: How do I change the objective function in Cplex MATLAB API?

    Posted 04/24/12 02:43 PM

    Originally posted by: cgaray


    As an update, it seems as though I'm missing something incorrect in my code. I tried the following (based on the mipex1.m example) and it executes properly:

    % Initialize the CPLEX object
    cplex = Cplex('mipex1');
    cplex.Model.sense = 'maximize';
          
    % Use arrays to populate the model
    cplex.Model.obj   = [ 1;   2;   3; 1];
    cplex.Model.lb    = [ 0;   0;   0; 2];
    cplex.Model.ub    = [40; inf; inf; 3];
    cplex.Model.ctype = 'CCCI';
    cplex.Model.A =  [-1  1  1  10;
                       1 -3  1   0;
                       0  1  0  -3.5];
    cplex.Model.lhs = [-inf; -inf; 0];
    cplex.Model.rhs = [  20;   30; 0];
      
    % Optimize the problem
    cplex.solve();
     
    cplex.addRows(-Inf, [0 1 0 1], 10);
    cplex.Model.obj = [0; 1; 1; 0];
     
    cplex.solve()
     
    % Write the solution
    fprintf ('\nSolution status = %s \n', cplex.Solution.statusstring);
    fprintf ('Solution value = %f \n', cplex.Solution.objval);
    disp ('Values =');
    disp (cplex.Solution.x);
    disp ('Slacks =');
    disp (cplex.Model.rhs - cplex.Solution.ax);
    


    I've added a new row and modified the objective function coefficients. The previous solution is used as a MipStart.
    #CPLEXOptimizers
    #DecisionOptimization