Search code examples
optimizationsumampl

AMPL double summation


I am new to ampl and I am trying to formulate a mathematical model, and I need a double summation I am trying to write : 600+ (the total capacity of the generators installed until year j) >= (demand of year j)

the capacity is being denoted by x[i,j], i in GENERATOR, j in YEAR.

600+ \sum_{j}(\sum_{i} x[i,j])>=demand[j]

subject to dem{j in YEAR}:600+sum{i in GENERATOR, j in YEAR}x[i,j]>=demand[j];

I tried this way but it gives me an error. Please help me to write a double summation.


Solution

  • If I've understood your requirements, what you need is something like this:

    subject to dem{j_current in YEAR}:
        600 + sum{i in GENERATOR, j_past in YEAR: j_past <= j_current}x[i,j_past] 
        >= demand[j_current];
    

    (adds all x[i,j] UP TO current year)

    or this:

    subject to dem{j_current in YEAR}:
        600 + sum{i in GENERATOR}x[i,j_current] 
        >= demand[j_current];
    

    (adds all x[i,j] AT current year)

    The way you had it will get you error messages because each constraint created by that statement already has a specific value of j so you can't then iterate over different values of j within the constraint.