Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

MATLAB : How to write a solution to a file using CPLEX in MATLAB

  • 1.  MATLAB : How to write a solution to a file using CPLEX in MATLAB

    Posted 05/14/12 06:03 AM

    Originally posted by: SystemAdmin


    Hi all,

    I have my model in LP format. I saw the example mipex2.m which read the LP file and displays the output. But I am interested in writing a complete solution to a file in txt or sol format. I could not find any commands from the documentation related to that. Kindly help me how to write a solution to a file in MATLAB.
    Thanks in Advance!!!!
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: MATLAB : How to write a solution to a file using CPLEX in MATLAB

    Posted 05/14/12 07:41 AM

    Originally posted by: Eumpfenbach


    My method: type something like

    Problem_Solution = cplex.Solution;

    Then you can see all the fields that are in the solution (not sure exactly why they are hidden to begin with, but they are).

    If you go to the x field, highlight all, you can copy and paste to a text or excel document. If you solution is really big and it freezes on the copy and paste google "printing an array value in matlab". If you want any of the other information, its all there too (basis if LP, statuscodes, etc...).
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: MATLAB : How to write a solution to a file using CPLEX in MATLAB

    Posted 05/14/12 08:24 AM

    Originally posted by: SystemAdmin


    Hi,

    Thanks for the reply. I am still not clear its not working. Can you please explain me bit more how to use this method? I am new to Matlab. And also my solution will be huge data, so only possible way is to write my solution to a file and view the output.

    Thanks
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: MATLAB : How to write a solution to a file using CPLEX in MATLAB

    Posted 05/14/12 11:50 AM

    Originally posted by: SystemAdmin


    Hi All,

    Please someone help me to my thread. I am struck here how to write a optimized solution to a file in either .sol or .txt file format in MATLAB, so that I can view all parmaters and variables in the solution as a file. Actually I am using MIP model in LP format as input. Since my solution will be huge volume of size, I am interested in directly writing the solution to a file. But I dont find any methods to call in MATLAB as we do in C++. I kindly request you to help me as soon as possible.
    Thanks
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: MATLAB : How to write a solution to a file using CPLEX in MATLAB

    Posted 05/14/12 12:10 PM

    Originally posted by: Eumpfenbach


    Once you write something like my_solution = cplex.Solution (so that you can see exactly what the cplex solution container holds, although this step isn't 100% necessary, and assuming you are using the cplex class functionality) you can use fprintf to print to a file.

    http://www.mathworks.com/help/techdoc/ref/fprintf.html

    The matlab function xlswrite might provide better functionality but is probably a bit slower.
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: MATLAB : How to write a solution to a file using CPLEX in MATLAB

    Posted 05/14/12 02:15 PM

    Originally posted by: SystemAdmin


    Is it sufficient to only have the values for integral variables in the file? In that case you could use the writeMipStart() function of the Cplex class.
    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: MATLAB : How to write a solution to a file using CPLEX in MATLAB

    Posted 05/31/12 03:26 AM

    Originally posted by: SystemAdmin


    Hi,

    I would like to output all the variables I am used in my problem. Like how I could see its possible in C++, the same way I want to output all information from my problem. I kindly request you to help me how can be sorted out this issue to extract all variables to a output file.
    Thanks
    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: MATLAB : How to write a solution to a file using CPLEX in MATLAB

    Posted 05/31/12 05:06 AM

    Originally posted by: John Cui


    Did you try writeMipStart method in Cplex class?

    John Cui
    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: MATLAB : How to write a solution to a file using CPLEX in MATLAB

    Posted 05/31/12 05:28 AM

    Originally posted by: SystemAdmin


    Hi John,

    I used the writeMipStart method in mipex2.m file which is an example matlab file in CPLEX. I just added the following line in that file.
    cpx.writeMipStart('C:\Users\Desktop\output.sol');
    Here cpx is used since it is initialized as Cplex object in this file.
    I am able to run this file and save the output as a file. But the problem is, it is only storing the binary Integer variables(since its a MIP problem). But, what I am interested is also to store other variables used in my problem. This is not happening using this writeMipStart method.
    Actually writeSolutions method using C++ CPLEX save a file and gives a complete solution list. I am looking similar way in MATLAB.

    Thanks
    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: MATLAB : How to write a solution to a file using CPLEX in MATLAB

    Posted 05/31/12 06:48 AM

    Originally posted by: SystemAdmin


    Hi,

    Someone pls reply to my previous message from this thread. I am struck here.
    #CPLEXOptimizers
    #DecisionOptimization


  • 11.  Re: MATLAB : How to write a solution to a file using CPLEX in MATLAB

    Posted 05/31/12 11:01 PM

    Originally posted by: John Cui


    Because we didn't expose writeSolution function in MATLAB API, so you don't need to look for in current edition.
    We will add it in in future release.

    However, here is a work around:

    Use cplex.writeModel('mymodel.sav'); to export your model to a file in MATLAB,
    then use CPLEX interactive to read&solve mymodel.sav, then write solution file
    by CPLEX interactive as well.

    John Cui
    #CPLEXOptimizers
    #DecisionOptimization


  • 12.  Re: MATLAB : How to write a solution to a file using CPLEX in MATLAB

    Posted 06/01/12 02:11 AM

    Originally posted by: SystemAdmin


    Hi,

    Yes, infact I am doing that workaround approach only as you specified. I have my model file in LP as well as SAV format. I am reading in that file in a matlab code and solving. But again here is the problem in producing the output. If I try to use interactive way, then I could not see my results completely since my problem size is huge and because of that I could not able to view my complete output in interactive window. Thats why I have been looking for how to save the output to a file with all variables results for the problem. My problem has more than 5000 constraints. So I guess its difficult to output all the information in the interactive window.
    I am struck here totally.
    Thanks
    #CPLEXOptimizers
    #DecisionOptimization


  • 13.  Re: MATLAB : How to write a solution to a file using CPLEX in MATLAB

    Posted 06/01/12 02:14 AM

    Originally posted by: John Cui


    I mean, use cplex interactive to write out the solution to a file!
    Like this:

    
    CPLEX> r sosex3.lp Problem 
    'sosex3.lp' read. Read time =    0.02 sec. CPLEX> o MIP Presolve eliminated 2 redundant SOS constraints. MIP Presolve eliminated 1 redundant SOS constraints. Tried aggregator 2 times. MIP Presolve eliminated 5 rows and 4 columns. MIP Presolve added 5 rows and 5 columns. MIP Presolve modified 26 coefficients. Aggregator did 4 substitutions. Reduced MIP has 16 rows, 30 columns, and 93 nonzeros. Reduced MIP has 21 binaries, 5 generals, 1 SOSs, and 0 indicators. Probing time =    0.00 sec. Tried aggregator 1 time. Presolve time =    0.02 sec. Probing time =    0.00 sec. Clique table members: 1. MIP emphasis: balance optimality and feasibility. MIP search method: dynamic search. Parallel mode: deterministic, using up to 2 threads. Root relaxation solution time =    0.00 sec.   Nodes                                         Cuts/  Node  Left     Objective  IInf  Best Integer    Best Bound    ItCnt     Gap   0     0     2811.8728     6                   2811.8728       12 0     0     2848.5950     9                     Cuts: 9       18 0     0     2849.8717    13                     Cuts: 4       26 0     0     2849.9109    13                     Cuts: 3       29 0     0     2849.9109    14                     Cuts: 3       31 *     0+    0                         3302.0000     2849.9109       31   13.69% *     0+    0                         3188.0000     2849.9109       31   10.61% 0     2     2849.9109    11     3188.0000     2849.9109       31   10.61% Elapsed real time =   0.12 sec. (tree size =  0.01 MB, solutions = 2) *     3+    3                         3089.0000     2850.2472       40    7.73%   Cover cuts applied:  2 Flow cuts applied:  1 Mixed integer rounding cuts applied:  3 Gomory fractional cuts applied:  2   Root node processing (before b&c): Real time             =    0.06 Parallel b&c, 2 threads: Real time             =    0.08 Sync time (average)   =    0.00 Wait time (average)   =    0.00 ------- Total (root+branch&cut) =    0.14 sec.   Solution pool: 3 solutions saved.   MIP - Integer optimal solution:  Objective = 3.0890000000e+003 Solution time =    0.19 sec.  Iterations = 219  Nodes = 92 Deterministic time = 2.55 ticks  (13.65 ticks/sec)   CPLEX> w sosex3.sol Incumbent solution written to file 
    'sosex3.sol'. CPLEX> Then you can get solution from sosex3.sol file.
    


    John Cui
    #CPLEXOptimizers
    #DecisionOptimization


  • 14.  Re: MATLAB : How to write a solution to a file using CPLEX in MATLAB

    Posted 06/01/12 02:20 AM

    Originally posted by: SystemAdmin


    Hi,

    Ok but what is the use of using CPLEX Interactive optimizer??? But I am interested to do all such things in a MATLAB environment. My idea is to basically implement my model in the MATLAB and solve and output the solution by displaying as well as saving to a file. This is my plan.
    #CPLEXOptimizers
    #DecisionOptimization