Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Redirecting output from execute blocks in OPL model using Java

    Posted 04/05/15 02:28 PM

    Originally posted by: IrvL


    I have a CP model and am calling it from Java, and using IloCP.next() to loop through solutions.  As I get each solution, I call IloOplModel.postProcess() to do some computations on the solution and execute an OPL execute() block that does output.  I would like to put that output into a file, that is different each time next() and postProcess() are executed.

    I took the sample oplrunsample.java, and added these lines around opl.postProcess() for a CPLEX model to see if I could capture the output:

                            FileOutputStream fout = new FileOutputStream(new File("irv.out"));
                            IloOplFactory.getOplFactoryFrom(opl).setOut(fout);
                            opl.postProcess();
                            fout.close();
    

    I get a "contact IBM" message:

    ### UNEXPECTED UNKNOWN ERROR ...
    java.lang.RuntimeException: internal error (Please notify IBM)
    at ilog.opl.opl_lang_wrapJNI.IloOplModel_postProcess(Native Method)
    at ilog.opl.IloOplModel.postProcess(IloOplModel.java:656)
    at oplrunsample.OplRunSample.run(OplRunSample.java:131)
    at oplrunsample.OplRunSample.main(OplRunSample.java:32)
     

    Is there an issue with IloOplFactory.setOut()  from Java?

    This is CPLEX Optimization Studio 12.6.1 on Windows x64.

        -Irv

     


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 2.  Re: Redirecting output from execute blocks in OPL model using Java

    Posted 04/07/15 06:04 AM

    Originally posted by: Ph.Gregoire


    Hello Irv,

    I get the issue by modifying the opl_interfaces\java\oplrunsample in COS 12.6.0.1 too.

    I get a GPF when running with a cplex .mod, with same call stack (i.e. calling from postProcess(IloOplModel.java:656) ).
    This looks like a dereferenced native object somehow (I suspect that the original outstream wrapper is cached internally and when you set the new one, some native classes still reference the replaced and now-deleted native wraper object)

    Now, if I setOut() from the beginning , right after the new IloOplFactory();, I get the trace in the output file as expected.

    Even though it could sound awkward, what you could do is to setOut() from the beginning with an OutputStream implementation that would redirect to a modifiable FileOutputStream?

    PhG


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 3.  Re: Redirecting output from execute blocks in OPL model using Java

    Posted 04/07/15 08:22 AM

    Originally posted by: Ph.Gregoire


    More on this, if you also do a setOut() on the cplex (or cp) object, there is no more crash:

    So, that's probably the quickest solution, opl.getCP().setOut(newFileOS);

    PhG


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 4.  Re: Redirecting output from execute blocks in OPL model using Java

    Posted 04/07/15 01:03 PM

    Originally posted by: IrvL


    Philippe:

    Thanks.  This idea works, and I'll use it.  I guess this ought to be documented.

       -Irv


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 5.  Re: Redirecting output from execute blocks in OPL model using Java

    Posted 04/08/15 03:59 AM

    Originally posted by: Ph.Gregoire


    Well, it is definitely a defect, at worse there should be a decent error message, at best the factory should propagate the change to the underlying engine...

    This being said, having a switchable-output OutpuStream setup at the beginning once for all is not a bad idea, it can allow extra versatility, such as forking the stream to both a file and the console, prefix with timestamp, merge error/out/warning to a single stream, ...

    I'm attaching a very basic example of this. you set it on your factory at the beginning and then use switchOutputStream() at will.

     

    PhG


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 6.  Re: Redirecting output from execute blocks in OPL model using Java

    Posted 04/08/15 09:49 AM

    Originally posted by: IrvL


    Philippe:

    Thanks again for responding.  I did implement something similar to your example and it is working well.  

    What I am doing is opening a file just before calling opl.postProcess() and having the output stream (like yours) then also add output to that file if a file is opened, and then the file can be closed.  One thing I discovered is that I need to flush the optimizer stream when the file is opened, otherwise the optimizer's unflushed output also appears in the OPL output, which I don't want.

    -Irv


    #DecisionOptimization
    #OPLusingCPOptimizer