Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

A question of variable when using Cplex.net

  • 1.  A question of variable when using Cplex.net

    Posted 11/14/11 08:36 AM

    Originally posted by: SystemAdmin


    Hi,everyone.my question is like this:
    if (decide[i] == 0 )
    {
    cplex.AddEq(cplex.Sum(decidei+1, decidei+2), 2);
    }

    decide is a Variable.I should make a judge of the Variable using "if'',before write the Constraint.But it is always wrong.who can help me ?thank you very much.
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: A question of variable when using Cplex.net

    Posted 11/14/11 07:13 PM

    Originally posted by: SystemAdmin


    Assuming that 'decide[]' is a 'decision' variable, you could do something like the following:

    cplex.Add(cplex.IfThen(cplex.AddEq(decide[i], 0), cplex.AddEq(cplex.Sum(decide[i+1], decide[i+2]),2)));
    


    This would add a constraint stating that if (decide[i]==0) => (decide i+1+decide i+2==2)

    Hope this helps.
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: A question of variable when using Cplex.net

    Posted 12/06/11 02:41 AM

    Originally posted by: SystemAdmin


    Thank you for your help!
    I tried that as you said,but i find there is no the method of "IfThen" in cplex (.net)

    I also try it as following,but the Constraint does not make any effect on the object.Can you tell me why??

    // Constraint3:
    for (int i = 0; i < num; i++)
    {
    for (int j = 0; j < t-3; j++)
    {

    cplex.Add(cplex.Ifthen(cplex.AddEq(decide[i][j], 0), cplex.AddEq(cplex.Sum(decide[i]j+1, decide[i]j+2), 2)));

    if (decide[i][j].Equals(0) && decide[i][j].Equals(1)){
    cplex.i(cplex.Sum(decide[i]j + 2, decide[i]j + 3),2); };
    if (decide[i][j].Equals(1) && decide[i][j].Equals(0)){
    cplex.AddEq(cplex.Sum(decide[i]j + 2, decide[i]j + 3), 0); };
    }
    }
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: A question of variable when using Cplex.net

    Posted 12/06/11 06:15 AM

    Originally posted by: SystemAdmin


    There definitely is an IfThen method, see here.
    Concerning your constraint: Are you saying that the solution computed by CPLEX violates the constraint?
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: A question of variable when using Cplex.net

    Posted 12/06/11 07:50 AM

    Originally posted by: SystemAdmin


    Thank you for your help!
    I can not find the method of "IfThen",because my cplex edition is just 9.0.

    I means that the object's value will not change whether the Constraint exists or not! In other words,the Constraint will not be executed.
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: A question of variable when using Cplex.net

    Posted 12/06/11 08:01 AM

    Originally posted by: SystemAdmin


    Any chance you can upgrade to a more recent version of CPLEX?
    Note that the more recent versions of CPLEX are free for academic use.

    Considering your constraint
    if (decide[i][j].Equals(0) && decide[i][j].Equals(1)) {
       cplex.i(cplex.Sum(decide[i][j + 2], decide[i][j + 3]),2);
    }
    

    First of all I don't see how something like
    if (decide[i][j].Equals(0) && decide[i][j].Equals(1))
    

    can ever be true. How can an object we equal to two different objects simultaneously? Did you check that this condition was ever true? I guess it was not and therefore the constraint was never added.
    Moreover, 'decide' is an array of INumVar instances, correct? What exactly are you trying to model here? In the if-condition, are you trying to refer to the value of the variable and compare that against 0 or 1? This will not work that way. Could you explain in plain text what constraint you are trying to model by the code you posted?
    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: A question of variable when using Cplex.net

    Posted 12/06/11 09:24 AM

    Originally posted by: SystemAdmin


    I have checked that this condition can be true.

    you said that:"In the if-condition, are you trying to refer to the value of the variable and compare that against 0 or 1?",Perhaps it is ture.
    can you help me check the codes in the plus txt file ??

    Thank you for your patience,i really appreciate it !
    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: A question of variable when using Cplex.net

    Posted 12/06/11 04:25 PM

    Originally posted by: SystemAdmin


    Thank you for sending in your code, it greatly helps clarify things.

    > you said that:"In the if-condition, are you trying to refer to the
    > value of the variable and compare that against 0 or 1?",Perhaps it is
    > ture. can you help me check the codes in the plus txt file ??

    Daniel's comment of how an object can be equal to two things simultaneously, still holds true. I think the confusion is a result of typo, wherein you said that the if condition should be :
    
    
    
    if (decide[i][j].Equals(0) && decide[i][j].Equals(1))
    


    when you really meant it to be:
    
    
    
    if (decide[i][j].Equals(0) & decide[i][(j + 1)].Equals(1))
    


    I went through your code and have made a few changes. Please find the modified code attached. The if condition looks like the following:

    
    cplex.Add(cplex.IfThen(cplex.And(cplex.Eq(decide[i][j], 0), cplex.Eq(decide[i][j + 1], 1)), cplex.Eq(cplex.Sum(decide[i][j + 2], decide[i][j + 3]), 2)));
    


    You correctly pointed out that the 'ifthen' logical constraint is not available with CPLEX 9.0; CPLEX 10.0 was the first version that supports this type of constraint. Thus, for the attached code to work you will need to upgrade your CPLEX version to CPLEX 10.0 or later versions. You could be entitled to get the latest version of CPLEX (12.3) via the academic initiative program. Details about registering with this program can be found in the following technote:

    http://www-01.ibm.com/support/docview.wss?uid=swg21419058

    Hope this helps.
    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: A question of variable when using Cplex.net

    Posted 12/10/11 12:50 AM

    Originally posted by: SystemAdmin


    Thank you.I get it.
    May I ask you another question?
    if vas[i] is variable ,how can i add a constraint of vas[i] like this:"vas[i]=10 or vas[i]=20";
    It means that vas[i] equals to 10 or 20 in the optimize process? Which method can i use in cplex .net??
    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: A question of variable when using Cplex.net

    Posted 12/10/11 04:16 AM

    Originally posted by: SystemAdmin


    i have a new question
    #CPLEXOptimizers
    #DecisionOptimization


  • 11.  Re: A question of variable when using Cplex.net

    Posted 12/11/11 02:40 PM

    Originally posted by: SystemAdmin


    > if vas[i] is variable ,how can i add a constraint of vas[i] like
    > this:"vas[i]=10 or vas[i]=20";

    You could try something like the following for this sort of an 'Or' constraint:

    cplex.Add(cplex.Or(cplex.Eq(vas[i], 10), cplex.Eq(vas[i], 20)));
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 12.  Re: A question of variable when using Cplex.net

    Posted 12/12/11 03:50 AM

    Originally posted by: SystemAdmin


    Another solution would be to create a new binary variable (say 'x') and add a constraint that requires vas[i] = 10 + 10x. This will also force vas[i] to either 10 or 20.
    #CPLEXOptimizers
    #DecisionOptimization


  • 13.  Re: A question of variable when using Cplex.net

    Posted 01/02/12 03:14 AM

    Originally posted by: SystemAdmin


    Thank you.I get it.

    Another question is that:
    // Constraint2:
    for (int i = 0; i < num; i++)
    {
    for (int j = 0; j < t - 3; j++)
    {
    cplex.Add(cplex.IfThen(cplex.And(cplex.Eq(decide[i][j], 0), cplex.Eq(decide[i]j + 1, 1)), cplex.Eq(cplex.Sum(decide[i]j + 2, decide[i]j + 3), 2)));

    }
    }
    I want to change it like this:

    // Constraint2:
    for (int i = 0; i < num; i++)
    {
    for (int j = 0; j < t - dur_time; j++)
    {
    cplex.Add(cplex.IfThen(cplex.And(cplex.Eq(decide[i][j], 0), cplex.Eq(decide[i]j + 1, 1)), cplex.Eq(cplex.Sum(decide[i]j + 1,decide[i]j + 2,.....,decide[i]j + dur_time), dur_time)));

    }
    }

    it means that" decide[i]j + 1+decide[i]j + 2+.....+decide[i]j + dur_time=dur_time"
    how can i make it ??
    #CPLEXOptimizers
    #DecisionOptimization


  • 14.  Re: A question of variable when using Cplex.net

    Posted 01/02/12 03:46 AM

    Originally posted by: SystemAdmin


    Please wrap any source code listings into '{ code }' (without the blanks) tags. Without that your post is almost unreadable.
    Is this what you are trying to do:
    // Create constraint
    // decide[i][j+1] + ... + decide[i][j + dur_time] == dur_time
    INumExpr sum = cplex.NumExpr();
    for (int k = 1; k <= dur_time; ++k)
       sum = cplex.Sum(sum, decide[i][j + k];
    // sum is now decide[i][j+1] + ... + decide[i][j + dur_time]
    cplex.AddEq(sum, dur_time);
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 15.  Re: A question of variable when using Cplex.net

    Posted 01/03/12 05:29 AM

    Originally posted by: SystemAdmin


    thank ,i get it
    #CPLEXOptimizers
    #DecisionOptimization


  • 16.  Re: A question of variable when using Cplex.net

    Posted 01/02/12 03:25 AM

    Originally posted by: SystemAdmin


    Thank you.I get it.

    INumVar][ decide = new INumVarnum][; //N*M
    for (int i = 0; i < num; i++)
    {

    decide[i] = new INumVar[t];
    decide[i] = cplex.NumVarArray(t, 0.0, 1.0, NumVarType.Bool,name);
    };

    how can i print the result of decide[i][j] using "System.Console.WriteLine"?
    #CPLEXOptimizers
    #DecisionOptimization


  • 17.  Re: A question of variable when using Cplex.net

    Posted 01/02/12 03:34 AM

    Originally posted by: SystemAdmin


    Use
    cplex.GetValue(decide[i][j])
    

    to retrieve the value for the variable. The returned value is a double that can be output using WriteLine like any other double:
    System.Console.WriteLine(cplex.GetValue(decide[i][j]));
    

    Take a look at the .NET examples in the CPLEX distribution. For example, FoodManufact.cs has code that even shows how to format the value:
    System.Console.Write("{0,8:F2} ", cplex.GetValue(buy[i][p]));
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 18.  Re: A question of variable when using Cplex.net

    Posted 01/02/12 05:27 AM

    Originally posted by: SystemAdmin


    Thank you for your help!
    I just try it as you said,but i can not get the print result i need.

    I have sent my code to you,and you can see it in detail.

    qestion1:
    the object's value will not change whether the Constraint2 exists or not! In other words,the Constraint will not be executed.If i delete the Constraint2,the object is the same as before .But i am sure it will change when deleting the Constraint2

    qestion2:

    i can not get the print result i need,when using System.Console.Write("{0,8:F2} ", cplex.GetValue(buy[i][p]));
    #CPLEXOptimizers
    #DecisionOptimization


  • 19.  Re: A question of variable when using Cplex.net

    Posted 01/03/12 01:11 PM

    Originally posted by: SystemAdmin


    Could you explain in "prose" what your Constraint2 is supposed to do?
    And what do you mean "you cannot get the print result you need"? What is the result you need and what is printed instead?
    #CPLEXOptimizers
    #DecisionOptimization