Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

prepare bloc to transform dates

ALEX FLEISCHER

ALEX FLEISCHERFri February 07, 2014 07:31 AM

  • 1.  prepare bloc to transform dates

    Posted Mon January 20, 2014 07:52 AM

    Originally posted by: davidoff


    Hi

    I received as an input a collection of dates and try to transform it on the fly in minutes with a prepare block

       function transformDate(dateIn){
           //2013-12-13 08:10:00 +0100
            dateTime = dateIn.split(" "); 
            date = dateTime[0]; time = dateTime[1];
            dateSplit = date.split("-");
            dateReverse = dateSplit.reverse();
            dateNew = dateReverse.join("/") + " " + time; 
            dateOut = new Date(Date.parse(dateNew));
            return dateOut.getMinutes();
       }
    }
    
    SheetConnection sheet("C:/.../dates.xlsx");
    starts from SheetRead(sheet,"starts") invoke transformDate;
    

    Since starts are to be converted in minutes, I declared starts as a collection of int in the model.

    It seems then impossible to compute, starts since it cannot be at the same time integer and string

    Is there a way to capture the transformation directly ?

     

    Note that constructing a date through Date.parse is not staightforward : one shall use new Date( Date.parse(...));

     

     

    David


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 2.  Re: prepare bloc to transform dates

    Posted Fri February 07, 2014 07:31 AM

    Hi,

    what about

     

    execute
    {
      function transformDate(dateIn){
           
            dateTime = dateIn.split(" ");
            date = dateTime[0]; time = dateTime[1];
            dateSplit = date.split("-");
            dateReverse = dateSplit.reverse();
            dateNew = dateReverse.join("/") + " " + time;
            dateOut = new Date(Date.parse(dateNew));
            return dateOut.getMinutes();
       }
       
       
       function transformDate2(dateIn){
           
            var dateTime2 =  new Date(dateIn);
            return dateTime2.getMinutes();
       }

    writeln(transformDate("13/12/2013 08:10:00"));
    writeln(transformDate2("13/12/2013 08:10:00"));
    }
     

    ?

     

    regards


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 3.  Re: prepare bloc to transform dates

    Posted Mon February 10, 2014 05:03 AM

    Originally posted by: davidoff


    The conversion works fine.

    However, my initial point was more related to an automatic transform of a date into an int through the prepare block

    I have not been able to do that straightforward. Rather, i declared in the .mod file a tuple with both a string and an int :

     tuple DateInfo{  string date;  int minutes;} {DateInfo} dateInfos = ...;
    

     

    then , in the prepare block :

      function transformDate(truc,name){
           //2013-12-13 08:10:00 +0100 YYYY-MM-DD
            writeln(truc);
            writeln(name);
            writeln("----");
            //for(var rd=1;rd<=7 ;rd++){
            for(var i=0;i< Opl.card(thisOplModel[name]);i++){
                    var record = Opl.item(thisOplModel[name],i);
                    var dateIn = record.date;
                    writeln("dateIn=",dateIn);
                    dateTime = dateIn.split(" "); 
                    date = dateTime[0]; time = dateTime[1];
                    dateSplit = date.split("-");
                    dateNew = dateSplit[1]+"/"+dateSplit[2]+"/"+dateSplit[0] + " " + time;//MM/DD/YYYY
                    dateOut = new Date(Date.parse(dateNew));
                    writeln(dateNew);
                    var minutes = dateOut.getMinutes();
                    writeln("minutes=",minutes);
                    record.minutes = dateOut.getMinutes();
             }
              return true;
       }
      
    }
    
    SheetConnection sheet("uc1.xlsx");
    dateinfos from SheetRead(sheet,"starts") invoke  transformDate;
    

    You see in the code that I transform the date into minutes in order to store it in the attribute minutes of my tuples.I still wonder if there is a more elegant way to load transformed informations directly


    #DecisionOptimization
    #OPLusingCPOptimizer