Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  A Problem About Piecewise Linear Function(Cplex+Java)

    Posted 03/29/12 10:50 PM

    Originally posted by: QQDQ_Lilian_Cai


    Hello!
    I have a problem in using piecewise as I expect. i=1,2,...,252. when i changes, both num1 and Si will change. However, I exported model.lp and found that all the Si ranging from i=1 to i=252 using the same num1 of i=252 instead of the corresponding num1 ranging from i=1 to i=252. I am wondering how to solve this problem.

    The code in Java is as follows:

    // set Si
    private void setS(WholeEntity entity) {
    try {
    double[] a = new double[1];
    double[] b = new double[2];
    for (int i = 0; i < 252; i++) {
    double num1 = entity.getLastDayStock()[i].getClosingPrice();
    if (containsInN(i)) {
    // System.out.println("==first MILP======"i"======"+num1);
    a[0] = 5000 / num1;
    b[0] = -num1;
    b[1] = 0;
    Si = cplex.piecewiseLinear(x[i], a, b, 5000 / num1, 0);
    }

    We considered solving this problem by writing Si in an array. However, all the results turned out to be zero. The code in Java is as follows:

    IloNumExpr[] Si = new IloNumExpr252;
    double[] a = new double[1];
    double[] b = new double[2];
    for (int i = 0; i < 252; i++) {
    double num1 = entity.getLastDayStock()[i].getClosingPrice();
    if (containsInN(i)) {
    a[0] = 5000 / num1;
    b[0] = -num1;
    b[1] = 0;
    Si[i] = cplex.piecewiseLinear(x[i], a, b, 5000 / num1, 0);
    }
    }

    Whoever has some ideas about this problem, please don't hesitate to tell us. We will really appreciate it.
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: A Problem About Piecewise Linear Function(Cplex+Java)

    Posted 03/30/12 07:48 PM

    Originally posted by: SystemAdmin


    Your first approach overwrites Si in each iteration of the loop, so ultimately Si contains only what was assigned to it in the last pass (i=251).

    Your second approach makes more sense (if you are trying to construct 252 different piecewise linear functions, each with two segments, each using a different argument). However, what your arguments say is that:

    a) the function equals 0 (last argument) when the variable equals 5000/num1 (next to last argument);
    b) the function has slope -num1 (b[0]) between 5000/num1 and 5000/num1 (a[0]); and
    c) the function has slope 0 (b[1]) when the argument exceeds 5000/num1 (a[1]).

    So that function is identically zero.

    Paul

    Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. (Goethe)
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: A Problem About Piecewise Linear Function(Cplex+Java)

    Posted 04/01/12 09:29 PM

    Originally posted by: QQDQ_Lilian_Cai


    Thank you for your attention and patience.
    We are new users of CPLEX and hava been stucking in this problem for a long time. So your reply really helps. Thank you again.

    Just as you said, We are trying to construct 252 different piecewise linear functions, each with two segments, each using a different argument. So maybe we can revise and use the second approach(writing Si in an array). However, we still have difficulty in solving the problem (function is identically zero).

    a) and c) are what we want while b) is not. We don't know why b) is b).

    Would you please give us some advice about how to revise it or should we just use other approaches to write Si?

    The mathematical formula abount Si is enclosed.
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: A Problem About Piecewise Linear Function(Cplex+Java)

    Posted 04/03/12 10:31 AM

    Originally posted by: SystemAdmin


    Apologies: I misread the CPLEX documentation. I thought the "anchor point" had to be the leftmost point in the graph of the pw-linear function, but that is not the case. It can be any point on the graph.

    So, as far as I can see, your second approach should work, provided that you add Si[i] to your model correctly (and provided that containsInN(i) is true for at least one value of i). You did not show where Si[i] is being used in the code. The cplex.piecewiseLinear() function creates the pw-linear function but does not add it to your model.

    Paul

    Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. (Goethe)
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: A Problem About Piecewise Linear Function(Cplex+Java)

    Posted 04/05/12 10:30 PM

    Originally posted by: QQDQ_Lilian_Cai


    Hi, Paul

    Thank you for your last reply. But we still don't konw why the second approach doesn't work.

    The code mentioned above about Si is just a very small part of the entire program. When we use the first approach, the entire program runs normally and we can get the results(even if they are wrong). However, when we use the second approach, the entire program just cannot run, with Zi ranging from i=0 to i=251 all equal to 0.

    Just as you said, the second approach should work. But it just doesn't. We have checked the entire program for many times and still cannot figure it out. We would really appreciate it if you could help us with this problem.

    model.lp, model.sav and model.java are enclosed.
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: A Problem About Piecewise Linear Function(Cplex+Java)

    Posted 04/07/12 06:27 PM

    Originally posted by: SystemAdmin


    I cannot run your Java code here, because I lack something you imported (which I suspect provides the data). The syntax of the piecewise linear function looks correct to me. Your model is infeasible, and I suspect the issue is not an error expressing the piecewise linear function but an error in the model. I suggest you try the following. First, assign names to all your variables and constraints, so that when you export to an LP file you can related each entry in the file to a specific part of your model. Second, export the model to a SAV file (more accurate than LP format). Third, read the SAV file into the CPLEX interactive optimizer, try to solve it, and if (as I expect) CPLEX says it is infeasible, use the conflict refiner to identify the source of the infeasibility. (You may be able to do this from your Java code directly, without exporting, but I like to use the interactive optimizer because it gives me more flexibility to try things without having to do a lot of code tweaks.)

    Paul

    Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. (Goethe)
    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: A Problem About Piecewise Linear Function(Cplex+Java)

    Posted 04/15/12 12:34 PM

    Originally posted by: QQDQ_Lilian_Cai


    Hi, Paul

    Thanks for your reply!

    We tried your suggesting method about using CPLEX interactive optimizer and conflict refiner. Just as you have expected, CPLEX says the model is infeasible. So we used the conflict refiner to identify the source of the infeasibility. There is no difficulty in using the conflict refiner, but the analysis result of it is so complicated that we just cannot figure out the source of the infeasibility.

    Besides, I am confused about the infeasibility. When the first approach is used, the entire program works. Since we don't change anything except Si and it seems that the second approach should work, I really don't know how the problem arises.

    I would really appreciate it if you could answer my question again and help us find out the source of the infeasibility.

    The entire program including the data is enclosed.
    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: A Problem About Piecewise Linear Function(Cplex+Java)

    Posted 04/22/12 11:07 AM

    Originally posted by: SystemAdmin


    The conflict refiner will identify a collection of constraints (and, possibly, variable bounds) that are collectively inconsistent, regardless of the rest of the model. This many not be the only collection of inconsistent constraints, but it is a starting point. I suggest that you identify a solution that should be feasible. It does not have to be a good solution in terms of the objective function, just feasible. Then substitute it into each of the constraints identified by the conflict refiner until you find one that is not satisfied by that solution. Assuming you were correct in picking a feasible solution, that constraint is in error. Fix it, run the conflict refiner again, and repeat.

    Paul

    Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. (Goethe)
    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: A Problem About Piecewise Linear Function(Cplex+Java)

    Posted 04/05/12 10:32 PM

    Originally posted by: QQDQ_Lilian_Cai


    model.lp is enclosed.
    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: A Problem About Piecewise Linear Function(Cplex+Java)

    Posted 04/05/12 10:33 PM

    Originally posted by: QQDQ_Lilian_Cai


    model. sav is enclosed.
    #CPLEXOptimizers
    #DecisionOptimization