Re: Adding if/then/else statement to GMPL

Domingo Alvarez Duarte <[email protected]> Sun, 30 Aug 2020 11:19:56 +0200
Newsgroups gmane.comp.gnu.glpk
Message-ID <[email protected]>
Hello !

With this commit 
https://github.com/mingodad/GLPK/commit/2d445283a68c96fe64f0d7e5b26d9cd7e5cf6acf 
it now solve all original problems and also is able to solve 
examples/cut-fix-size.mod that shows what new GMPL extensions I've done 
(still as a prof of concept that need more work) and is usable with caution.

The 'let' update propagation is not done properly, but if we preallocate 
parameter/set dimensions we can work by changing the predicate filters 
({j in PATTERNS : j <= nPAT_top}).

Also to overwrite the type of solver to be used the syntax of solve is 
"solve[.lp/mip/ip] [problem name];".

Would be nice if anyone could provide examples of problems that they 
want to be able to express in GMPL to test/improve/extend the 
implementation.

Again any comment/suggestion/help is welcome !

====


problem Cutting_Opt;
# ----------------------------------------

param nPAT integer >= 0, default 0;
param nPAT_top integer >= 0, default 0;
param roll_width;

set PATTERNS := 1..nPAT;
set WIDTHS;

param orders {WIDTHS} > 0;
param nbr {WIDTHS,PATTERNS} integer >= 0;

let nPAT := 2 * card(WIDTHS);
let nPAT_top := 0;
for {i in WIDTHS} {
    let nPAT_top := nPAT_top + 1;
    let nbr[i,nPAT_top] := floor (roll_width/i);
    let {i2 in WIDTHS: i2 <> i} nbr[i2,nPAT_top] := 0;
}
#display nPAT, nPAT_top;
#display nbr;

check {j in PATTERNS : j <= nPAT_top}: sum {i in WIDTHS} i * nbr[i,j] <= 
roll_width;

var Cut {PATTERNS} integer >= 0;

minimize Number: sum {j in PATTERNS : j <= nPAT_top} Cut[j];

subject to Fill {i in WIDTHS}:
    sum {j in PATTERNS : j <= nPAT_top} nbr[i,j] * Cut[j] >= orders[i];


problem Pattern_Gen;
# ----------------------------------------

param price {WIDTHS} default 0;

var Use {WIDTHS} integer >= 0;

minimize Reduced_Cost:
    1 - sum {i in WIDTHS} price[i] * Use[i];

subject to Width_Limit:
    sum {i in WIDTHS} i * Use[i] <= roll_width;

problem Mix : Cut, Reduced_Cost;

problem Pattern_Gen;

if nPAT > 0 then {
     problem Mix;
     #problem Mix2; #problem creation not allowed here
}

display Cutting_Opt, Pattern_Gen, Mix;

repeat {
#for {1..4} {
    solve.lp Cutting_Opt;
    let {i in WIDTHS} price[i] := Fill[i].dual;
    #display price;

    solve/*.mip*/ Pattern_Gen;
    display Reduced_Cost, Use;
    if Reduced_Cost < -0.00001 and nPAT_top <= nPAT then
    {
       let nPAT_top := nPAT_top + 1;
       let {i in WIDTHS} nbr[i,nPAT_top] := Use[i];
       #display nPAT;
    }
    else break;
}

check {j in PATTERNS : j <= nPAT_top}: sum {i in WIDTHS} i * nbr[i,j] <= 
roll_width;

solve/*.mip*/ Cutting_Opt;
display Cut;
#solve Pattern_Gen;

data;

param roll_width := 110;
set WIDTHS :=
      20
      45
      50
      55
      75;
param orders :=
      [20] 48
      [45] 35
      [50] 24
      [55] 10
      [75] 8;
end;

====

Cheers !

On 24/8/20 17:02, Meketon, Marc wrote:
> Since GMPL is a subset of AMPL, I would begin by looking at:
> https://ampl.com/BOOK/CHAPTERS/16-script.pdf
>
> As far as examples, the classic 'cutting stock' problem is a good one.  The AMPL site has two different iterative approaches:
> https://ampl.com/BOOK/EXAMPLES/EXAMPLES2/cut.mod
>
> https://ampl.com/BOOK/EXAMPLES/EXAMPLES2/cut.run
>
> and
>
> https://ampl.com/BOOK/EXAMPLES/EXAMPLES2/cut2.mod
>
> https://ampl.com/BOOK/EXAMPLES/EXAMPLES2/cut2.run
>
> and the data file is at:
> https://ampl.com/BOOK/EXAMPLES/EXAMPLES2/cut.dat
>
>
> -----Original Message-----
> From: Domingo Alvarez Duarte <[email protected]>
> Sent: Monday, August 24, 2020 10:34 AM
> To: Meketon, Marc <[email protected]>; Andrew Makhorin <[email protected]>; [email protected]
> Subject: Re: Adding if/then/else statement to GMPL
>
> 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 ?
>
> Cheers !
>
> On 24/8/20 16:00, Meketon, Marc wrote:
>> I've always felt that GMPL needed if-then-else, for-loops, 'let' statements and the ability to re-solve to be a true modeling language.  And Andrew has always disagreed.
>>
>> Many of the models that I create ultimately are 'iterative' where I need to take the results of one model and use it to setup another model.  To me, that is also modeling.  GMPL doesn't have it.
>>
>> So often, I use GMPL for an initial model - it is a wonderful language, and I find it faster to code than alternatives.  But then when I 'get it right' I have to re-code it in PYOMO or PULP or write directly to an 'lp' file within a Python or C# or other language script.
>>
>> Having the ability to run, adjust variables, add/take away constraints, re-run would be extremely useful, and make GMPL more of a one-stop modeling language.
>>
>> -----Original Message-----
>> From: Help-glpk
>> <[email protected]> On Behalf Of
>> Andrew Makhorin
>> Sent: Sunday, August 23, 2020 2:56 PM
>> To: Domingo Alvarez Duarte <[email protected]>; [email protected]
>> Subject: Re: Adding if/then/else statement to GMPL
>>
>> On Sun, 2020-08-23 at 15:36 +0200, Domingo Alvarez Duarte wrote:
>>> Hello !
>>>
>>> Also I've added the break/continue statements here
>>> https://github.com/mingodad/GLPK/commit/9d70a37b16bd377722eeb3880fcf8
>>> 6
>>> bb3b812118
>>>
>>>
>>> Again any comment/suggestion is welcome !
>>>
>>> Cheers !
>>>
>>>
>>>
>> Please note that GNU MathProg is a *modeling* language; it is not a general-purpose programming language. If you need to produce a non-trivial solution report (since all such-like statements are allowed only on the post-solving stage), it would be more practical to write the solution to a temporary file and then process it with a separate program.
>>
>>
>>
>>
>> ________________________________
>> This e-mail and any attachments may be confidential or legally privileged. If you received this message in error or are not the intended recipient, you should destroy the e-mail message and any attachments or copies, and you are prohibited from retaining, distributing, disclosing or using any information contained herein. Please inform us of the erroneous delivery by return e-mail. Thank you for your cooperation.
> ________________________________
> This e-mail and any attachments may be confidential or legally privileged. If you received this message in error or are not the intended recipient, you should destroy the e-mail message and any attachments or copies, and you are prohibited from retaining, distributing, disclosing or using any information contained herein. Please inform us of the erroneous delivery by return e-mail. Thank you for your cooperation.