help-glpk
[Top][All Lists]

## Re: Adding if/then/else statement to GMPL

 From: Heinrich Schuchardt Subject: Re: Adding if/then/else statement to GMPL Date: Tue, 25 Aug 2020 21:46:29 +0200 User-agent: K-9 Mail for Android

```Am 25. August 2020 20:17:06 MESZ schrieb Domingo Alvarez Duarte
>Hello Heinrich !
>
>Here is better example from examples/shikaku.mod:
>

Just another example where the existing if-then-else expression can be used to
shorten the code.

Anyway comparing a binary to =1 is not a good idea in GLPK. You should use > .5.

Best regards

Heinrich

>With if/then/else:
>
>=====
>
>/* Output solution graphically */
>printf "\nSolution:\n";
>for { row in rows1 } {
>     for { col in cols1 } {
>         if (sum{(i,j,k,l,m,n) in B:
>                 col >= l and col <= n and (row = k or row = m) and
>                 x[i,j,k,l,m,n] = 1} 1) > 0 then
>         {
>             if (sum{(i,j,k,l,m,n) in B:
>                 row >= k and row <= m and (col = l or col = n) and
>                 x[i,j,k,l,m,n] = 1} 1) > 0 then printf "+";
>             else printf "-";
>         }
>         else
>         {
>             if (sum{(i,j,k,l,m,n) in B:
>                 row >= k and row <= m and (col = l or col = n) and
>                 x[i,j,k,l,m,n] = 1} 1) > 0 then printf "|";
>             else printf " ";
>         }
>
>         if (sum{(i,j,k,l,m,n) in B:
>             col >= l and col < n and (row = k or row = m) and
>             x[i,j,k,l,m,n] = 1} 1) > 0 then printf "---";
>         else printf "   ";
>     }
>     printf "\n";
>
>     for { col in cols: (sum{ s in rows: s = row } 1) = 1 } {
>         if (sum{(i,j,k,l,m,n) in B:
>             row >= k and row < m and (col = l or col = n) and
>             x[i,j,k,l,m,n] = 1} 1) > 0 then printf "|";
>         else printf " ";
>        if (sum{ (i,j) in V: i = row and j = col} 1) > 0 then printf "
>%2d", givens[row,col];
>         else printf "  .";
>     }
>     if (sum{ r in rows: r = row } 1) = 1 then printf "|\n";
>}
>
>=====
>
>Original:
>
>=====
>
>/* Output solution graphically */
>printf "\nSolution:\n";
>for { row in rows1 } {
>     for { col in cols1 } {
>         printf{0..0: card({(i,j,k,l,m,n) in B:
>                 col >= l and col <= n and (row = k or row = m) and
>                 x[i,j,k,l,m,n] = 1}) > 0 and
>             card({(i,j,k,l,m,n) in B:
>                 row >= k and row <= m and (col = l or col = n) and
>                 x[i,j,k,l,m,n] = 1}) > 0} "+";
>         printf{0..0: card({(i,j,k,l,m,n) in B:
>                 col >= l and col <= n and (row = k or row = m) and
>                 x[i,j,k,l,m,n] = 1}) = 0 and
>             card({(i,j,k,l,m,n) in B:
>                 row >= k and row <= m and (col = l or col = n) and
>                 x[i,j,k,l,m,n] = 1}) > 0} "|";
>         printf{0..0: card({(i,j,k,l,m,n) in B:
>                 row >= k and row <= m and (col = l or col = n) and
>                 x[i,j,k,l,m,n] = 1}) = 0 and
>             card({(i,j,k,l,m,n) in B:
>                 col >= l and col <= n and (row = k or row = m) and
>                 x[i,j,k,l,m,n] = 1}) > 0} "-";
>         printf{0..0: card({(i,j,k,l,m,n) in B:
>                 row >= k and row <= m and (col = l or col = n) and
>                 x[i,j,k,l,m,n] = 1}) = 0 and
>             card({(i,j,k,l,m,n) in B:
>                 col >= l and col <= n and (row = k or row = m) and
>                 x[i,j,k,l,m,n] = 1}) = 0} " ";
>
>         printf{0..0: card({(i,j,k,l,m,n) in B:
>             col >= l and col < n and (row = k or row = m) and
>             x[i,j,k,l,m,n] = 1}) > 0} "---";
>         printf{0..0: card({(i,j,k,l,m,n) in B:
>             col >= l and col < n and (row = k or row = m) and
>             x[i,j,k,l,m,n] = 1}) = 0} "   ";
>     }
>     printf "\n";
>
>     for { (col,p) in { cols, 1 }: card({ s in rows: s = row }) = 1 } {
>         printf{0..0: card({(i,j,k,l,m,n) in B:
>             row >= k and row < m and (col = l or col = n) and
>             x[i,j,k,l,m,n] = 1}) > 0} "|";
>         printf{0..0: card({(i,j,k,l,m,n) in B:
>             row >= k and row < m and (col = l or col = n) and
>             x[i,j,k,l,m,n] = 1}) = 0} " ";
>         printf{0..0: card({ (i,j) in V: i = row and j = col}) > 0} "
>%2d", givens[row,col];
>        printf{0..0: card({ (i,j) in V: i = row and j = col}) = 0} "
>.";
>     }
>     printf{0..0: card({ r in rows: r = row }) = 1} "|\n";
>}
>
>=====
>
>Cheers !
>On 24/8/20 16:59, Heinrich Schuchardt wrote:
>> On 24.08.20 16:33, Domingo Alvarez Duarte wrote:
>>> Hello Meketon !
>>>
>>> Could you share your view of how it would be expressed (an ideal
>model
>>> sample) ?
>>>
>>> If you want to talk about it, maybe I'll be interested in implement
>it !
>>>
>>> Can you share a collection of models data to be used as base for the
>>> test/implementation ?
>> Dear Domingo,
>>
>> I do not yet understand what was you weren't able to express with the
>> current syntax.
>>
>>
>> if length(p) == 3 then display "true 3"; else display "false 3";
>> if length(p) == 5 then display "true 5"; else display "false 5";
>>
>> you can write:
>>
>> display if length(p) == 3 then "true 3" else "false 3";
>> display if length(p) == 5 then "true 5" else "false 5";
>> solve;
>> end;
>>
>> Best regards
>>
>> Heinrich

```