Ranajit
Ranajit

Reputation: 49

How to write if condition in CPLEX OPL

I am creating a set called BlockBelow as shown in the following code (this is part of a larger model).

While doing so I get some blocks as empty - the data is such that it is likely to give empty sets for some blocks. However I want to do a check and fill the empty BlockBelow sets as shown below

{blockType} BlockBelow[b1 in PitBlocksType] =
     {b | b in PitBlocksType: b1.i == b.i -1 &&
                        (b1.k  == b.k ) &&
                         (b1.j  == b.j) };





execute{
for(var i in PitBlocksType){

if ( BlockBelow[i] == {} ) BlockBelow[i] = i;

writeln(i, BlockBelow[i]);
  }
} 

The code without the if line gives an output as below in the scripting log, where the empty sets are visible which need to be filled :

<"P74" 2 3 10> {<"P106" 3 3 10>}
 <"P75" 2 3 11> {<"P107" 3 3 11>}
 <"P76" 2 3 12> {}
 <"P77" 2 4 2> {<"P108" 3 4 2>}
 <"P78" 2 4 3> {<"P109" 3 4 3>}
 <"P79" 2 4 4> {<"P110" 3 4 4>}
 <"P80" 2 4 5> {<"P111" 3 4 5>}
 <"P81" 2 4 6> {<"P112" 3 4 6>}
 <"P82" 2 4 7> {<"P113" 3 4 7>}
 <"P83" 2 4 8> {<"P114" 3 4 8>}
 <"P84" 2 4 9> {<"P115" 3 4 9>}
 <"P85" 2 4 10> {<"P116" 3 4 10>}
 <"P86" 2 4 11> {<"P117" 3 4 11>}
 <"P87" 3 2 2> {}
 <"P88" 3 2 3> {}
 <"P89" 3 2 4> {}
 <"P90" 3 2 5> {}
 <"P91" 3 2 6> {}
 <"P92" 3 2 7> {}

I am getting an error for the if statement. Any suggestions on how to overcome this - either using if or any other method to achieve the objective of filling the empty sets with i

----EDIT AFTER ANSWER---------------

I am sorry I forgot to mention that {blockType} is a tuple

tuple blockType {
    key string id;
    int i;
    int j;
    int k;
 };

I tried the card() function, but it does not work for a tuple

Upvotes: 0

Views: 1279

Answers (2)

Alex Fleischer
Alex Fleischer

Reputation: 10059

To test how many elements you have in a set:

{int} a={1};
{int} b={};

int sizea=card(a);
int sizeb=card(b);

execute
{
var sizea2=Opl.card(a);
var sizeb2=Opl.card(b);
var sizea3=a.size;
var sizeb3=b.size;

writeln(sizea,sizea2,sizea3);
writeln(sizeb,sizeb2,sizeb3);
}

which gives

111
000

Upvotes: 1

Daniel Junglas
Daniel Junglas

Reputation: 5930

In scripting, you cannot use {} as a literal for sets to represent an empty set. You can use the card() function to get the cardinality of a set. An empty set will have cardinality 0.

Upvotes: 2

Related Questions