Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Set Seeds with Flow Control- OPL

    Posted Mon February 28, 2022 05:02 AM
    Dears,

    I have a question on how to set seeds for random numbers generation. I am using OPL for a scheduling problem which requires generating random numbers. For clarity, I simplified my problem and attached all the necessary files. I was able to generate these random numbers using the pre-processing block in the main model file (Question.mod). I need to run the model let's say for 5 times (IterNum=5) from the flow control model (Question_Flow.mod). Each time I do that, I get the same random numbers however, I want to have different random numbers for each iteration. Then whenever I run the model again for another 5 iterations, I get the same randomly generated numbers in the previous 5 iteration (this is the concept of set seeds if I am not wrong). I prefer having the random number generation block in the main model and keep using IloOplCallJava as this allows me to use statistical distributions in generating the numbers.

    Would you please advise on that?

    Best regards,
    Mohamed

    ------------------------------
    Mohamed Awad
    ------------------------------

    #DecisionOptimization


  • 2.  RE: Set Seeds with Flow Control- OPL

    Posted Tue March 01, 2022 02:46 AM
    Hi,

    I want to have different random numbers for each iteration.

    ==> You may use srand with some dates

    float d;
    
    execute
    {
      d=new Date();
    }
    
    int temp=srand(ftoi(ceil(d)));
    execute
    {
      temp;
    }
    
    int randomNumber=rand(5);
    
    execute
    {
      writeln(randomNumber);
    }​

    will give different values for each run

    whereas

    float d;
    
    execute
    {
      //d=new Date();
      d=2;
    }
    
    int temp=srand(ftoi(ceil(d)));
    execute
    {
      temp;
    }
    
    int randomNumber=rand(5);
    
    execute
    {
      writeln(randomNumber);
    }

    will always give the same value

    See https://community.ibm.com/community/user/datascience/communities/community-home/digestviewer/viewthread?GroupId=5557&MID=87124&CommunityKey=ab7de0fd-6f43-47a9-8261-33578a231bb7&tab=digestviewer for all kind of random generators  in OPL

    ------------------------------
    [Alex] [Fleischer]
    [EMEA CPLEX Optimization Technical Sales]
    [IBM]
    ------------------------------



  • 3.  RE: Set Seeds with Flow Control- OPL

    Posted Tue March 01, 2022 10:52 AM
    Hi Alex,

    Thanks a million for your great help. I really appreciate the time you always spend in replying for questions.

    Maybe I couldn't understand your solution. In my problem, I need to run the model 5 times via the flow control model (Question_Flow.mod) which also exports the results of each iteration to a *.csv file.  In Question.mod, the random numbers are generated using IloOplCallJava. I need to keep the structure of both files as it is at the same time, I want to control the random numbers generated in Question.mod by setting the seeds in Question_Flow.mod. Is that possible?

    Many thanks,
    Mohamed


    ------------------------------
    Mohamed Awad
    ------------------------------



  • 4.  RE: Set Seeds with Flow Control- OPL

    Posted Tue March 01, 2022 11:30 AM
    Ok so you use a fifth way to generate random numbers : call java

    Then I would change question.mod and write

    execute{ 
    var rnd = IloOplCallJava("java.util.Random", "<init>", "()");
    			    rnd.setSeed(10+IterNum);	
    for (var da in allActivities) {
      {
    		for (var i = 1; i < allActivities.size; ++i) 
    			{
    			    			  
    		  		t = rnd.nextDouble();
    				random_time[da]=Math.round(t)*25+da.activity.duration;;
    	
      			}
       }  
     }
     
    } ​

    instead of what you wrote

    regards



    ------------------------------
    [Alex] [Fleischer]
    [EMEA CPLEX Optimization Technical Sales]
    [IBM]
    ------------------------------