Iterating of elements in array

75 views Asked by At

I am trying to write a code of CPLEX OPL on an example of (from control systems) a typical MPC (Model Predictive Control) problem. As described, here:

With optimization variables:

With following parameters:

I have tried to write it but I am stuck at iteration of the array of variable "x" (state variable) as mentioned in the constraint of the optimization problem. The code I have so far written on OPL CPLEX is given as: (The model file as .mod extension on OPL platform)

//data

{string} state = ...;
{string} input = ...;

float A[state][state] =...;
float B[state][input] =...;
float Q[state] =...;
float R[input] =...;

//variable

dvar float State[state];
dvar float Input[input];
 
 
minimize
  sum( s in state, u in input ) 
    (State[s]*Q[s]*State[s] + Input[u]*R[u]*Input[u]);
    
subject to {
 
  forall( s in state, u in input )
    ct1: 
      A[s][s]*State[s] + B[s][u]*Input[u] == State[s+1];
} 

And the data file which I am using is given as: (the data file of OPL platform with .dat extension)

state = {"x","y","vx","vy"};
input = {"ux","uy"};

A = [[1, 0, 0.2, 0],
     [0, 1, 0, 0.2],
     [0, 0, 1, 0  ],
     [0, 0, 0, 1  ]];
     
B = [[0,   0],
     [0,   0],
     [0.2, 0],
     [0, 0.2]];

Q = [[1, 1, 1, 1],
     [1, 1, 1, 1],
     [1, 1, 1, 1],
     [1, 1, 1, 1]];

R = [[1, 1],
     [1, 1]];

Therefore, I need help kindly to solve this system as I am unable to solve the matter of iteration in the variable of the state variable in the constraint of the given problem.

Your kind help will be highly appreciated as I am stuck on this one for several weeks.

1

There are 1 answers

0
Alex Fleischer On

You can turn ct1 into

forall( s in state, u in input:s !=last( state ))
    ct1: 
      A[s][s]*State[s] + B[s][u]*Input[u] == State[next(state,s)];

.mod

//data

{string} state = ...;
{string} input = ...;

float A[state][state] =...;
float B[state][input] =...;
float Q[state] =...;
float R[input] =...;

//variable

dvar float State[state];
dvar float Input[input];
 
 
minimize
  sum( s in state, u in input ) 
    (State[s]*Q[s]*State[s] + Input[u]*R[u]*Input[u]);
    
subject to {
 
  forall( s in state, u in input:s !=last( state ))
    ct1: 
      A[s][s]*State[s] + B[s][u]*Input[u] == State[next(state,s)];
} 

.dat

state = {"x","y","vx","vy"};
input = {"ux","uy"};

A = [[1, 0, 0.2, 0],
     [0, 1, 0, 0.2],
     [0, 0, 1, 0  ],
     [0, 0, 0, 1  ]];
     
B = [[0,   0],
     [0,   0],
     [0.2, 0],
     [0, 0.2]];

Q = [1, 1, 1, 1]
     ;
R = [1, 1];

works