# Need Assistance with AMPL Constraint Modeling

**URL:** <https://discuss.ampl.com/t/need-assistance-with-ampl-constraint-modeling/1127>\
**Category:** Support\
**Created:** [June 7, 2024, 6:22am UTC](https://discuss.ampl.com/t/need-assistance-with-ampl-constraint-modeling/1127 "2024-06-07T06:22:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nolanmaris](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/nolanmaris/32/786_2.png) [@nolanmaris](https://discuss.ampl.com/u/nolanmaris)\
**Post date:** [June 7, 2024, 6:22am UTC](https://discuss.ampl.com/t/need-assistance-with-ampl-constraint-modeling/1127/1 "2024-06-07T06:22:52Z")

</div>

Hello ,

I hope you’re doing well. I am currently working on a project where I am utilizing AMPL for optimization modeling. I have faced a technical challenge related to modeling a specific constraint.

The issue I am facing revolves around incorporating time-dependent constraints into my optimization model. Specifically, I need to enforce constraints that vary dynamically based on temporal factors such as production schedules, resource availability, and demand fluctuations.

Even after many attempts and referring to the documentation, I am not able to devise a satisfactory solution. The constraint in question is crucial for my optimization problem, as it directly impacts the feasibility and efficiency of the proposed solutions.

If there are any recommended approaches for handling similar temporal issues, I would greatly appreciate the advice.

Thank you all ! 🙂

Thank you  
nolanmaris

---

<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:** [June 8, 2024, 2:12pm UTC](https://discuss.ampl.com/t/need-assistance-with-ampl-constraint-modeling/1127/2 "2024-06-08T14:12:28Z")

</div>

The principles of a time-based formulation are described in section [4.2 A multiperiod production model](https://ampl.com/wp-content/uploads/Chapter-4-Building-Larger-Models-AMPL-Book.pdf#page=5) of the [AMPL book](https://ampl.com/learn/ampl-book/). But for help with your particular constraint, you will need to provide some more details:

- The decision variables involved in your constraint.
- A description in words of the restriction on the variables that your constraint needs to enforce.
- A mathematical description of your constraint.
- Your attempt at describing the constraint in AMPL, even though it is not fully correct.

---

<div class="post-metadata">

**Author:** ![nolanmaris](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/nolanmaris/32/786_2.png) [@nolanmaris](https://discuss.ampl.com/u/nolanmaris)\
**Post date:** [June 10, 2024, 9:16am UTC](https://discuss.ampl.com/t/need-assistance-with-ampl-constraint-modeling/1127/3 "2024-06-10T09:16:48Z")

</div>

Hi 4er,

Thanks for your reply . Below are the required information -

1. Decision variables: Production quantities, resource allocations, and inventory levels.

2. Description: Constraint ensures production meets demand, considering resource constraints and inventory policies, dynamically adjusting over time.

3. Mathematical description: Pt≤Dt+It−1−It+RtP\_{t} \leq D\_{t} + I\_{t-1} - I\_{t} + R\_{t}Pt≤Dt+It−1−It+Rt

4. Attempt in AMPL:

subject to meet\_demand { forall(t in forall(t Time) { production prod [t] \<= demand\_forecast[t] + inventory[t-1] - inventory[t] + resource\_availability[t]; } }

Appreciate any further advice.

Thank you  
nolanmaris

---

<div class="post-metadata">

**Author:** ![marcos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/marcos/32/154_2.png) [@marcos](https://discuss.ampl.com/u/marcos)\
**Post date:** [June 10, 2024, 11:24am UTC](https://discuss.ampl.com/t/need-assistance-with-ampl-constraint-modeling/1127/4 "2024-06-10T11:24:42Z")

</div>

Hi @nolanmaris ,

The right way of writing indexed constraints in ampl is just specifying the index (there are no forall loops involved)

P\_{t} \leq D\_{t} + I\_{t-1} - I\_{t} + R\_{t}, \quad \forall t \in Time

```auto
subject to meet_demand {t in Time: t > 0}:
	prod[t] <= demand_forecast[t] + inventory[t-1] - inventory[t] + resource_availability[t];

```

I added the t \> 1 condition so you do not get an error for t = 0 (inventory[-1]?). It depends on what is the first time value you are considering.

You can use ordered sets to handle time in case it helps you:

```auto
set Time ordered := 1..100;
...
...
subject to meet_demand {t in Time: t != first(Time)}:
	prod[t] <= demand_forecast[t] + inventory[t-1] - inventory[t] + resource_availability[t];

```
