Decision Optimization

Decision Optimization

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

 View Only
  • 1.  GetValue() function returning wrong values

    Posted Sun June 23, 2013 07:13 PM

    Originally posted by: aysenur


    Dear All,

    I work with .NET callable library of Cplex and I recently encountered a strange problem using the Cplex.GetValue() function. This is normally what I use to recover the values of my variables after a Solve() command. However, this time the values it would return didn't seem correct. So, I did a crosscheck with Cplex.WriteSolution(). I discovered that the output solution file given by WriteSolution() contains the right values for my variables, yet GetValue() will return values different than those. Has anyone ever encountered such a problem? Any ideas, comments on what would cause this problem?

    Thank you in advance for the help.


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: GetValue() function returning wrong values

    Posted Mon June 24, 2013 02:34 AM

    What does "incorrect" mean here? Is it possible that the values you observe just contain some small roundoff error? WriteSolution() writes a text file and will implicitly round/truncate floating point values to some decimal digits.

    Could you please give an example of incorrect values and what is written for them in the solution file?


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: GetValue() function returning wrong values

    Posted Mon June 24, 2013 09:13 AM

    Originally posted by: aysenur


    Hi,

    Thank you for the reply. "Incorrect" here means way-off. For instance I have these three integer variables z1, z2 and z3 that I know take the values z1=1, z2=1 and z3=1 in the optimal solution and these are the values in the WriteSolution() file. However, when I inquire using GetValue(), I will have z1=3, z2=3 and z3=6.

    On a side note I use these variables in several other models that I solve as subproblems before my main model. I then use the EndModel() on these small models before I solve and report my main model. Do you think there might still be some issues associated with that? Should I have used End() on the subproblem models instead?

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: GetValue() function returning wrong values

    Posted Mon June 24, 2013 10:52 AM

    From what you describe I cannot see an error in your code but it might be easier to analyze if you could provide some code snippets or even the full code here.

    Do you invoke the GetValue() function at the exact same place as the WriteSolution() function? If not, what happens if you do so?

    Can you please double check that you don't have multiple variables with the same name (make sure names "z1", "z2", "z3" appear only once for each solution in the solution file).


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: GetValue() function returning wrong values

    Posted Mon June 24, 2013 03:22 PM

    Originally posted by: aysenur


    1. Nothing changes when I invoke the WriteSolution() and GetValue() at the same place

    2. I checked once again that I don't have multiple variables with the same name.

    That being noted, before I post parts of my code that I think might be causing the problem let me also give you this piece of information.

    I went ahead and tried commenting out the part of my code that deals with submodels and if I do that I get the same values from WriteSolution() and GetValue(). I think there is something going in the memory that even after the models are destroyed their effects on variables and their values continue.

    This also relates to my other post about Conversion(). Any ideas on trying to identify the reason and solving the issue?


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: GetValue() function returning wrong values

    Posted Tue June 25, 2013 10:16 AM

    If I understand correctly you create INumVar instances via cpx1.NumVar() and use these INumVar instances in two different CPLEX instances cpx1 and cpx2? If you do that then this may indeed be the cause of your problems. As a rule of thumb you should use INumVar instances only in the CPLEX class that created them. Is it feasible to change your code so that it conforms to this rule?

    In case you are doing something else and I got you wrong: could you please explain in more detail how you create the INumVar instances, how you use them in the submodels and in the main model? A code snippet showing a rough outline of your code might be helpful.


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: GetValue() function returning wrong values

    Posted Tue June 25, 2013 10:38 AM

    Originally posted by: aysenur


    Hi,

    That is precisely what I have been doing. I figured a way of solving my submodels outside of my main BuildModel function so that the variables are only used by the Cplex object that created them. I didn't realize using same variables in various Cplex objects could be this troublesome. Everything seems to work as they are supposed to now, including my Conversion() instances.

    As one final question what is a way of removing Conversion() instances from a model so that after I solve the LP relaxation locally I can go on with my MIP formulation if I want to. In the example code that comes with Cplex there is only one instance of Conversion() so you can just remove that one from the model. In my case though I have been using a loop to create and add Conversion() instances (because I have 3-index variables), so I am not sure about how the .Remove() should be invoked.

    Thank you so much for the help once again.


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: GetValue() function returning wrong values

    Posted Tue June 25, 2013 11:20 AM

    You just need to keep a reference for the created IConversion instances so that you can remove then later. I am not a C# expert but the following should give you the idea:

    List<IConversion[]> conversions = new List<IConversion[]>();
    foreach (Pair p in PairsIn) {
       foreach (Node n in STIn.Nodes) {
          IConversion[] c = model.Conversion(ypnvc[PairsIn.IndexOf(p)][STIn.Nodes.IndexOf(n)]);
          model.Add(c);       // Add conversions to model
          conversions.Add(c); // Keep a reference so that we can delete them later
       }
    }
    // Solve the relaxed model here
    ...
    // Now delete the conversions
    foreach (IConversion[] c in conversions) {
       model.Delete(c);
    }

    Did I understand correctly that the problems with conversions described here are now resolved as well and that question can be considered as "answered"?


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: GetValue() function returning wrong values

    Posted Tue June 25, 2013 02:40 PM

    Originally posted by: aysenur


    That idea should work, I will see the implementation details.

    You got it right, both problems I had were caused by side effects of using same variables in different Cplex objetcs, therefore they are solved now. I will mark the posts, thank you again for the help.

     


    #CPLEXOptimizers
    #DecisionOptimization