Fixing my Ampl code

16 views
Skip to first unread message

Sanjana Achar

unread,
Oct 11, 2023, 10:35:11 PM10/11/23
to AMPL Modeling Language
I need help with this problem 
you run a small company providing napkins for hotels. For
each of the next 5 days you know how many napkins you’ll
need: 80, 70, 120, 150, 100, respectively. Each morning
when delivering your napkins, you can pick up the used
ones (what you delivered previous day). If you send those
(or some of those) for a cleaning (for $a/napkin) than they
can be used as new ones at the next morning delivery. You
can also buy new napkins at $b/napkin any given day,
before you make the deliveries for that day. You can also
keep clean (new) napkins in your stock for $c/day/napkin
for following days. Initially you have 100 napkins in stock.
Create a model to find a cost minimal plan (of
buying/cleaning/storing) to fulfill your contract (for the
next 5 days) create a .dat and .mod file

Here is my dat file 

data 

param n := 5;

param demand := 

    1 80

    2 70

    3 120

    4 150

    5 100;

param cost_cleaning := 0.50;  # Set the actual cleaning cost here

param cost_new := 0.75;       # Set the actual cost of buying new napkins here

param cost_stock := 0.10;     # Set the actual cost of storing napkins here


here is my mod file 

param n; # Number of days

set Days := 5;


param demand{Days};

param cost_cleaning;

param cost_new;

param cost_stock;


param initial_stock := 100; # Initial stock of napkins


var x{Days} >= 0;   # Number of napkins bought on each day

var y{Days} >= 0;   # Number of napkins cleaned on each day

var s{Days} >= 0;   # Number of napkins kept in stock after each day


# Objective function: Minimize the total cost

minimize TotalCost:

    cost_new * sum {d in Days} x[d] +

    cost_cleaning * sum {d in Days} y[d] +

    cost_stock * sum {d in Days} s[d];


subject to DemandConstraint {d in Days}:

    s[d] + initial_stock + x[d] = demand[d] + y[d];


# The number of napkins in stock cannot be negative

s.t. NonNegativeStock {d in Days}:

    s[d] >= 0;

    

# Variables representing buying and cleaning cannot be negative

s.t. NonNegativeBuys {d in Days}:

    x[d] >= 0;

s.t. NonNegativeCleaning {d in Days}:

    y[d] >= 0;


# Solve the LP model

data SAHC-hw4.dat;


option solver cplex;


solve;


display x;

display y;

display s;

display TotalCost;


i keep getting this error 

n is already defined

context:  param  >>> n; <<<  # Number of days

ampl: 


please help me ASAP 




AMPL Google Group

unread,
Oct 12, 2023, 5:58:26 PM10/12/23
to AMPL Modeling Language
Your statement "set Days := 5;" gives an error message, because 5 is a number, not a set. Instead, since you have already defined param n to be the number of days, you should write

set Days := 1..n;

which says that Days is the set of all integer numbers from 1 to n.

If you read the model and data, and then you decide to make some changes, be sure to give the command "reset;" before reading the model and data again. If you forget "reset;" then you will get an "n is already defined" error.


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
{#HS:2387683736-119963#}
--
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/7eda80b1-8a04-4142-94bd-b1bd11c2a2efn%40googlegroups.com.

Sanjana Achar

unread,
Oct 14, 2023, 8:36:26 AM10/14/23
to AMPL Modeling Language
Actually I have come up with a new dat and mod file. but I am still getting errors. Please use the problem I sent as reference. 

I do not know what is going on 

set Days := 1,2,3,4,5;  # Number of days


param d :=

    1 80

    2 70

    3 120

    4 150

    5 100;


param a := 1;  # Cost per napkin for cleaning

param b := 2;  # Cost per napkin for buying new napkins

param c := 0.5;  # Cost per day for storing clean napkins


param stock_init := 100;  # Initial number of napkins in stock


Mod file 

param d{1..n};  # Number of napkins needed each day

param a;  # Cost per napkin for cleaning

param b;  # Cost per napkin for buying new napkins

param c;  # Cost per day for storing clean napkins

param stock_init;  # Initial number of napkins in stock


var Buy{1..n} integer >= 0;  # Number of napkins bought each day

var Clean{1..n} integer >= 0;  # Number of napkins cleaned each day

var Stock{0..n} integer >= 0;  # Number of napkins in stock at the end of each day


minimize sum(i in 1..n, a*Clean[i] + b*Buy[i] + c*Stock[i]);


subject to NapkinBalance{i in 1..n}:

    Stock[i-1] + Buy[i] + Clean[i] = d[i] + Stock[i];


subject to StockInit: Stock[0] = stock_init;


solve;


for i in 1..n do

    printf "Day %d: Buy %d, Clean %d, Stock %d\n", i, Buy[i], Clean[i], Stock[i];

endfor;

I am still not getting the desired result and I do not know what is going wrong

Sanjana Achar

unread,
Oct 14, 2023, 8:36:26 AM10/14/23
to AMPL Modeling Language
I am still getting an error. This time it is telling me 

syntax error

context:   >>> param  <<< demand{DAYS}; # Demand for napkins on each day

ampl: 

Here is my mod file:

reset;


set Days # Define the set of days


param demand{DAYS}; # Demand for napkins on each day

param cost_cleaning; # Cost per napkin for cleaning

param cost_purchase; # Cost per napkin for purchasing

param cost_stock; # Cost per napkin for stock

param initial_stock; # Initial stock of napkins


var buy{DAYS} >= 0; # Number of napkins to purchase on each day

var clean{DAYS} >= 0; # Number of napkins to send for cleaning on each day

var stock{DAYS} >= 0; # Number of napkins to stock for each day


minimize TotalCost:

    sum {d in DAYS} cost_cleaning * clean[d] + cost_purchase * buy[d] + cost_stock * stock[d];


subject to StockBalance {d in DAYS}:

    stock[d] = (initial_stock + buy[d] - clean[d]) - demand[d];


option solver cplex;


data SAHC-hw4.dat;


solve;


display buy;


display clean;


display stock;

Here is my dat file:

set DAYS := 1 2 3 4 5;

param demand :=

1 80

2 70

3 120

4 150

5 100;

param cost_cleaning := a; # Replace with the actual cost per napkin for cleaning

param cost_purchase := b; # Replace with the actual cost per napkin for purchasing

param cost_stock := c;  # Replace with the actual cost per napkin for stock

param initial_stock := 100; can you please help me so that i can solve the napkin problem?


On Thursday, October 12, 2023 at 5:58:26 PM UTC-4 AMPL Google Group wrote:
Reply all
Reply to author
Forward
0 new messages