Decision Optimization

Decision Optimization

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

 View Only
  • 1.  OPL -- Array of floats does not sort correctly

    Posted Tue October 12, 2021 10:48 PM
    Edited by System Admin Fri January 20, 2023 04:50 PM
    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


  • 2.  RE: OPL -- Array of floats does not sort correctly

    Posted Wed October 13, 2021 02:07 AM
    Edited by System Admin Fri January 20, 2023 04:26 PM
    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 == b) return 0;

        if (a < b) return -1;

        return 1;

      }

     yvalues.sort(c);

    will produce the correct array.



    ------------------------------
    Vincent Beraudier
    ------------------------------



  • 3.  RE: OPL -- Array of floats does not sort correctly

    Posted Wed October 13, 2021 03:37 AM
    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 gives
    
    length = 5
    join = 2 , 10 , 4 , 1 , 7
    reverse = 7 , 1 , 4 , 10 , 2
    sort default (string) = 1 , 10 , 2 , 4 , 7
    sort 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]
    ------------------------------



  • 4.  RE: OPL -- Array of floats does not sort correctly

    Posted Thu October 14, 2021 04:14 AM
    Edited by System Admin Fri January 20, 2023 04:14 PM
    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
    ------------------------------



  • 5.  RE: OPL -- Array of floats does not sort correctly

    Posted Thu October 14, 2021 05:07 AM
    arrays are javascript objects and will be destroyed by the javascript garbage collector

    regards

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