Original Message:
Sent: Sat April 22, 2023 02:38 PM
From: Mohamed Awad
Subject: How to use Java function in OPL Model?
Hi Alex,
Thank you for you reply. It will help to achieve what I am looking for. I have a question on:
var rnd = IloOplCallJava("java.util.Random", "<init>", "()");
How can I use IloOplCallJava to generate random integer number between upper and lower limits. I tried the following but it didn't work:
min + rnd.nextInt(max - min + 1);
Could you please suggest a way to do that.
Best regards,
Mohamed
------------------------------
Mohamed Awad
------------------------------
Original Message:
Sent: Tue April 11, 2023 04:55 AM
From: ALEX FLEISCHER
Subject: How to use Java function in OPL Model?
Hi,
Let me try to address your 2 questions:
1) How to call java from OPL ?
IloOplCallJava
/*IloOplCallJava : This function calls a Java method from a scripting block*/execute{ var convert180toRadian = IloOplCallJava("java.lang.Math","toRadians", "", 180);writeln("180 degrees in radian : ",convert180toRadian); } /*180 degrees in radian : 3.141592654*/ execute{var title="title";var msg="hello"; IloOplCallJava("javax.swing.JOptionPane", "showMessageDialog","(Ljava/awt/Component;Ljava/lang/Object;Ljava/lang/String;I)V", null, msg, title, 1); } 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);}}
2) Read an excel file in OPL : Use SheetRead
https://github.com/AlexFleischerParis/oplexcel/blob/main/read1cell.mod
int value=...; execute{ writeln("value=",value);}subject to{ }int value2=value+1;/*which givesvalue=4*/
https://github.com/AlexFleischerParis/oplexcel/blob/main/read1cell.dat
SheetConnection s("read1cell.xlsx");value from SheetRead(s,"B1");value2 to SheetWrite(s,"B5");
------------------------------
[Alex] [Fleischer]
[Data and AI Technical Sales]
[IBM]