# Help with Code Please!

**URL:** <https://discuss.ampl.com/t/help-with-code-please/762>\
**Category:** Support\
**Created:** [October 14, 2023, 11:12pm UTC](https://discuss.ampl.com/t/help-with-code-please/762 "2023-10-14T23:12:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![emsoccer1125](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/emsoccer1125/32/450_2.png) [@emsoccer1125](https://discuss.ampl.com/u/emsoccer1125)\
**Post date:** [October 14, 2023, 11:12pm UTC](https://discuss.ampl.com/t/help-with-code-please/762/1 "2023-10-14T23:12:56Z")

</div>

I need help with this problem. It is not returning with the answers I need.

Question:  
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).  
Note that what you buy can be used for delivery  
the same day!  
Note that you cannot send more to the cleaner  
than what you delivered the previous day!  
You need to pick it up in the morning from the  
hotel (when you deliver the clean ones for the day)  
… take it to the cleaner in the afternoon … use it  
following morning …

DAT File:

# HW4 data file

#SETS  
set days := 1 2 3 4 5;

#PARAMETERS  
param Demand := #Demand of napkins per day for 5 days  
1 80  
2 70  
3 120  
4 150  
5 100;

param a := 5; #param a, cleaning cost  
param b := 10; #param b, new napkin cost  
param c := 1; #param c, stock cost per day per napkin  
param r := 100; #param r, initial napkin stock

MOD File:

# HW4 model file

reset ;

#SETS

set days;

#PARAMETERS

param Demand{days} \>=0;

param a;

param b;

param c;

param r;

#variables

var X{days} \>=0; #Napkins to send for cleaning on day t

var Y{days} \>=0; #Number of new napkins to buy on day t

var Z{days} \>=0; #Number of napkins to keep in stock on day t

#OBJECTIVE FUNCTION- MINIMIZE TOTAL COST

minimize TotalCost:

sum{d in days}

(a \* X[d] + b \* Y[d] + c \* Z[d]);

#CONSTRAINTS

subject to InitialStock:

Z[1] = r;

subject to CleaningConstraint{d in days}:

if d \>=2 then X[d] \<= X[d-1];

subject to NapkinBalance{d in days}:

Z[d] = Z[d-1] + Y[d] - Demand[d];

#EXECUTION

data HW4.dat;  
option solver cplex;  
solve ;

#OUTPUT

display {d in days} X[d];  
display {d in days} Y[d];  
display {d in days} Z[d];

---

<div class="post-metadata">

**Author:** ![4er](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/4er/32/530_2.png) [@4er](https://discuss.ampl.com/u/4er)\
**Post date:** [October 15, 2023, 2:11am UTC](https://discuss.ampl.com/t/help-with-code-please/762/2 "2023-10-15T02:11:04Z")

</div>

AMPL’s if-then is an expression, not a constraint. To restrict your CleaningConstraint to d \>= 2, put the restriction in the indexing:

```auto
subject to CleaningConstraint {d in days: d >= 2}:
   X[d] <= X[d-1];

```

Also it looks like NapkinBalance will only work for d \>= 2.

---

<div class="post-metadata">

**Author:** ![emsoccer1125](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/emsoccer1125/32/450_2.png) [@emsoccer1125](https://discuss.ampl.com/u/emsoccer1125)\
**Post date:** [October 15, 2023, 2:54am UTC](https://discuss.ampl.com/t/help-with-code-please/762/3 "2023-10-15T02:54:00Z")

</div>

> [@emsoccer1125](#):
>
> subject to CleaningConstraint{d in days}:
> 
> if d \>=2 then X[d] \<= X[d-1];

Thank you so much, that worked. I really appreciate it!!!
