Assigned set used as domain

What is wrong with my code below?

I get these errors:

Error 187: Assigned set used as domain

b(j)     coefficent

Error 148: Dimension different

eq1(i).. F(i) =e= sum(j,b(j)*Y(i-j)) ;

Error 147: Real value for lag operator expected

eq1(i).. F(i) =e= sum(j,b(j)*Y(i-j)) ;

Here is the full code:

sets
t        total forecasting period        /1,2,3,4/
i(t)     index of the forecasting period /4/
j(t)     order
;

j(t) = not i(t)    ;


parameters

Y(t)     historical value
/
1        2
2        4
3        6
4        8
/

;

variables

F(i)     forecasted value
b(j)     coefficent
e(i)     error
etotal   total error

;


equations
eq1(i), eq2(i), eq3;

eq1(i).. F(i) =e= sum(j,b(j)*Y(i-j)) ;

eq2(i).. e(i) =e= F(i) - Y(i) ;

eq3.. etotal =e= sum(i,e(i)) ;



model timereg as /all/

option optcr = 0 ;

solve timereg using lp minimizing etotal ;


display t, i, j, Y

Thanks for your help!

I have also tried the following code:

sets
i        index of the forecasting period /1,2,3,4/
;

Alias    (j,i) ;

scalar
ii       week to predict /4/

parameters

Y(i)     historical value
/
1        2
2        4
3        6
4        8
/

;

variables

F(i)     forecasted value
b(j)     coefficent
e(i)     error
etotal   total error

;


equations
eq1(i), eq2(i), eq3;

eq1(i)$(ord(i)=ii)..     F(i) =e= sum(j$(ord(j)<ord(i)), b(j)*Y(ord(i)-ord(j))) ;

eq2(i)$(ord(i)=ii)..     e(i) =e= F(i) - Y(i) ;

eq3.. etotal =e= sum(i,e(i)) ;



model timereg as /all/

option optcr = 0 ;

solve timereg using lp minimizing etotal ;


display i

Hi Angert

You are running into problems because you define a variable over an assigned set (j) that is a subset of another subset, which is not possible.
You can correct this by defining the assigned set as a subset of the main set (t); same goes for i.

You can’t substract a set element for lagging because the subtracted set element is treated as a string. Here you can use ord(j) to get things running.
To use the subset in the sum, you can use the dollar sign when summing over the main set t (e.g. sum(t$j(t), …).

Here is the complete code:

sets
t        total forecasting period        /1,2,3,4/
i(t)     index of the forecasting period /4/
j(t)     order
;

j(t) = not i(t);

parameters

Y(t)     historical value
/
1        2
2        4
3        6
4        8
/

;

variables

F(t)     forecasted value
b(t)     coefficent
e(t)     error
etotal   total error

;


equations
eq1(i), eq2(i), eq3;

eq1(i).. F(i) =e= sum(t$j(t),b(t)*Y(i-ord(t))) ;

eq2(i).. e(i) =e= F(i) - Y(i) ;

eq3.. etotal =e= sum(i,e(i)) ;

model timereg as /all/

option optcr = 0 ;

solve timereg using lp minimizing etotal ;


display t, i, j, Y
$exit        


sets
    t        total forecasting period        /1,2,3,4/
    fp(t)    forecast periods                /2,4/
;

parameters
    Y(t)     historical value
    /
    1        2
    2        4
    3        6
    4        8
    /,
    index(t);

index("2") = 2;
index("4") = 4;

variables
    F(t)     forecasted value
    B(t)     coefficent
    E(t)     error
    ETOTAL   total error
;

equations
    eq1(fp),
    eq2(fp),
    eq3;

eq1(fp).. F(fp) =e= sum(t$(ord(t) < index(fp)), B(t) * Y(t));

eq2(fp).. E(fp) =e= F(fp) - Y(fp) ;

eq3.. ETOTAL =e= sum(fp, E(fp)) ;

model timereg as /all/

option optcr = 0 ;

solve timereg using lp minimizing etotal ;

display t,  Y;

By the way: it might be a good idea to write variables upper case and parameters lower case, so you or somebody else have to check if something is a parameter of a variable by scrolling back in the file.

Hope this helps

Cheers
Renger