Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Read data from a file with Java

    Posted 06/16/17 08:26 AM

    Originally posted by: JoeBen


    Hello everyone.

    I would like to read data from a txt or excel or.. file using Java. I am using Cplex optimization studio 12.7. The format of file will be as follows:

    Price   Cost  Production

    1.45   4.9      -4.3

    2.98   3         32.9

    ....     ....       ....

    Then I want to fetch the values of a specific column , as Price for example, to optimize total income, so something like that :

    .....
    
    IloCplex cplex = new IloCplex();
    
    double min = ...;
    double max = ...;
    
    IloNumVar[] Price = new IloNumVar[STEP];
    
    for(int j = 0; j< STEP;j++){
    Price[i] = cplex.NumVar(min,max);
    }
    ....
    .....
    

    The problem is how to get values from a column in file and use them as IloNumVar with java?


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Read data from a file with Java

    Posted 06/16/17 10:22 AM

    This looks more like a pure Java question that is unrelated to CPLEX? You want to retrieve data from a file in a certain format. In your case it is a text file and you want the data in it returned in two double arrays (columns). I guess you better ask this question on a Java forum.

    Notice however that the CPLEX Java examples come with a class InputDataReader (which can be found in cplex/examples/src/java/InputDataReader.java). This class can read simple text files into vectors and/or matrices.

    You may want to take a look at the Facility.java example to see how to use that class.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Read data from a file with Java

    Posted 06/16/17 04:54 PM

    Originally posted by: JoeBen


    No is a pure question of cplex using Java of course because I can read values of a specific column, in my example, Price, but the values will be String or Numeric type not IloNumvar so when I want to use this values in cplex to solve an opitimization problem,I'll get an error :

    Cannot convert Numeric value to IlonumVar
    

    You can see please my short code in my previous message to understand what I mean..


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Read data from a file with Java

    Posted 06/17/17 12:58 AM

    An IloNumVar is not a plain number. It is a modeling object that represents a decision variable in an optimization model. Hence the assignment fails.

    Your code example is not clear to me. In your data file you have a column "Price" that contains floating point numbers. In your code, you create an array of IloNumVar called "Price". How are these related? Is the "Price" in the data file the objective coefficient of the variables in "IloNumVar[] Price"? Is it the coefficient in some constraint? Do you want to fix the decision variable to that value?

    Can you also show as text the model you want to create from your example data?


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Read data from a file with Java

    Posted 06/17/17 06:40 AM

    Originally posted by: JoeBen


    Ok I wish to solve an optimization problem, something like that and of course in Java:

    Minimize  IncomeTotal=Price(i)-Cost(i)*Production(i)
    subject to {
    
    1/2*Price(i) = 1/3(Cost(i)*0.68)
    
    Pmin(i)<Price(i)<Pmax(i)
    Cost(i)>0
    ...
    ...
    }
    

    So the values of Price in a time "i" I will fetch it from txt or excel file.

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Read data from a file with Java

    Posted 06/19/17 01:27 AM

    Sorry, I still don't get it. Your description suggests that Price, Cost, Production are all variables? But your data file lists values for each of the three. So if the values for Price(i), Cost(i), and Production(i) are all fetched from the data file they will be fixed, what is left to optimize?

    Also, what does a line like

    1/2*Price(i) = 1/3(Cost(i)*0.68)

    mean, given that Price(i) and Cost(i) are both fixed data values read from your data file? Which of Price(i), Cost(i), Production(i) are data and which are variables?

    Did you have a look at the various Java examples shipped with CPLEX that illustrate how to build a model?


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Read data from a file with Java

    Posted 06/23/17 11:36 AM

    Originally posted by: JoeBen


    Hi, after your last message I tried to classify my optimization problem and I fixed some problems, but still I have issue, i found cplex with java very hard with little documetation and example.

     

    About this equation  :

    1/2*Price(i) = 1/3(Cost(i)*0.68)
    

    I do like that :

    IloCplex cplex = new IloCplex();
    
    
    for (i=0;i<n;i++){
    
    cplrx.addeq(cplex.prod(0.5,price[i]),cplex.prod(0.22),cost[i]);
    
    }
    .....
    

    but I get this error :

    The method addEq(double, IloNumVar) in the type ... is not applicable for the arguments 
     (double)
    

    So how I can do the equation above with price is the type double not IloNumVar, Price in that case is a data?

    The problem is I can't get data from a file without type(I mean in my case double), but I the same time I should use this input data as iloNumVar to make it work with Cplex...
     


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Read data from a file with Java

    Posted 06/25/17 06:58 PM

    I'm not sure what you mean by "little documentation and example". CPLEX Studio ships with a rather large collection of documentation, including a fully documented Java API, and perhaps more importantly with a large collection of source code examples. Daniel's first response pointed you to one; there are many others in the same directory.

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: Read data from a file with Java

    Posted 06/25/17 08:35 PM

    Originally posted by: JoeBen


    Hi and thank you, but I'm focusing now on my issue with code, please I explain the problem in my previous message, do you have any suggestion about the solution?


    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: Read data from a file with Java

    Posted 06/26/17 01:54 AM

    Sorry, it is hard or even impossible to help you if you do not answer the questions I ask. What in your model is data and what are variables. Again, you say that Price and Cost are read as data from your data file, but in your code you try to use them as decision variables. So which is what?

    About the single line of code you showed: you wrote cplex.prod(0.22). What is that supposed to mean? A product requires two arguments. Also, if you want to multiply two double numbers you don't need cplex.prod(). You can just use the Java '*' operator.


    #CPLEXOptimizers
    #DecisionOptimization