Topic
• 4 replies
• Latest Post - ‏2014-04-10T11:43:03Z by ChrisBr
appleme
7 Posts

# Pinned topic How to count in a desicion variable?

‏2014-04-08T07:50:39Z | count

Hi,all.

tuple  JUP
{   int index;
string p;
string u;
} ;

{JUP} jup=...;

dvar int+ jupz[jup] in 0..1;

I want to get the count from  jupz where  jup.p="T" and jupz[jup]==1 ,How to realize?I found there is no count function to do this.

Could you help me

Thanks.

• ChrisBr
55 Posts

#### Re: How to count in a desicion variable?

‏2014-04-08T13:08:43Z

Hello,

You could try this:
c == count(all(j in jup : j.p=="T") jupz[j], 1);

I suggest you have a look at the documentation
IDE and OPL > Optimization Programming Language (OPL) > Language Quick Reference > OPL functions > count

I hope this helps,

Chris.

Updated on 2014-04-08T13:18:41Z at 2014-04-08T13:18:41Z by ChrisBr
• appleme
7 Posts

#### Re: How to count in a desicion variable?

‏2014-04-09T02:08:04Z
• ChrisBr
• ‏2014-04-08T13:08:43Z

Hello,

You could try this:
c == count(all(j in jup : j.p=="T") jupz[j], 1);

I suggest you have a look at the documentation
IDE and OPL > Optimization Programming Language (OPL) > Language Quick Reference > OPL functions > count

I hope this helps,

Chris.

Thanks for your help.

I tested,It works well.

• appleme
7 Posts

#### Re: How to count in a desicion variable?

‏2014-04-09T02:16:04Z
• ChrisBr
• ‏2014-04-08T13:08:43Z

Hello,

You could try this:
c == count(all(j in jup : j.p=="T") jupz[j], 1);

I suggest you have a look at the documentation
IDE and OPL > Optimization Programming Language (OPL) > Language Quick Reference > OPL functions > count

I hope this helps,

Chris.

I found a question.

c == count(all(j in jup : j.p=="T") jupz[j], 1);

this line is a constraint,It is not a equation.

could this function "count(all(j in jup : j.p=="T") jupz[j], 1)" set to a int variable or as condition in if ?

I found

if(count(all(j in jup : j.p=="T") jupz[j], 1)<=5) or c=count(all(j in jup : j.p=="T") jupz[j], 1);

it doesn't work.

• ChrisBr
55 Posts

#### Re: How to count in a desicion variable?

‏2014-04-10T11:43:03Z
• appleme
• ‏2014-04-09T02:16:04Z

I found a question.

c == count(all(j in jup : j.p=="T") jupz[j], 1);

this line is a constraint,It is not a equation.

could this function "count(all(j in jup : j.p=="T") jupz[j], 1)" set to a int variable or as condition in if ?

I found

if(count(all(j in jup : j.p=="T") jupz[j], 1)<=5) or c=count(all(j in jup : j.p=="T") jupz[j], 1);

it doesn't work.

Hello,

Since the result of "count(...)" depends on decision variables, it is not possible to set it to an int as long as the values of the concerned decision variables are not known. But this is possible after the resolution when the decision variables are fixed.

You can set it to a "dexpr int" or define a "dvar int" and constrain it to be equal to the result of "count(...)".

To sum up, all the following statements are allowed:

```dexpr int c1 = count(all(j in jup : j.p=="T") jupz[j], 1);
dvar int c2 in 0..card(jup);
subject to {
c2 == count(all(j in jup : j.p=="T") jupz[j], 1);
}
int c3 = count(all(j in jup : j.p=="T") jupz[j], 1);
```

About your second question: it is mentioned in documentation that
Conditions in if-else statements must be ground; that is, they must not contain decision variables.

So you cannot write:

``` if(count(all(j in jup : j.p=="T") jupz[j], 1)<=5) ...
```

But you can use implications of constraints instead, for example:

```((count(all(j in jup : j.p=="T") jupz[j], 1)) <=5) => (...);
```

or conditional expression, for example:

```  k == (((count(all(j in jup : j.p=="T") jupz[j], 1)) <=5) ? 1000 : -1);
```

I hope this clarifies,

Chris.

Updated on 2014-04-11T13:23:30Z at 2014-04-11T13:23:30Z by ChrisBr