# \[AMPL 24428\] Solve objective from a series of constraint

**URL:** <https://discuss.ampl.com/t/ampl-24428-solve-objective-from-a-series-of-constraint/284>\
**Category:** Google Group Mirror\
**Created:** [December 19, 2022, 6:01am UTC](https://discuss.ampl.com/t/ampl-24428-solve-objective-from-a-series-of-constraint/284 "2022-12-19T06:01:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oink\_oink\_B](https://avatars.discourse-cdn.com/v4/letter/o/4491bb/32.png) [@Oink\_oink\_B](https://discuss.ampl.com/u/Oink_oink_B)\
**Post date:** [December 19, 2022, 6:01am UTC](https://discuss.ampl.com/t/ampl-24428-solve-objective-from-a-series-of-constraint/284/1 "2022-12-19T06:01:40Z")

</div>

Hello. I am new to AMPL and I want to solve the same objective function but under different constraints where the only difference is MR[r], which is a vector. I have tried different variations similar to the one below, but it will only display r = 13. How do I display objective function for all r = 1…13?

My .mod file  
minimize Objective {r in 1…13}:  
sum {i in 1…n,j in 1…n} Mu[i]\*Mu[j]\*Cov[i,j];

subject to Constraint {r in 1…13}:  
sum {i in 1…n} Mu[i]\*ER[i] \>= MR[r];

My .run file  
for {r in 1…13}{  
objective Objective[r];  
solve;  
printf ‘%8.5f’ , sum {i in 1…n} Mu[i]\*ER[i];  
}

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [December 19, 2022, 7:06pm UTC](https://discuss.ampl.com/t/ampl-24428-solve-objective-from-a-series-of-constraint/284/2 "2022-12-19T19:06:51Z")

</div>

The simplest was to do this is to define only one objective function and one constraint:

```auto
param MR {1..13};
param MR_val;

minimize Objective:
   sum {i in 1..n,j in 1..n} Mu[i]*Mu[j]*Cov[i,j];

subject to Constraint:
   sum {i in 1..n} Mu[i]*ER[i] >= MR_val;

```

Then in each pass through the loop, assign one of the MR[r] values to MR\_val:

```auto
for {r in 1..13} {
   let MR_val := MR[r];
   solve;
   printf '%8.5f\n', sum {i in 1..n} Mu[i]*ER[i];
}

```
