# Seeking Assistance with AMPL Programming - Syntax Error

**URL:** <https://discuss.ampl.com/t/seeking-assistance-with-ampl-programming-syntax-error/867>\
**Category:** Support\
**Tags:** errors-and-messages\
**Created:** [December 23, 2023, 3:30pm UTC](https://discuss.ampl.com/t/seeking-assistance-with-ampl-programming-syntax-error/867 "2023-12-23T15:30:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Liu\_Fan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/liu_fan/32/229_2.png) [@Liu\_Fan](https://discuss.ampl.com/u/Liu_Fan)\
**Post date:** [December 23, 2023, 3:30pm UTC](https://discuss.ampl.com/t/seeking-assistance-with-ampl-programming-syntax-error/867/1 "2023-12-23T15:30:45Z")

</div>

I hope this email finds you well. I am currently working on a mathematical optimization problem and using AMPL for programming. However, I have encountered syntax errors while trying to translate certain mathematical constraints into AMPL code. I would greatly appreciate your guidance on resolving this issue.

Here is a brief description of the mathematical constraints I am working with:

 ![image](https://us1.discourse-cdn.com/flex019/uploads/ampl/original/1X/f1450f3f3f1cf2628aa158027ecffd9b632411cf.png)

The code I wrote in AMPL is as follows：  
set I := {1..10 by 1}; # Signal cycles  
set J := {0..4 by 1}; # Signal phases  
var g{i in I, j in J} \>= 10, \<= 25 integer; # Decision variable for green light duration in phase j of signal cycle i  
var C\_{I}; # Start time of cycle i  
var cycle\_length{i in I} = sum{j in 1..4} g[i, j]; # Length of cycle i  
var in\_cycle{k in K, i in I} binary; # Binary variable indicating if vehicle k arrives at the intersection during cycle i

# Calculate the start time of signal cycle i

subject to Cycle\_Start\_Time\_Constraint{i in 2..10}:  
C\_[i] = sum{j in 1..4} g[i-1, j] + C\_[i-1];

# Initial condition

subject to Cycle\_Start\_Time\_Constraint\_0:  
C\_[1] = 0; # The first cycle starts at time 0

# Determine in which signal cycle each vehicle arrives

subject to Car\_In\_Cycle{k in K, i in I}:  
t[k] \>= C\_[i] and t[k] \<= C\_[i] + cycle\_length[i] \* in\_cycle[k, i] ;

param p{k in K}; # Phase at which vehicle k enters the intersection  
var delay{k in K}; # Vehicle delay

# If the vehicle can enter the intersection in the current cycle

subject to delay\_1{k in K, i in I}:  
t[k] \<= C\_[i\* in\_cycle[k, i]] + sum {1..p[k]} g[i, p[k]]  
==\> d[k] = max(0, C\_[i\* in\_cycle[k, i] ] + sum {1..p[k]-1} g[in\_cycle[k, i] \* i, p[k]]-t[k]) ;

# If the vehicle needs to enter the intersection in the next cycle

subject to delay\_2{k in K, i in I}:  
t[k] \>= C\_[in\_cycle[k, i] \* i] + sum {1..p[k]} g[in\_cycle[k, i] \* i, p[k]]  
==\> d[k] = C\_[in\_cycle[k, i] \* i + 1] + sum {1..p[k]-1} g[in\_cycle[k, i] \* i + 1, p[k]]-t[k];

# Minimize the total delay of all vehicles

minimize:  
sum{k in K} d[k];

The syntax error message I encountered is as follows：  
Variables in subscripts are not yet allowed.  
context: t[k] \>= \>\>\> C\_[in\_cycle[k,i]\*i] \<\<\< + sum {1..p[k]} g[in\_cycle[k,i]\*i,p[k]]

```
variable in index expression

```

context: t[k] \>= \>\>\> C\_[in\_cycle[k,i]\*i] \<\<\< + sum {1..p[k]} g[in\_cycle[k,i]\*i,p[k]]

```
k is not defined

```

context: t[k] \>= C\_r[in\_cycle[k,i]\*i]+ sum \>\>\> {1..p[k]} g[in\_cycle[k,i]\*i,p[k]]

---

<div class="post-metadata">

**Author:** ![4er](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/4er/32/530_2.png) [@4er](https://discuss.ampl.com/u/4er)\
**Post date:** [December 27, 2023, 4:59pm UTC](https://discuss.ampl.com/t/seeking-assistance-with-ampl-programming-syntax-error/867/3 "2023-12-27T16:59:44Z")

</div>

As indicated by “variable in index expression”, AMPL does not recognize `C_[i*in_cycle[k,i]]` where `in_cycle` is a variable.

Were you able to find a different formulation, as suggested in your later question to this forum?
