Re: Adding if/then/else statement to GMPL

Domingo Alvarez Duarte <[email protected]> Tue, 25 Aug 2020 20:17:06 +0200
Newsgroups gmane.comp.gnu.glpk
Message-ID <[email protected]>
Hello Heinrich !

Here is better example from examples/shikaku.mod:

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.
>
> Instead of
>
> 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:
>
> param p,symbolic := "dad";
> 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