Syntax error in ampl

Hi. I’m new in AMPL. I have a quick question.

set YEARS; # Set representing the years
set UNITS; # Set representing the types of units

param demand{YEAR in YEARS}; # Demand data for each year
param capacity{UNIT in UNITS}; # Capacity data for each type of unit
param cost{UNIT in UNITS}; # Cost data for each type of unit
param unit_no{UNIT in UNITS}; # Limited number of units available

var units{UNIT in UNITS, YEAR in YEARS} integer >= 0; # Amount of units installed

# Objective:

minimize Total_Cost:
   sum{YEAR in YEARS, UNIT in UNITS} (cost[UNIT] * units[UNIT, YEAR]) / (1.05^((YEAR - 2025)));

# Demand constraints: Total capacity of installed units should meet the demand

subject to Demand_Constraint{YEAR in YEARS}:
   sum{UNIT in UNITS} (capacity[UNIT] * units[UNIT, YEAR]) >= demand[YEAR];

# Limited number of units available constraint

subject to Unit_Limit_Constraint{UNIT in UNITS}:
   sum{UNIT in UNITS} (units[UNIT, YEAR]) <= unit_no[UNIT];

data;

set YEARS := 2025 2030 2035 2040 2045 2050 2055 2060 2065;

set UNITS := Coal Gas Solar Wind Energy;

param demand :=
2025 0
2030 1000
2035 1600
2040 2200
2045 2600
2050 3000
2055 3500
2060 4000
2065 4400;

param capacity :=
Coal 500
Gas 200
Solar 100
Wind 50
Energy 150;

param cost :=
Coal 400
Gas 200
Solar 120
Wind 75
Energy 100;

param unit_no :=
Coal 1
Gas 10
Solar 8
Wind 10
Energy 4;

end;

this is my code.
I m getting this error;

hm.mod, line 23 (offset 902):
	syntax error
context:  sum{UNIT  >>> in  <<< UNITS} (units[UNIT, YEAR]) <= unit_no[UNIT];
ampl: 

What can be cause this error?

Thank you.

# Limited number of units available constraint

subject to Unit_Limit_Constraint{YEAR in YEARS}:
    sum{UNIT in UNITS} (units[UNIT, YEAR]) <= unit_no[UNIT];

when i change like this, i got this error;

hm.mod, line 23 (offset 935):
	UNIT is not defined
context:  sum{UNIT in UNITS} (units[UNIT, YEAR]) <=  >>> unit_no[UNIT] <<< ;

First, you tried to write

subject to Unit_Limit_Constraint {UNIT in UNITS}:
   sum {UNIT in UNITS} (units[UNIT, YEAR]) <= unit_no[UNIT];

This is not correct because you are defining UNIT twice: you are indexing the sum over {UNIT in UNITS} in the constraint expression, when you have already indexed the constraint over {UNIT in UNITS}.

Second, you tried to write

subject to Unit_Limit_Constraint {YEAR in YEARS}:
    sum {UNIT in UNITS} (units[UNIT, YEAR]) <= unit_no[UNIT];

This is not correct because you are not defining UNIT for the whole constraint. The expression sum {UNIT in UNITS} only defines UNIT where it appears in the expression that is being summed — that is, only in (units[UNIT,YEAR]). Thus you get an error when you write unit_no[UNIT] later in the constraint expression.

It would be correct to use your first version, but to sum over YEAR in YEARS. However, you need to decide whether that is the constraint definition that you want. After you read the model and data, you can use the AMPL command expand Unit_Limit_Constraint; to see the constraints that AMPL generates and to decide whether they are what you intended.