Ampl model

338 views
Skip to first unread message

Mifthahul Janna Rosyid

unread,
Dec 21, 2023, 1:03:02 PM12/21/23
to AMPL Modeling Language
Hi, I'm having trouble writing these model in ampl. Can you help me in writing it in the ampl model and can you give me examples to write the parameters in the ampl data??
The food Food groups are divided into four groups: vegetables, fruits, side dishes, and food staples. 

Your help means a lot, thank you 
Screenshot 2023-12-21 135520.png
Screenshot 2023-12-21 135503.png

Ijtihad Emon

unread,
Dec 22, 2023, 3:25:27 PM12/22/23
to am...@googlegroups.com
Hi, Sure i can help. Can you send me the doc file instade of those screenshots 

Sent from Outlook for iOS

From: am...@googlegroups.com <am...@googlegroups.com> on behalf of Mifthahul Janna Rosyid <1220...@student.itk.ac.id>
Sent: Thursday, December 21, 2023 6:01:07 AM
To: AMPL Modeling Language <am...@googlegroups.com>
Subject: [AMPL 24929] Ampl model
 
Hi, I'm having trouble writing these model in ampl. Can you help me in writing it in the ampl model and can you give me examples to write the parameters in the ampl data??
The food Food groups are divided into four groups: vegetables, fruits, side dishes, and food staples. 

Your help means a lot, thank you 

--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ampl/ac5528c0-0390-4858-b3e5-d1fb577dc993n%40googlegroups.com.
Message has been deleted

AMPL Google Group

unread,
Dec 26, 2023, 5:21:31 PM12/26/23
to AMPL Modeling Language
I am guessing that a "food" and a "commodity" are the same thing in your description. You could set up the food groups like this:

set FOODS;
set FOOD_TYPES;
set FOOD_GROUP {FOOD_TYPES} within FOODS;

Then the data would look something like this (where just for this example, I have used some arbitrary letters for the food names):

set FOODS := a b c d e f g h i j ;

set FOOD_TYPES := vegetables fruits sides staples ;

set FOOD_GROUP[vegetables] := a b c ;
set FOOD_GROUP[fruits] := d e ;
set FOOD_GROUP[sides] := f ;
set FOOD_GROUP[staples] := g h i  j;

Your "minrat" constraint could then be written like this (after T, R, and minrat are defined appropriately):

subject to MinRatio {ft in FOOD_TYPES, t in T}:
   sum {f in FOOD_GROUP[ft]} R[f,t] >= minrat[ft];



--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
{#HS:2459926200-121309#}
On Sat, Dec 23, 2023 at 2:41 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Do you mean the word document or ampl.mod? Here is the word document.

On Fri, Dec 22, 2023 at 8:25 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi, Sure i can help. Can you send me the doc file instade of those screenshots

Mifthahul Janna Rosyid

unread,
Dec 27, 2023, 9:47:03 AM12/27/23
to AMPL Modeling Language
Then, how to write the parameter of minrat and maxrat. Would you mind give me some examples please ?

AMPL Google Group

unread,
Dec 27, 2023, 1:17:43 PM12/27/23
to AMPL Modeling Language
To test my example, I added these definitions:

set T;
param minrat {FOOD_TYPES};
var R {FOODS,T} >= 0;


Then I made up some test data for T and minrat.


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
{#HS:2459926200-121309#}
On Wed, Dec 27, 2023 at 2:47 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Then, how to write the parameter of minrat and maxrat. Would you mind give me some examples please ?

On Tue, Dec 26, 2023 at 10:12 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
I am guessing that a "food" and a "commodity" are the same thing in your description. You could set up the food groups like this:

set FOODS;
set FOOD_TYPES;
set FOOD_GROUP {FOOD_TYPES} within FOODS;

Then the data would look something like this (where just for this example, I have used some arbitrary letters for the food names):

set FOODS := a b c d e f g h i j ;

set FOOD_TYPES := vegetables fruits sides staples ;

set FOOD_GROUP[vegetables] := a b c ;
set FOOD_GROUP[fruits] := d e ;
set FOOD_GROUP[sides] := f ;
set FOOD_GROUP[staples] := g h i  j;

Your "minrat" constraint could then be written like this (after T, R, and minrat are defined appropriately):

subject to MinRatio {ft in FOOD_TYPES, t in T}:
   sum {f in FOOD_GROUP[ft]} R[f,t] >= minrat[ft];



--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.

Mifthahul Janna Rosyid

unread,
Dec 30, 2023, 8:58:21 AM12/30/23
to AMPL Modeling Language
can ampl solve the multi-objective? minimize and maximize?

AMPL Google Group

unread,
Dec 31, 2023, 11:54:56 PM12/31/23
to AMPL Modeling Language
Some solvers have options for multi-objective optimization. For example, to use Gurobi's multi-objective feature, you will need to first specify

option gurobi_options 'multiobj=1';

or, if you are already defining a gurobi_options string, add multiobj=1 to it. Then to provide priority and/or weighting information, you will need to use AMPL "suffix" commands to define some suffixes, which may include the following:

suffix objpriority IN;
suffix objweight IN;
suffix objreltol IN;
suffix objabstol IN;

As an example, suppose that your model has three AMPL "minimize" statements, which define three objective functions: A, B, and C. If you want Gurobi to minimize a weighted combination 8*A + 2*B + C, use AMPL "let" statements to assign objweight values to the objectives:

let A.objweight := 8;
let B.objweight := 2;
let C.objweight := 1;

Or, if you want Gurobi to first minimize A, then fix A at its minimum value and minimize B, then also fix B at its minimum value and minimize C, you can assign objpriority values to the objectives:

let A.objpriority := 3;
let B.objpriority := 2;
let C.objpriority := 1;

You can use any integers as objpriority values; the objective with the highest priority is minimized first, then the objective with the next highest priority, and so forth. Gurobi also offers two generalizations of this approach:
  • If two objectives have the same priority, then their weighted sum is minimized, using weights given by objweight. For example if B.objpriority is set instead to 1 above, then after A is minimized, Gurobi fixes A at its minimum value and minimizes 2*B + C.
  • You can assign an objreltol or objabstol value to an objective to allow its objective value to be degraded by a limited amount when lower-priority objectives are optimized. For example, if in addition to the priorities shown above, you set A.objreltol to 0.05, then instead of fixing A at its minimum value, Gurobi adds a constraint that A's value must be <= its minimum value plus 5%. Or, if you set A.objabstol to 100, then instead of fixing A at its minimum value, Gurobi adds a constraint that A's value must be <= its minimum value plus 100.
These generalizations can be combined to specify a variety of ways in which objective functions are handled. Also the same ideas apply to maximized objective functions -- but if you are using .objweight, you need to have all objectives be minimize or all objective be maximize at each priority level.

There are similar options in CPLEX. Also Xpress has more limited features for priorities only. If you are using another solver, you will have to program the multi-objective optimization yourself, which will involve writing an objective that is a weighted sum of objective expressions, and/or making a series of solves..


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
{#HS:2459926200-121309#}
On Sat, Dec 30, 2023 at 1:58 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
can ampl solve the multi-objective? minimize and maximize?

On Wed, Dec 27, 2023 at 5:36 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
To test my example, I added these definitions:

set T;
param minrat {FOOD_TYPES};
var R {FOODS,T} >= 0;


Then I made up some test data for T and minrat.


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.

Mifthahul Janna Rosyid

unread,
Jan 2, 2024, 10:48:24 AMJan 2
to AMPL Modeling Language
what about the preemptive method in ampl? how to solve it when it has multi-objectives?

AMPL Google Group

unread,
Jan 2, 2024, 8:55:08 PMJan 2
to AMPL Modeling Language
If you use .objpriority (rather than .objweight) then you get a preemptive method:
  • The solver first solves with the objective that has the highest priority.
  • Then the solver fixes the highest-priority objective to its optimal value, and solves with the objective that has the second-highest priority.
  • Then the solver fixes the second-highest-priority objective to its optimal value, and solves with the objective that has the third-highest priority.
The procedure continues in this way until all objectives have been considered.


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
{#HS:2459926200-121309#}
On Tue, Jan 2, 2024 at 3:52 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
what about the preemptive method in ampl? how to solve it when it has multi-objectives?

Mifthahul Janna Rosyid

unread,
Jan 3, 2024, 10:11:41 PMJan 3
to AMPL Modeling Language
ok, thank you Robert. I will try that.

Mifthahul Janna Rosyid

unread,
Jan 4, 2024, 2:36:47 PMJan 4
to AMPL Modeling Language
im so sorry. To make me understand better, can you give me some examples?

AMPL Google Group

unread,
Jan 5, 2024, 9:50:27 PMJan 5
to AMPL Modeling Language
To run the attached example using Gurobi as the solver, execute "include diet.run" in AMPL. The model (diet.mod) has two objective functions, Total_Cost and Total_Vit_C:

minimize Total_Cost: sum {j in FOOD} cost[j] * Buy[j];
maximize Total_Vit_C: sum {j in FOOD} amt["C",j] * Buy[j];


In diet.run, Total_Cost is set to have a higher priority:

suffix objpriority IN;
let Total_Cost.objpriority := 2;
let Total_Vit_C.objpriority := 1;


Then Gurobi is set up for multi-objective optimization, and is invoked with a "solve" command:

option solver gurobi;
option gurobi_options 'multiobj=1';
solve;


Gurobi minimizes the higher priority objective (Total_Cost) first. Then it fixes Total_Cost at its optimal value, and maximizes the lower-priority objective (Total_Vit_C). The resulting solution is returned to AMPL, where you can view it:

display Total_Cost, Total_Vit_C;
display Buy;



--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
{#HS:2459926200-121309#}
On Thu, Jan 4, 2024 at 7:36 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
im so sorry. To make me understand better, can you give me some examples?

On Thu, Jan 4, 2024 at 3:11 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
ok, thank you Robert. I will try that.

On Wed, Jan 3, 2024 at 1:20 AM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
If you use .objpriority (rather than .objweight) then you get a preemptive method:
  • The solver first solves with the objective that has the highest priority.
  • Then the solver fixes the highest-priority objective to its optimal value, and solves with the objective that has the second-highest priority.
  • Then the solver fixes the second-highest-priority objective to its optimal value, and solves with the objective that has the third-highest priority.
The procedure continues in this way until all objectives have been considered.


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
diet.mod
diet.dat
diet.run

Mifthahul Janna Rosyid

unread,
Jan 6, 2024, 10:54:08 AMJan 6
to AMPL Modeling Language

what if i have three objective functions and the third is about maximizing?

AMPL Google Group

unread,
Jan 8, 2024, 6:35:45 PMJan 8
to AMPL Modeling Language
It works the same way with three objective functions. In your model, use an AMPL "minimize" or "maximize" statement to define each of the three objective functions. Then assign a different priority to each of the three objective functions, using .objpriority as shown in my example.

Solving with Gurobi is then also the same as in my example. Also the same setup works with CPLEX, using

option solver cplex;
option cplex_options 'multiobj=1';


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
{#HS:2459926200-121309#}
On Sat, Jan 6, 2024 at 3:54 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
what if i have three objective functions and the third is about maximizing?

On Sat, Jan 6, 2024 at 1:15 AM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
To run the attached example using Gurobi as the solver, execute "include diet.run" in AMPL. The model (diet.mod) has two objective functions, Total_Cost and Total_Vit_C:

minimize Total_Cost: sum {j in FOOD} cost[j] * Buy[j];
maximize Total_Vit_C: sum {j in FOOD} amt["C",j] * Buy[j];


In diet.run, Total_Cost is set to have a higher priority:

suffix objpriority IN;
let Total_Cost.objpriority := 2;
let Total_Vit_C.objpriority := 1;


Then Gurobi is set up for multi-objective optimization, and is invoked with a "solve" command:

option solver gurobi;
option gurobi_options 'multiobj=1';
solve;


Gurobi minimizes the higher priority objective (Total_Cost) first. Then it fixes Total_Cost at its optimal value, and maximizes the lower-priority objective (Total_Vit_C). The resulting solution is returned to AMPL, where you can view it:

display Total_Cost, Total_Vit_C;
display Buy;



--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
Message has been deleted

Mifthahul Janna Rosyid

unread,
Jan 8, 2024, 11:15:32 PMJan 8
to AMPL Modeling Language
oke, i got it. Thank you Robert 

AMPL Google Group

unread,
Jan 9, 2024, 4:40:31 PMJan 9
to AMPL Modeling Language
Is nutrient_value_score the name of an objective function (from a "minimize" or "maximize" statement in your model)? If you try to use a "let" statement to assign a value to an objective function, then you will get an error message like the one you see. AMPL always computes the value of an objective automatically, from the current values of the variables.

If nutrient_value_score is not an objective function, then can you show the complete definition of nutrient_value_score from your model? With that information, it will be possible to give you more help.


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
{#HS:2459926200-121309#}
On Tue, Jan 9, 2024 at 4:15 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
oke, i got it. Thank you Robert

On Tue, Jan 9, 2024 at 4:15 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
What does it mean?

The let command cannot assign to nutrient_value_score.

context: let nutrient_value_score := >>> 1; <<<

Gurobi 10.0.1: Gurobi 10.0.1: optimal solution; objective 579486.6704

20 simplex iterations

1 branching nodes

Objective = total_cost

total_cost = 579487

kilocalories = 19982.5

nutrient_value_score = 99.7933

On Mon, Jan 8, 2024 at 11:23 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
It works the same way with three objective functions. In your model, use an AMPL "minimize" or "maximize" statement to define each of the three objective functions. Then assign a different priority to each of the three objective functions, using .objpriority as shown in my example.

Solving with Gurobi is then also the same as in my example. Also the same setup works with CPLEX, using

option solver cplex;
option cplex_options 'multiobj=1';


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.

Mifthahul Janna Rosyid

unread,
Jan 19, 2024, 1:33:00 PMJan 19
to AMPL Modeling Language
Hi,  what does it mean? Can you fix it?

#SHOW RESULTS

display total_cost, kilocalories, nutrient_value_score;

display ratio_commodity;

ampl: Gurobi 10.0.1: Gurobi 10.0.1: optimal solution; objective 61571.80715

23 simplex iterations

1 branching nodes

absmipgap=1.45519e-11, relmipgap=0

Objective = total_cost

total_cost = 61571.8

kilocalories = 1400

nutrient_value_score = 100


AMPL Google Group

unread,
Jan 19, 2024, 2:40:20 PMJan 19
to AMPL Modeling Language
For a mixed-integer program, the solver has proved optimality when the gap between the lower bound and the upper bound is 0. However, to avoid extra work that makes only a very minor improvement, Gurobi may stop when the absolute gap (absmipgap) or the gap relative to the size of the solution (mipgap) is "small enough".

In your example, the absmipgap is such a tiny number (1.45519e-11) that you should consider it to be the same as 0. Thus you should consider the solution returned by Gurobi to be optimal (and there is nothing that needs to be fixed).


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
{#HS:2459926200-121309#}
On Fri, Jan 19, 2024 at 6:33 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi, what does it mean? Can you fix it?

#SHOW RESULTS

display total_cost, kilocalories, nutrient_value_score;
display ratio_commodity;

ampl: Gurobi 10.0.1: Gurobi 10.0.1: optimal solution; objective 61571.80715
23 simplex iterations
1 branching nodes
absmipgap=1.45519e-11, relmipgap=0
Objective = total_cost
total_cost = 61571.8
kilocalories = 1400
nutrient_value_score = 100

On Tue, Jan 9, 2024 at 8:35 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
Is nutrient_value_score the name of an objective function (from a "minimize" or "maximize" statement in your model)? If you try to use a "let" statement to assign a value to an objective function, then you will get an error message like the one you see. AMPL always computes the value of an objective automatically, from the current values of the variables.

If nutrient_value_score is not an objective function, then can you show the complete definition of nutrient_value_score from your model? With that information, it will be possible to give you more help.


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.

Ijtihad Emon

unread,
Jan 19, 2024, 2:40:20 PMJan 19
to am...@googlegroups.com
Let me try

Ijtihad Emon BBA, (Notre Dame University of Bangladesh) MSc.Business Analytics (University of Kent), UK
UK, Canterbury 
  
Please make sure to maintain the time available for a contact.
Available Time for Contact:
Saturday - Not available.
Sunday - Not available.
Monday - 11:00 AM to 12:00 PM and 1:00 PM to 3:00 PM, (UK + Time Zone)
Tuesday - 12:00 PM to 2:00 PM, (UK + Time Zone)
Wednesday - 12:00 PM to 2:00 PM, (UK + Time Zone)
Thursday - 12:00 PM to 2:00 PM, (UK + Time Zone)
Friday - 12:00 PM to 2:00 PM, (UK + Time Zone)

Contact Number: +4407915302721
Email: ijtih...@gmail.com


--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.

Mifthahul Janna Rosyid

unread,
Jan 21, 2024, 11:34:21 AMJan 21
to AMPL Modeling Language
can ampl show the top 5 solutions for each objective function. If it yes, how to write in the running script?

AMPL Google Group

unread,
Jan 23, 2024, 11:46:08 AMJan 23
to AMPL Modeling Language
Computing solutions is a solver function, so you need to use a solver that can compute and return multiple solutions. I see you have been using Gurobi, which is a solver that can do this. There are a number of Gurobi options for requesting multiple solutions of various kinds; as a start, I suggest setting the following options before solving:

option gurobi_options 'sol_stub=bestgurobi sol:poolmode=2 sol:poollimit=5';

(Note that, if you are already defining a gurobi_options string, then you should instead add these three options to it.) This will cause Gurobi to write solution files named bestgurobi1.sol, bestgurobi2.sol, etc. Then you can read and process them with a loop like this:

option solver gurobi;
option gurobi_options 'sol:stub=bestgurobi sol:poolmode=2 sol:poollimit=5';

solve;

for {i in 1..Total_Cost.npool}
{
   solution ("bestgurobi" & i & ".sol");
   # put commands to display or process each solution here
}

where you replace Total_Cost by the actual name of your AMPL objective function.


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
{#HS:2459926200-121309#}
On Sun, Jan 21, 2024 at 4:34 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
can ampl show the top 5 solutions for each objective function. If it yes, how to write in the running script?

On Fri, Jan 19, 2024 at 7:40 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Let me try

8fa0012739429b.jpeg?v=0.7667126419756247


Ijtihad Emon BBA, (Notre Dame University of Bangladesh) MSc.Business Analytics (University of Kent), UK

+447915302721 | ijtih...@gmail.com

UK, Canterbury

whatsapp.png linkedin.png

green_32.png

Please make sure to maintain the time available for a contact.

Available Time for Contact:
Saturday - Not available.
Sunday - Not available.
Monday - 11:00 AM to 12:00 PM and 1:00 PM to 3:00 PM, (UK + Time Zone)
Tuesday - 12:00 PM to 2:00 PM, (UK + Time Zone)
Wednesday - 12:00 PM to 2:00 PM, (UK + Time Zone)
Thursday - 12:00 PM to 2:00 PM, (UK + Time Zone)
Friday - 12:00 PM to 2:00 PM, (UK + Time Zone)

Contact Number: +4407915302721
Email: ijtih...@gmail.com
On Fri, Jan 19, 2024 at 7:33 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
For a mixed-integer program, the solver has proved optimality when the gap between the lower bound and the upper bound is 0. However, to avoid extra work that makes only a very minor improvement, Gurobi may stop when the absolute gap (absmipgap) or the gap relative to the size of the solution (mipgap) is "small enough".

In your example, the absmipgap is such a tiny number (1.45519e-11) that you should consider it to be the same as 0. Thus you should consider the solution returned by Gurobi to be optimal (and there is nothing that needs to be fixed).


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
On Fri, Jan 19, 2024 at 6:33 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi, what does it mean? Can you fix it?

#SHOW RESULTS

display total_cost, kilocalories, nutrient_value_score;
display ratio_commodity;

ampl: Gurobi 10.0.1: Gurobi 10.0.1: optimal solution; objective 61571.80715
23 simplex iterations
1 branching nodes
absmipgap=1.45519e-11, relmipgap=0
Objective = total_cost
total_cost = 61571.8
kilocalories = 1400
nutrient_value_score = 100

On Tue, Jan 9, 2024 at 8:35 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
Is nutrient_value_score the name of an objective function (from a "minimize" or "maximize" statement in your model)? If you try to use a "let" statement to assign a value to an objective function, then you will get an error message like the one you see. AMPL always computes the value of an objective automatically, from the current values of the variables.

If nutrient_value_score is not an objective function, then can you show the complete definition of nutrient_value_score from your model? With that information, it will be possible to give you more help.


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.

Mifthahul Janna Rosyid

unread,
Feb 5, 2024, 12:17:43 PMFeb 5
to AMPL Modeling Language
oke, i will try. Thank you robert
Reply all
Reply to author
Forward
0 new messages