Need help

32 views
Skip to first unread message

Sun Fei

unread,
May 16, 2017, 8:53:50 PM5/16/17
to AMPL Modeling Language

Hello,
Below is the practice code for the scenario tree model. I can not run out the result. Would you help me find out where has the problem.

Thank you!

model file:
param T; # number of periods
set investments; # different investment options
set periods:=1..T;
param initwealth; # initial holdings
param target; # target value G at time T
param invest; # value of investing ABOVE target value
param penalty; # penalty for not meeting target
#Parameters to define the random structure
param realizations{t in periods}>0 integer;
param return{k in investments,t in periods, s in 1..realizations[t]}>0; # return on each inv
param cond_prob{t in periods, s in 1..realizations[t]}<=1;
#Auxiliary sets and parameters for easier manipulation of scenarios
param branches{t in 1..T+1}:= (if t<=T then prod{s in t..T}realizations[s] else 1);
set scenarios:= 0..(prod{t in periods}realizations[t]-1);
param starttime{j in scenarios}:=min{t in 1..T+1: (j mod branches[t])=0}t;
param parent{j in scenarios}:=(if j = 0 then 0 else j-(j mod branches[starttime[j]-1]));
set event_nodes:={j in scenarios, t in 1..T+1:t>= starttime [j]};
param rv_address{(j,t) in event_nodes:t>1}:=(((j-(j mod branches[t]))/ branches[t]) mod realizations[t-1])+1;
param previous{(j,t) in event_nodes:t>1}:=( if t=starttime[j] then parent[j] else j );
param path_prob{(j,t) in event_nodes}:=(if t=1 then 1 else path_prob[previous[j,t] ,t-1]*cond_prob[t-1,rv_address[j,t]]);
# variables
var amtinvest{investments,t in periods,s in scenarios:starttime[s]<= t}>= 0; #actual amounts inv’d
var above_target{s in scenarios}>= 0; # amt above final target
var below_target{s in scenarios}>= 0; # amt below final target
# objective
maximize Exp_value:
sum{s in scenarios, (j,t) in event_nodes}path_prob[j,t]*(invest*above_target[s]-penalty*below_target[s]); 
 # constraints
subject to budget{s in scenarios}:
sum{k in investments}amtinvest[k,1,0]=initwealth;
subject to balance{k in investments,t in 2..T, (s,t) in event_nodes:t>1}:
amtinvest[k,t,s]=return[k,t-1,rv_address[s,t]]*amtinvest[k,t-1,previous[s,t]];
subject to stage{s in scenarios,t in T..T}:
sum{k in investments}return[k,T,s]*amtinvest[k,T,s]-above_target[s]+below_target[s]=target;

Data file
set investments:=stock bond; # different investment options
param T:=3; # number of periods
param realizations:= 1 2
                     2 2  
                     3 2;
 param initwealth:=55000;    
param target:=80000; # target value G at time T
param invest:=1; # value of investing beyond target value
param penalty:=4; # penalty for not meeting target
param return:=
[stock,*,*]:  1   2:= 
         1 1.25 1.06
         2 1.25 1.06
         3 1.25 1.06

[bond,*,*]:  1   2:= 
         1 1.12 1.03
         2 1.12 1.03
         3 1.12 1.03;
param cond_prob:=
              [1,*] 1 0.5    2 0.5
              [2,*] 1 0.25   2 0.25
              [3,*] 1 0.125  2 0.125;


Robert Fourer

unread,
May 17, 2017, 11:14:01 AM5/17/17
to am...@googlegroups.com
You can see that the error is

ampl: solve;
Error executing "solve" command:
error processing constraint stage[0,3]:
invalid subscript return['stock',3,0]

So your constraint stage[0,3] is making a reference to variable return['stock',3,0], but the indexing set in your definition for return,

param return {k in investments, t in periods, s in 1..realizations[t]} > 0;

does not contain ('stock',3,0):

ampl: display {k in investments, t in periods, s in 1..realizations[t]};

set {k in investments, t in periods, s in 1 .. realizations[t]} :=
(stock,1,1) (stock,2,2) (bond,1,1) (bond,2,2)
(stock,1,2) (stock,3,1) (bond,1,2) (bond,3,1)
(stock,2,1) (stock,3,2) (bond,2,1) (bond,3,2);

Perhaps the problem is that your definition of constraint stage is indexed over "s in scenarios" but your definition of param return is indexed over "s in 1..realizations[t]".

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of Sun Fei
Sent: Tuesday, May 16, 2017 7:54 PM
To: AMPL Modeling Language
Subject: [AMPL 14041] Need help

Below is the practice code for the scenario tree model. I can not run out the result. Would you help me find out where has the problem.

.......


Sun Fei

unread,
May 20, 2017, 4:48:03 PM5/20/17
to AMPL Modeling Language, 4...@ampl.com
Hello;
Below are the mathmatics function and code file for my question. after running, I got this result "MINOS 5.51: infeasible problem.
10 iterations"   which are not match my solution by excel . Please help me, I am stuck in this question for a long time.

Thank you!

amtinvest [bond,*,*] (tr)
:        1              2              3              4          :=
0   -3.62404e-20   -3.42048e-20   -3.5204e-20    -3.7777e-20
1     .              .              .            -3.34698e-20
2     .              .            -2.53804e-20   -3.7777e-20
3     .              .              .            -3.30956e-20
4     .            -3.29308e-20   -3.48373e-20   -3.7777e-20
5     .              .              .            -3.3426e-20
6     .              .            -2.51006e-20   -3.7777e-20
7     .              .              .            -2.47892e-20
 [stock,*,*] (tr)
:        1             2             3             4         :=
0   1.92716e-21   4.23387e-21   4.46097e-21   9.84106e-21
1    .             .             .            4.01881e-21
2    .             .            8.25471e-21   9.84106e-21
3    .             .             .            7.86177e-21
4    .            3.92909e-21   4.36997e-21   9.84106e-21
5    .             .             .            4.00847e-21
6    .             .            8.07663e-21   9.84106e-21
7    .             .             .            7.84155e-21
;


param T; # number of periods
set investments; # different investment options
set periods:=1..T;
param initwealth; # initial holdings
param target; # target value G at time T
param invest; # value of investing ABOVE target value
param penalty; # penalty for not meeting target
#Parameters to define the random structure
param realizations{t in periods}>0 integer;
param return{k in investments,t in periods, s in 1..realizations[t]}>0;

param cond_prob{t in periods, s in 1..realizations[t]}<=1;
#Auxiliary sets and parameters for easier manipulation of scenarios
param branches{t in 1..T+1}:= (if t<=T then prod{s in t..T}realizations[s] else 1);
set scenarios:= 0..(prod{t in periods}realizations[t]-1);
param starttime{j in scenarios}:=min{t in 1..T+1: (j mod branches[t])=0}t;
param parent{j in scenarios}:=(if j = 0 then 0 else j-(j mod branches[starttime[j]-1]));
set event_nodes:={j in scenarios, t in 1..T+1:t>= starttime [j]};
param rv_address{(j,t) in event_nodes:t>1}:=(((j-(j mod branches[t]))/ branches[t]) mod realizations[t-1])+1;
param previous{(j,t) in event_nodes:t>1}:=( if t=starttime[j] then parent[j] else j );
param path_prob{(j,t) in event_nodes}:=(if t=1 then 1 else path_prob[previous[j,t] ,t-1]*cond_prob[t-1,rv_address[j,t]]);
# variables
var amtinvest{investments,t in 1..T+1,s in scenarios:starttime[s]<= t}>= 0; #actual amounts inv’d
var above_target{s in scenarios}>= 0; # amt above final target
var below_target{s in scenarios}>= 0; # amt below final target
# objective
maximize Exp_value{ t in T..T}:
sum{s in scenarios}path_prob[s,T+1]*(invest*above_target[s]-penalty*below_target[s]);
# constraints
subject to budget:
sum{k in investments}amtinvest[k,1,0]=initwealth;
subject to balance{k in investments,t in 2..T+1, (s,t) in event_nodes:t>1}:
amtinvest[k,t,s]=return[k,t-1,rv_address[s,t]]*amtinvest[k,t-1,previous[s,t]];

subject to last{(s,t) in event_nodes:t=4}:
sum{k in investments}amtinvest[k,t,s]-above_target[s]+below_target[s]=target;
 subject to nont1{k in investments,(s,t) in event_nodes:t=3}:
amtinvest[k,t,0]/return[k,t-1,rv_address[0,t]]=amtinvest[k,t,2]/return[k,t-1,rv_address[2,t]];
subject to nont2{k in investments,(s,t) in event_nodes:t=3}:
amtinvest[k,t,4]/return[k,t-1,rv_address[4,t]]=amtinvest[k,t,6]/return[k,t-1,rv_address[6,t]];
subject to nont3{k in investments,(s,t) in event_nodes:t=2}:
amtinvest[k,t,0]/return[k,t-1,rv_address[0,t]]=amtinvest[k,t,4]/return[k,t-1,rv_address[4,t]];
subject to nont4{k in investments,s in 1..7,(s,t) in event_nodes:t=4}:
amtinvest[k,t,s]/(return[k,t-1,rv_address[s,t]]*return[k,t-2,rv_address[s,t]])=amtinvest[k,t,s-1]/(return[k,t-1,rv_address[s-1,t]]*return[k,t-2,rv_address[s-1,t]]);
 
data file:
set investments:=stock bond; # different investment options
param T:=3; # number of periods
param realizations:= 1 2
                     2 2  
                     3 2;
param initwealth:=55000;
         
param target:=80000; # target value G at time T
param invest:=1; # value of investing beyond target value
param penalty:=4; # penalty for not meeting target
param return:=
[stock,*,*]:  1   2:= 
         1 1.25 1.06 
         2 1.25 1.06 
         3 1.25 1.06
[bond,*,*]:  1   2:= 
         1 1.12 1.03
         2 1.12 1.03
         3 1.12 1.03; #{investments,1..scenarios,2..T}; # return on each inv
param cond_prob:=
              [1,*] 1 0.5  2 0.5
              [2,*] 1 0.5  2 0.5
              [3,*] 1 0.5  2 0.5;
           
  

Robert Fourer

unread,
May 23, 2017, 12:59:53 AM5/23/17
to am...@googlegroups.com
In "infeasible problem" message means that there is no feasible solution to your problem -- no way to assign values to the variables that will satisfy all of the constraints and bounds. When you display amtinvest you see a lot of tiny magnitudes that should be interpreted as zeros, but that does not mean anything since it is part of an infeasible solution.

If you switch to a different solver you can find an IIS as described in other recent posts to this forum. For example when I set

option solver gurobi;
option gurobi_options 'iisfind 1 iismethod 1';

and then solve, I can then use the command

expand {i in 1.._ncons: _con[i].iis <> "non"} _con[i];

to see the 13 constraints that are in this IIS. (This particular IIS does not have any variable bounds in it.) There is no feasible solution to just these 13 constraints, but there is a feasible solution to any 12 of them. You should first check these constraints to be sure they are all being generated correctly; if so, then since there are only a few of them, it should not be hard to figure out why they can't have a feasible solution.
Reply all
Reply to author
Forward
0 new messages