Solve Code Errors

Hello!

I’m new to developing code in AMPL. I started studying some tutorials and I’m trying to solve some exercises available on the internet.

I tried to develop a code for the problem shown at the end of this video link: AMPL Tutorial - YouTube

I’m getting some error messages, but I can’t figure out what’s wrong with my code.

The error that I can´t solve right now is:
“j is not defined”
context: maximize profit: sum{j in J} Pj[j] * Xj[j] - sum{j in J} Kj[j] * DELTAj[j] + >>> Cj[j] <<< * Xj[j] ;

I would greatly appreciate it if anyone could help me with any other errors you may encounter. I’ve attached the .mod, .dat and .run files I wrote.

run

It appears that you want the objective’s third term, Cj[j] * Xj[j], to also be summed over {j in J}:

maximize profit: 
  sum{j in J} Pj[j] * Xj[j] - sum{j in J} Kj[j] * DELTAj[j] + sum{j in J} Cj[j] * Xj[j] ;

Since all of the sums are over j in J, you could also write this as

maximize profit: 
  sum{j in J} (Pj[j]*Xj[j] - Kj[j]*DELTAj[j] + Cj[j]*Xj[j]) ;

(In general, an index like j that appears in a sum operator is defined only until the next + or - sign that is outside of parentheses, or until the end of the statement.)

1 Like