If then Else throwing syntax error

Hello,

I am trying to use if-ten-else in AMPL modeling in python.

I am getting the following error:

line 53 offset 1806
syntax error
context:   >>> else <<< 

The constraint looks like:

subject to battery_level_constraint{n in TIMESLOTS}:
    if n > 1 then
        battery_level[n] = battery_level[n-1] - (y[n]*battery_usage) + (q[n]*battery_usage) + (p[n]*battery_usage)
    else
        battery_level[n] = battery_size;

any help is highly appreciated.

When using an if-then-else construct in an AMPL subject to statement, then and else must be followed by expressions rather than by entire constraints. In your example, that is easy to do as follows:

subject to battery_level_constraint {n in TIMESLOTS}:
    battery_level[n] = 
       if n > 1 then
          battery_level[n-1] - (y[n]*battery_usage) + (q[n]*battery_usage) + (p[n]*battery_usage)
       else
          battery_size;

Thank you for the reply that solved my issue.