Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

Writing correct date and time values to external csv files

ALEX FLEISCHER

ALEX FLEISCHERTue February 14, 2017 03:19 AM

Archive User

Archive UserTue February 14, 2017 08:33 PM

ALEX FLEISCHER

ALEX FLEISCHERWed February 15, 2017 04:44 AM

Archive User

Archive UserWed February 15, 2017 07:40 AM

  • 1.  Writing correct date and time values to external csv files

    Posted Sun February 12, 2017 02:28 PM

    Originally posted by: K_N_Srikanth


    I have internal DATE and TIME fields in my OPL code, as well as in imported ODME data tables. They work fine, and the output tables show correct date & time values on the ILOG DOC screen.

    I have a problem with writing these date and time values out to an external csv file.

     

    1. I am using a single-row input table Parameters_DateTime, shown within the _odm.mod file as

    tuple TParametersDateTime {
      int referenceDate;
      int dateIncrement;
      int referenceTime;
      int timeIncrement;
      string comment;
    };

    TParametersDateTime parametersDateTime = .

    ..;

     

     

    2. The relevant tuple whose info I'm trying to print to the output file is 

    tuple TSolutionTruckRoutes {

      ...  (other fields)
      int startDate;         //mapped to DATE field in ODME input table
      int startTime;         //mapped to TIME field in ODME input table
      int endDate;         //mapped to DATE field in ODME input table
      int endTime;        //mapped to TIME field in ODME input table
    };

    {TSolutionTruckRoutes} solutionTruckRoutes;

     

    3. External parameters OUTPUT_FILE_FOLDER and BACKSLASH are read in from a Parameters.csv input data file

    string OUTPUT_FILE_FOLDER = parameter["OUTPUT_FILE_FOLDER"]; 
    string BACKSLASH = parameter["BACKSLASH"];

     

     

    4. The OPL Script executable that's presently failing is 

    execute WriteOutputFiles {
        var wOutFilePathAndName = OUTPUT_FILE_FOLDER + BACKSLASH + "TruckRoutes.csv";

        var f2=new IloOplOutputFile(wOutFilePathAndName);
        
        f2.writeln(    "//Index,","Truck,","Type,","Order,","Customer,","Product,","Tons,",
                    "sPlant,","ePlant,",
                      "sNodeType,","eNodeType,","sDate,","sTime,","eDate,","eTime,","Distance,",
                      "LoadMinutes,","UnloadMinutes,","WashMinutes,","TravelMinutes,","TotalMinutes,",
                      "sAddress,","eAddress,","Comment"
                  );
        for(var str in solutionTruckRoutes)    {
            var wStartDate = new Date(parametersDateTime.referenceDate + str.startDate
                                    //+ parametersDateTime.referenceTime + str.startTime
                                    );
            var wEndDate = new Date(parametersDateTime.referenceDate + str.endDate
                                    //+ parametersDateTime.referenceTime + str.endTime
                                    );
            
            var wStartDay = wStartDate.getDate();
            var wStartMonth = wStartDate.getMonth();
            var wStartYear = wStartDate.getYear();
            var wStartHour = wStartDate.getHours();
            var wStartMinute = wStartDate.getMinutes();
            
            var wEndDay = wEndDate.getDate();
            var wEndMonth = wEndDate.getMonth();
            var wEndYear = wEndDate.getYear();
            var wEndHour = wEndDate.getHours();
            var wEndMinute = wEndDate.getMinutes();
        
            f2.writeln(     str.index,",",
                             str.truckId,",",
                             str.truckTypeId,",",
                             str.orderId,",",
                             str.customerId,",",
                             str.productId,",",
                             str.tonsProduct,",",
                             
                             str.plantStart,",",
                             str.plantEnd,",",
                             str.startNodeTypeId,",",
                             str.endNodeTypeId,",",
                             
                             wStartDay,"-",wStartMonth,"-",wStartYear,",",        //wStartDate.toLocaleString(),",",
                             wStartHour,":",wStartMinute,",",                    //str.startTime,",",
                             wEndDay,"-",wEndMonth,"-",wEndYear,",",                //wEndDate.toLocaleString(),",",
                             wEndHour,":",wEndMinute,",",                        //str.endTime,",",
                             
                             str.distance,",",
                             str.loadMinutes,",",
                             str.unloadMinutes,",",
                             str.washMinutes,",",
                             str.travelMinutes,",",
                             str.totalMinutes,",",
                             
                             "'" + str.startFullAddress+ "'",  ",",
                             "'" + str.endFullAddress  + "'",   ",",
                             
                             str.comment
                        );
        }
        f2.close();

    }

     

      The problem is that all date and time values showing up in the TruckRoutes.csv file show the OPL reference date & time i.e. 31-11-1969  for date  and  19:00 for time. I'm doing something stupid when trying to create the date and time values for external output, but I can't figure out what it is.

     

    Help, please... :-)

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Writing correct date and time values to external csv files

    Posted Tue February 14, 2017 03:19 AM

    Hi,

    could you tell me what you get if you add

    execute
    {
    writeln(solutionTruckRoutes);
    }

    ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Writing correct date and time values to external csv files

    Posted Tue February 14, 2017 07:40 AM

    Originally posted by: K_N_Srikanth


    (Answer edited at 0810 US_EST)

    Alex:

     

    All dates show up as 0. The times are correct, shown as in minutes_of_day.  

    e.g.  writeln shows                                         0          1430                     0          0000                    //1430 = (23 hours * 60) + 50

    The values shown onscreen are   2016-08-01    23:50:00    2016-08-01    00:00:00

    (I know the 2nd pair of date/time values should be 2016-09-01  00:00:00, but that is a bug in my code)

    Regards,

    Sri


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Writing correct date and time values to external csv files

    Posted Tue February 14, 2017 11:14 AM

    Hi,

     

    you should remember that time is in mili second and that getMonth() gives a value between 0 and 11!

     

    year: any integer

    month: range 0-11, where 0 = January, 1 = February, and so on

    day: range 1-31, default 1

    hours: range 0-59, default 0

    minutes: range 0-59, default 0

    seconds: range 0-59, default 0

    mseconds: range 0-999, default 0

     

      tuple TSolutionTruckRoutes {

     
      int startDate;         //mapped to DATE field in ODME input table
      int startTime;         //mapped to TIME field in ODME input table
      int endDate;         //mapped to DATE field in ODME input table
      int endTime;        //mapped to TIME field in ODME input table
    };

    {TSolutionTruckRoutes} solutionTruckRoutes={ <0,1430,0,0> };

    execute
    {
    writeln(solutionTruckRoutes);
    }
     
     
     execute WriteOutputFiles {
     
     var parametersDateTimereferenceDate=new Date();
      var parametersDateTimereferenceTime=0;
     
        var wOutFilePathAndName = "TruckRoutes.csv";

        var f2=new IloOplOutputFile(wOutFilePathAndName);
        
        f2.writeln(    "sDate,","sTime,","eDate,","eTime,"
                  );
        for(var str in solutionTruckRoutes)    {
            var wStartDate = new Date(parametersDateTimereferenceDate + str.startDate*1000*24*3600
                                    + parametersDateTimereferenceTime + str.startTime*1000*60
                                    );
            var wEndDate = new Date(parametersDateTimereferenceDate + str.endDate*1000*24*3600
                                    + parametersDateTimereferenceTime + str.endTime*1000*60
                                    );
            
            var wStartDay = wStartDate.getDate();
            var wStartMonth = wStartDate.getMonth();
            var wStartYear = wStartDate.getYear();
            var wStartHour = wStartDate.getHours();
            var wStartMinute = wStartDate.getMinutes();
            
            var wEndDay = wEndDate.getDate();
            var wEndMonth = wEndDate.getMonth();
            var wEndYear = wEndDate.getYear();
            var wEndHour = wEndDate.getHours();
            var wEndMinute = wEndDate.getMinutes();
        
            f2.writeln(    
    //         
                             
                             wStartDay,"-",wStartMonth+1,"-",wStartYear,",",        //wStartDate.toLocaleString(),",",
                             wStartHour,":",wStartMinute,",",                    //str.startTime,",",
                             wEndDay,"-",wEndMonth+1,"-",wEndYear,",",                //wEndDate.toLocaleString(),",",
                             wEndHour,":",wEndMinute,","                       //str.endTime,",",
                             
    //                        
                        );
        }
        f2.close();

    }

    gives

    sDate sTime eDate eTime  
    15-2-2017 17:00 14-2-2017 17:10

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Writing correct date and time values to external csv files

    Posted Tue February 14, 2017 08:33 PM

    Originally posted by: K_N_Srikanth


    Alex:

     

      Your suggestion above fixes part of my problem, thanks - I am able to get the correct hh:mm values now (subject to an offset I need to make for the fact that the values from the code above seem to be in GMT, and I need to print the US_EST values). However, the date values are still an issue.

     

      When you use 

     var parametersDateTimereferenceDate=new Date();

    above, you get the exact dd/mm/yyyy of the time when the program is run. However, I have an input dataset spanning 3 months of customer orders, each with a start/end date and start/end time window for deliveries. The program is run several times, each time with a 24 hour planning horizon and a different parameters_DateTime.referenceDate   (like  2016-08-01,  2016-08-02  etc). This results in a series of different daily schedules, each shown in the output table TruckRoutes for each OPL scenario.

      The main issue seems to be that I cannot recover the value of parametersDateTime.referenceDate  from within OPL code.  The input ODME table has this as a DATE field, the mapped OPL table shown in _odm.mod has this as an int field.  When I do  

      writeln( parametersDateTime.referenceDate);

    the scripting log shows 0. It looks like even though this is the reference date for the entire run, and all DATE vars within the OPL code show values of +1, +2  etc  (representing dates that are 1, 2, ..  days after the referenceDate of 2016-08-01 ), I cannot seem to recover this value of 2016-08-01 to use when printing to the output .csv file.

     

    I have tried 

            wStartDate = new Date((parametersDateTime.referenceDate + str.startDate)*1000*24*3600
                                + (parametersDateTime.referenceTime + str.startTime)*1000*60
                                    );
            wEndDate = new Date((parametersDateTime.referenceDate + str.endDate)*1000*24*3600
                              + (parametersDateTime.referenceTime + str.endTime)*1000*60
                                    );
            

            wStartMonth = wStartDate.getMonth() + 1;
            wStartDay = wStartDate.getDate();
            wStartYear = wStartDate.getYear();
            wStartHour = wStartDate.getHours();
            wStartMinute = wStartDate.getMinutes();
            
            wEndDay = wEndDate.getDate();
            wEndMonth = wEndDate.getMonth() + 1;
            wEndYear = wEndDate.getYear();
            wEndHour = wEndDate.getHours();
            wEndMinute = wEndDate.getMinutes();

      The day/month/year values from this code always print out as 1-1-1970  (the base reference date 31-12-1969 for the OPL dating system, plus some hours representing the increasing time as each truck travels along its route).

       An option would be to duplicate parame

    ters_DateTime.referenceDate in an input parameter string, but in a different mm/dd/yyyy format, as  "08/01/2016", and read that with     

     var wRefDateFromString = new Date("08/01/2016");

      I could then use statements like 

            wStartDate = new Date((wRefDateFromString + str.startDate)*1000*24*3600
                                + (parametersDateTime.referenceTime + str.startTime)*1000*60
                                    );
            wEndDate = new Date((
    wRefDateFromString + str.endDate)*1000*24*3600
                              + (parametersDateTime.referenceTime + str.endTime)*1000*60
                                    );

     This could work, but it would leave me with the awkward need to insist that the user must specify the referenceDate twice within the same single-row parameters table, and in different formats (I think OPL insists that date info must be in mm/dd/yyyy format, if it is read from a string). I would prefer to avoid this.

     

      So, to cut a long question short - how can I recover the value of parameters_DateTime.referenceDate as a meaningful offset from the OPL base date of 31-Dec-1969? I always seem to get 0 for its value, no matter what I try.

     

    (I have uploaded the entire workspace Argos_RMS_2017_02_14b.zip  to a \Basis folder in a Box for Cementos Argos, and given you access to the box, if you want to access it ).

    Regards,

    Sri


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Writing correct date and time values to external csv files

    Posted Wed February 15, 2017 04:44 AM

    Hi,

    if you change

     var parametersDateTimereferenceDate=new Date();

    into

     var parametersDateTimereferenceDate=new Date(2000, 0, 1, 14, 35, 12, 820);

    then you will get in the csv

    sDate sTime eDate eTime
    2/1/2000 14:25 1/1/2000 14:35

    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: Writing correct date and time values to external csv files

    Posted Wed February 15, 2017 07:40 AM

    Originally posted by: K_N_Srikanth


    Alex:

     

      OK. But I don't see how to get the values '2000, 0, 1' from my parameters_DateTime  single-row table. Like I said,  writeln( parameters_DateTime.referenceDate) just shows 0 .


    #DecisionOptimization
    #OPLusingCPLEXOptimizer