Carl
Carl

Reputation: 73

Method in a contraint

I've a cplex constraint in the form of a a binary variable multiply for a number >= to another number. The second number is complex to calculate, I think I need a method to compute it, it is possible in cplex write a constraint like this:

k*y[i] > method(parameter1,parameter2)

In the method I need to access to binary variables values. Thanks a lot for replies.

Upvotes: 1

Views: 95

Answers (2)

Alex Fleischer
Alex Fleischer

Reputation: 10062

Let me try this oulipo challenge.

Write an OPL models that works and that contains what you wrote.

Could this help?

float k=1.2;
dvar boolean y[1..1];
int parameter1=1;
int parameter2=2;
dvar boolean x;
dexpr float method[i in 1..10,j in 1..10]=x*(i+j);

subject to
{
forall(i in 1..1)
  k*y[i] >= method[parameter1,parameter2];
}

PS: with your later comments:

float k=1.2;
dvar boolean y[1..1];
int parameter1=1;
int parameter2=2;
dvar boolean x;
float methodresults[i in 1..10,j in 1..10]; //=x*(i+j);
range r=1..10;
execute
{
function method(i,j)
{
return i+j;
}

for(var i in r) for (var j in r) methodresults[i][j]=method(i,j);
}


subject to
{
forall(i in 1..1)
  k*y[i] >= x*methodresults[parameter1,parameter2];
}

Upvotes: 1

Xavier Nodet
Xavier Nodet

Reputation: 5105

If you are using a script in a .mod file, then you can define a function whithin an execute block [1]. These blocks define pre-processing or post-processing instructions written in ILOG Script [2]. Here's a trivial example from the documentation at https://www.ibm.com/support/knowledgecenter/SSSA5P_12.9.0/ilog.odms.ide.help/OPL_Studio/opllangref/topics/opl_langref_script_struct_statements_function.html.

execute {
   function add(a, b) {
       return a+b
   }
   writeln(add(1,2));
}

[1] https://www.ibm.com/support/knowledgecenter/SSSA5P_12.9.0/ilog.odms.ide.help/OPL_Studio/opllanguser/topics/opl_languser_script_intro_presynt.html

[2] https://www.ibm.com/support/knowledgecenter/SSSA5P_12.9.0/ilog.odms.ide.help/OPL_Studio/opllanguser/topics/opl_languser_script.html

Upvotes: 0

Related Questions