Decision Optimization

 View Only
  • 1.  Random within OPL

    Posted Fri April 07, 2017 10:20 AM

    Hi,

    in order to cope with simulation, robustness, Monte Carlo ... random functions are very useful.

    In OPL, we have 4 random generators and with 3 of them you can set the random seed.

    Let me list them and then show examples on how to set random seeds and then use them.

    • Cplex random seed
    • CPO random seed
    • OPL random seed
    • OPL scripting random

    int sr=srand(2); // OPL random seed

    execute
     {
     sr;
     writeln("OPL random seed=",sr);
     }
     
    int oplrandomnumber=rand(20);

    execute
    {
    writeln("OPL random number =",oplrandomnumber);
    }
     
    execute
    {
    cplex.randomseed=3; // CPLEX random seed
    }

    execute
    {
    writeln("scripting random function = ",Math.random());  // scripting random function
    writeln("cplex random seed =",cplex.randomseed);
    }

     

    gives

    OPL random seed=2
    OPL random number =5
    scripting random function = 0.001251221
    cplex random seed =3

    and

    using CP;

    int sr=srand(2); // OPL random seed
     
     execute
     {
     sr;
     writeln("OPL random seed=",sr)
     }
     
     int oplrandomnumber=rand(20);

    execute
    {
    writeln("OPL random number =",oplrandomnumber);
    }
     
     
    execute
    {
    cp.param.RandomSeed=4; // CPO random seed
    }

    execute
    {
    writeln(Math.random());  // scripting random function
    writeln("cpo random seed=",cp.param.randomseed);

    }

    gives

    OPL random seed=2
    OPL random number =5
    0.001251221
    cpo random seed=4

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  RE: Random within OPL

    Posted 4 days ago
    Edited by ALEX FLEISCHER 4 days ago

    A 5th way is to call a function outside OPL that can be in java or python or any language.

    execute{ 
    writeln("5 random numbers");
    var rnd = IloOplCallJava("java.util.Random", "<init>", "()");
    rnd.setSeed(1);	
    for(var i=1;i<=5;i++)
    {			    			  
     var t = rnd.nextDouble();
     writeln(t);
    }
    writeln("Gaussian");
    for(var i=1;i<=5;i++)
    {			    			  
     var t = rnd.nextGaussian();
     writeln(t);
    }
    }

    in https://github.com/AlexFleischerParis/oplscripting/blob/main/IloOplCallJava.mod



    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------