Hi Alex,
In your example, why do you not issue ar.end() when ar goes out of scope?
In my OPL code, following the examples in CPLEX documentation, I carefully issue cplex.end(), for instance, where previously I have instantiated it via a new such as cplex = new IloCplex() to avoid memory leakage.
When I tried ar.end() at my end, OPL complains that end is an undefined method. Perhaps .end() give one the end of array position like in STL iterators?
Like in C/C++, is there no need to issue ar.end() to clean up the allocated memory? Is there some other method that cleans up the allocated memory?
------------------------------
CPLEX User
------------------------------
Original Message:
Sent: Wed October 13, 2021 03:37 AM
From: ALEX FLEISCHER
Subject: OPL -- Array of floats does not sort correctly
Hi,
I shared a full example at https://github.com/AlexFleischerParis/oplscripting/blob/main/arraymethods.mod
execute{ var ar=new Array(4); ar[0]=2; ar[1]=10; ar[2]=4; ar[3]=1; ar[4]=7; writeln("length = ",ar.length); writeln("join = ",ar.join(" , ")); ar.reverse(); writeln("reverse = ",ar.join(" , ")); ar.sort(); writeln("sort default (string) = ",ar.join(" , ")); function numberorder(a,b){ return ((a==b)?(0):((a<b)?-1:1)); } ar.sort(numberorder); writeln("sort number = ",ar.join(" , "));} /*which giveslength = 5join = 2 , 10 , 4 , 1 , 7reverse = 7 , 1 , 4 , 10 , 2sort default (string) = 1 , 10 , 2 , 4 , 7sort number = 1 , 2 , 4 , 7 , 10*/
Which is part of https://www.linkedin.com/pulse/javascript-within-opl-cplex-alex-fleischer/
------------------------------
[Alex] [Fleischer]
[EMEA CPLEX Optimization Technical Sales]
[IBM]
Original Message:
Sent: Wed October 13, 2021 02:06 AM
From: Vincent Beraudier
Subject: OPL -- Array of floats does not sort correctly
The sort() method sorts the values as strings in alphabetical and ascending order.
Because of this, the sort() method will produce an incorrect result when sorting numbers.
In order to sort numbers, you need to pass a comparison function to sort:
function c(a,b){
if (a == 0) return 0;
if (a < b) return -1;
return 1;
}
yvalues.sort(c);
will produce the correct array.
------------------------------
Vincent Beraudier
Original Message:
Sent: Tue October 12, 2021 10:48 PM
From: CPLEX User
Subject: OPL -- Array of floats does not sort correctly
Hello,
Consider code:
main {
var yvalues = new Array(10);
yvalues[0] = 500;
yvalues[1] = -500;
yvalues[2] = 250;
yvalues[3] = 200;
yvalues[4] = 200;
yvalues[5] = 142.8;
yvalues[6] = 142.8;
yvalues[7] = 111.1;
yvalues[8] = 1000;
yvalues[9] = 1000;
// write the Values before sort
write("Before sort Values = [");
for (var i = 0; i < 10; i++) { write(yvalues[i], " "); } writeln("]");
yvalues.sort();//Sorting does not work
// write the Values after sort
write("After sort Values = [");
for (var i = 0; i < 10; i++) { write(yvalues[i], " "); } writeln("]");
}
Here, yvalues[] is hardcoded some floats. Then, I hope to sort this in ascending order via yvalues.sort(). Yet, as will be seen from the scripting log, this does not happen.
Please see attached image:
https://imgur.com/txXyHrB
where the entries, 1000 come before 111.1, for instance.
Is there a different function to sort yvalues array?
Thanks.
------------------------------
CPLEX User
------------------------------
#DecisionOptimization