Sanjana Achar
Sanjana Achar

Reputation: 1

AMPL code. Error: Where is the issue? mod or dat file?

set D := {1, 2, 3, 4};

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


 param  D := {1, 2, 3, 4};;  # 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;

This .dat and .mod file is not giving me the desired output. I do not know if the mod file is the issue or the dat file. I am trying to find the optimal solution based on these conditions. 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.

I was expecting the optimal solution, based on those constraints

Upvotes: 0

Views: 73

Answers (1)

fdabrandao
fdabrandao

Reputation: 121

There are multiple errors in the model but it is not far from solving the problem. The main issues were the following:

  • You start with set D := {1, 2, 3, 4};, but it is not used anywhere.
  • You do not define parameter n. You need param n;.
  • You did not specify a name for the objective (e.g., minimize Cost: ...;).
  • You have data before the declarations. You need to declare set and parameters before loading the data in a data; section.
  • You did not limit Clean[i] so it could take any value and would be the cheapest option.

Solving the model below:

param n;  # number of days
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{i in 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 Cost:
    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 MaxClean{i in 1..n}:
    Clean[i] <= sum {j in 1..i-1} (d[j]-Clean[j]);

subject to StockInit:
    Stock[0] = stock_init;

data;
    param n := 5;
    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
model;

option solver gurobi;
solve;

for {i in 1..n} {
    printf "Day %d: Buy %d, Clean %d, Stock %d\n", i, Buy[i], Clean[i], Stock[i];
}

you should get the following optimal solution:

Gurobi 11.0.0: optimal solution; objective 480
6 simplex iterations
1 branching nodes

Day 1: Buy 0, Clean 0, Stock 20
Day 2: Buy 50, Clean 0, Stock 0
Day 3: Buy 0, Clean 120, Stock 0
Day 4: Buy 0, Clean 150, Stock 0
Day 5: Buy 0, Clean 100, Stock 0

Upvotes: 0

Related Questions