Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Trying to create an objective function with no luck

    Posted 01/10/12 07:05 PM

    Originally posted by: Washout


    Hi all,
    I am writing some code using the Java API for Cplex 12.3 for the first time and I am having problems in constructing the objective function.
    It should look something like:
    Sum(for some Trains t)((arrivalTime(t) - departureTime(t) - optimalTravelTime)/optimalTravelTime)

    But I am getting an error :
    " bad operand types for binary operator '-'
    obj = model.sum(obj,(arrtrainIndexdestLoc - deptrainIndexinitialLoc - optimumTravelTime)/(double)optimumTravelTime);
    first type: IloIntVar
    second type: IloIntVar"

    Here is a snippet of my code
    
    
    
    public 
    
    void addObjectiveFunction(IloModeler model, IloIntVar[][] arr, IloIntVar[][] dep) 
    
    throws IloException 
    { 
    
    try
    { IloNumExpr obj = model.numExpr(); ArrayList<Train> newTrains = rw.getNewTrains(); 
    
    for(
    
    int i = 0; i < newTrains.size(); i++) 
    { Train tr = newTrains.get(i); 
    
    int trainIndex = tr.getID(); 
    
    int initialLoc = 0;     
    // for down trains, the index of the initial location in the journey 
    
    int destLoc = rw.getNumberStations() - 1;   
    // ID of the last station in the railway 
    
    int optimumTravelTime = rw.getDownTravelTime(); 
    
    if (tr.isUpTrain()) 
    {   
    // reverse applies for trains in UP direction initialLoc = rw.getNumberStations() - 1; destLoc = 0; optimumTravelTime = rw.getUpTravelTime(); 
    } obj = model.sum(obj,(arr[trainIndex][destLoc] - dep[trainIndex][initialLoc] - optimumTravelTime)/(
    
    double)optimumTravelTime); 
    } 
    }
    
    catch (IloException e)
    { System.out.println(
    "Error in creating objective function - exception: " + e); 
    } 
    }
    

    How do I construct each term properly?
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Trying to create an objective function with no luck

    Posted 01/10/12 07:52 PM

    Originally posted by: Washout


    I really should have looked more closely on the forum boards before posting - apologies. I believe I have fixed the problem with the modification of the line of code
    obj = model.sum(obj,model.prod(model.diff(arr[trainIndex][destLoc],(model.diff(dep[trainIndex][initialLoc], optimumTravelTime))),(1.0/optimumTravelTime)));
    


    I was so caught up on how to implement the division that I didnt think of the obvious - use the .prod() method and the reciprocal of my constant. Just starting out with CPLEX and it's API so be kind to this bonehead =)
    #CPLEXOptimizers
    #DecisionOptimization