Topic
  • 11 replies
  • Latest Post - ‏2012-11-28T04:14:43Z by SystemAdmin
SystemAdmin
SystemAdmin
7929 Posts

Pinned topic Non linear constraints in linear objective function

‏2012-11-16T18:05:50Z |
Hi,

Anyone know how to handle the non linear constraints in linear objective function?
When I wrote the code as

"cplex = Cplex('test');
cplex.Model.sense = 'minimize';

obj = 1;1;
lb = 0;0;
ub = inf;inf;
ctype = ;

cplex.addCols(obj, [], lb, ub, ctype);

supply=[1,0;
0,1];
cplex.addRows(2, supply, 2);

cplex.solve();"

It showed the error message as
"??? Error using ==> Cplex.addRows at 1114
Cplex.addRows Error: incompatible matrix sizes."

Thanks for your help.
Updated on 2012-11-28T04:14:43Z at 2012-11-28T04:14:43Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Non linear constraints in linear objective function

    ‏2012-11-20T08:44:48Z  
    Can you please wrap your code into '{code}' tags? Otherwise it is hard to read.
    If I read your code correctly then the problem is that you call addRows() with a two-row matrix but the lhs and rhs vectors are only singletons. If the 'supply' matrix has two rows then each of those vectors needs to have two elements as well.
    Please take a look at the third example for addRows() in the reference documentation. This illustrates how to add multiple rows in one shot.
    Finally, I don't see anything non-linear in your model, neither in the constraints nor in the objective function. So I fail to see how your problem relates to the subject of this thread :-(
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Non linear constraints in linear objective function

    ‏2012-11-20T18:08:19Z  
    Can you please wrap your code into '{code}' tags? Otherwise it is hard to read.
    If I read your code correctly then the problem is that you call addRows() with a two-row matrix but the lhs and rhs vectors are only singletons. If the 'supply' matrix has two rows then each of those vectors needs to have two elements as well.
    Please take a look at the third example for addRows() in the reference documentation. This illustrates how to add multiple rows in one shot.
    Finally, I don't see anything non-linear in your model, neither in the constraints nor in the objective function. So I fail to see how your problem relates to the subject of this thread :-(
    Thanks for you reply. Sorry for the confusion.

    Assume there are two variables: X1 and X2,
    If there is a constraint as "X1^2 + X2^2 + X1*X2<=2"
    how can I express this non-linear constraint using addRows() function.

    If there is two lhs and rhs for each vector, it will look like the linear constraints.
    Thanks for your help
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Non linear constraints in linear objective function

    ‏2012-11-21T07:19:37Z  
    Thanks for you reply. Sorry for the confusion.

    Assume there are two variables: X1 and X2,
    If there is a constraint as "X1^2 + X2^2 + X1*X2<=2"
    how can I express this non-linear constraint using addRows() function.

    If there is two lhs and rhs for each vector, it will look like the linear constraints.
    Thanks for your help
    These kind of constraints are known as "quadratic constraints" and must be added using method addQCs() instead of addRows(). Please refer to the reference documentation for addQCs() for more details.
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Non linear constraints in linear objective function

    ‏2012-11-21T14:39:08Z  
    These kind of constraints are known as "quadratic constraints" and must be added using method addQCs() instead of addRows(). Please refer to the reference documentation for addQCs() for more details.
    Thank you very much. I do appreciate it.
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Non linear constraints in linear objective function

    ‏2012-11-21T14:56:38Z  
    These kind of constraints are known as "quadratic constraints" and must be added using method addQCs() instead of addRows(). Please refer to the reference documentation for addQCs() for more details.
    Hi Daniel,

    Could you show me a case of how to use "addIndicators"?

    for example:
    '(cplex.addIndicators(5, 1, 1 2 3 4 5 6', 'E', 100, {'indc3'});)'

    Does it mean when X5 (binary variable) equal to 1, X1+X2+X3+X4+X5+X6==100?

    Thanks again.

    Aaron
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Non linear constraints in linear objective function

    ‏2012-11-22T07:45:55Z  
    Hi Daniel,

    Could you show me a case of how to use "addIndicators"?

    for example:
    '(cplex.addIndicators(5, 1, 1 2 3 4 5 6', 'E', 100, {'indc3'});)'

    Does it mean when X5 (binary variable) equal to 1, X1+X2+X3+X4+X5+X6==100?

    Thanks again.

    Aaron
    Again, please wrap code into '
    ' tags, otherwise it is hard to read (for example because the Forum software swallows square brackets). I guess your code is
    

    cplex.addIndicators(5, 1, 1 2 3 4 5 6', 'E', 100, {'indc3'});
    {code}
    This constraints says "If x(5) is 0 then x(1)+...+x(6) must be 100". I think you misinterpreted the second argument (where you pass '1'): This argument says whether the indicator variable (first argument) should be complemented or not. If the indicator variable is complicated then the constraint is "if indicator variable is 0 then ...", otherwise it is "if indicator variable is 1 then ...".
    You can find more details about the arguments in the reference documentation for function addIndicators().
    Please also look at the examples etsp.m and foodmanu.m in the CPLEX distribution. They use indicator constraints and have comments that describe what constraints are created.
    Updated on 2014-03-24T22:45:45Z at 2014-03-24T22:45:45Z by iron-man
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Non linear constraints in linear objective function

    ‏2012-11-22T11:19:40Z  
    Hi Daniel,

    Could you show me a case of how to use "addIndicators"?

    for example:
    '(cplex.addIndicators(5, 1, 1 2 3 4 5 6', 'E', 100, {'indc3'});)'

    Does it mean when X5 (binary variable) equal to 1, X1+X2+X3+X4+X5+X6==100?

    Thanks again.

    Aaron
    Sorry, this time I messed up the tags. Reposting ...

    I guess your code is
    cplex.addIndicators(5, 1, 1 2 3 4 5 6', 'E', 100, {'indc3'});
    

    This constraints says "If x(5) is 0 then x(1)+...+x(6) must be 100". I think you misinterpreted the second argument (where you pass '1'): This argument says whether the indicator variable (first argument) should be complemented or not. If the indicator variable is complicated then the constraint is "if indicator variable is 0 then ...", otherwise it is "if indicator variable is 1 then ...".
    You can find more details about the arguments in the reference documentation for function addIndicators().
    Please also look at the examples etsp.m and foodmanu.m in the CPLEX distribution. They use indicator constraints and have comments that describe what constraints are created.

    Again, please wrap code into '{code}' tags, otherwise it is hard to read (for example because the Forum software swallows square brackets).
    Updated on 2014-03-24T22:45:41Z at 2014-03-24T22:45:41Z by iron-man
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Non linear constraints in linear objective function

    ‏2012-11-22T21:42:39Z  
    Sorry, this time I messed up the tags. Reposting ...

    I guess your code is
    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">cplex.addIndicators(5, 1, 1 2 3 4 5 6', 'E', 100, {'indc3'}); </pre>
    This constraints says "If x(5) is 0 then x(1)+...+x(6) must be 100". I think you misinterpreted the second argument (where you pass '1'): This argument says whether the indicator variable (first argument) should be complemented or not. If the indicator variable is complicated then the constraint is "if indicator variable is 0 then ...", otherwise it is "if indicator variable is 1 then ...".
    You can find more details about the arguments in the reference documentation for function addIndicators().
    Please also look at the examples etsp.m and foodmanu.m in the CPLEX distribution. They use indicator constraints and have comments that describe what constraints are created.

    Again, please wrap code into '{code}' tags, otherwise it is hard to read (for example because the Forum software swallows square brackets).
    Thank you very much. Daniel.
    I am make sure to wrap the code into '{code}' tags next time.
    I am new to this forum, sorry about that.
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Non linear constraints in linear objective function

    ‏2012-11-27T03:48:25Z  
    Sorry, this time I messed up the tags. Reposting ...

    I guess your code is
    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">cplex.addIndicators(5, 1, 1 2 3 4 5 6', 'E', 100, {'indc3'}); </pre>
    This constraints says "If x(5) is 0 then x(1)+...+x(6) must be 100". I think you misinterpreted the second argument (where you pass '1'): This argument says whether the indicator variable (first argument) should be complemented or not. If the indicator variable is complicated then the constraint is "if indicator variable is 0 then ...", otherwise it is "if indicator variable is 1 then ...".
    You can find more details about the arguments in the reference documentation for function addIndicators().
    Please also look at the examples etsp.m and foodmanu.m in the CPLEX distribution. They use indicator constraints and have comments that describe what constraints are created.

    Again, please wrap code into '{code}' tags, otherwise it is hard to read (for example because the Forum software swallows square brackets).
    Hi Danniel

    I wrote a code as

    '{for i=1:32
    vv=zeros(69,1);
    vv(i+32)=1;
    cplex.addIndicators(i,1,vv,'G',0);
    end}'

    I tried to express that when variable i equal to '0', the equation vv is equal to '0'.
    I try the simple example, it works.
    But when I did in my research code, it appeared infeasible. Is there something I need pay attention to when I used cplex.addIndicators?
    Thanks.
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Non linear constraints in linear objective function

    ‏2012-11-27T07:26:05Z  
    Hi Danniel

    I wrote a code as

    '{for i=1:32
    vv=zeros(69,1);
    vv(i+32)=1;
    cplex.addIndicators(i,1,vv,'G',0);
    end}'

    I tried to express that when variable i equal to '0', the equation vv is equal to '0'.
    I try the simple example, it works.
    But when I did in my research code, it appeared infeasible. Is there something I need pay attention to when I used cplex.addIndicators?
    Thanks.
    The code
    cplex.addIndicators(i,1,vv,'G',0);
    

    creates a 'greather or equal' constraint, not an equality constraint (like you said you want to create) but that should not affect feasibility.
    There is nothing special to observe when creating indicator constraints, so I guess the problem is somewhere within your model. To analyze infeasibility of a model the conflict refiner (see function refineConflict() here and the Infeasibility and unboundedness section in the user manual may be helpful.
    Updated on 2014-03-24T22:45:14Z at 2014-03-24T22:45:14Z by iron-man
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Non linear constraints in linear objective function

    ‏2012-11-28T04:14:43Z  
    The code
    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">cplex.addIndicators(i,1,vv,'G',0); </pre>
    creates a 'greather or equal' constraint, not an equality constraint (like you said you want to create) but that should not affect feasibility.
    There is nothing special to observe when creating indicator constraints, so I guess the problem is somewhere within your model. To analyze infeasibility of a model the conflict refiner (see function refineConflict() here and the Infeasibility and unboundedness section in the user manual may be helpful.
    Thank you very much. Daniel.

    I finally finish my first CPLEX research project. I used addIndicators the other way around.
    It can't be done without ur help. Thanks again.