Data Assign with AMPL in SP

35 views
Skip to first unread message

jinh...@gmail.com

unread,
Oct 29, 2014, 11:36:11 PM10/29/14
to am...@googlegroups.com

I have a problem with AMPL to do a stochastic programming,

currently I have three scenarios and assign probabilities before each solution.

A for loop is to solve a bunch of SP problem based on new simulated state. And assign its probabilities.

for { t in 1..SIZE-1 by 2} {

    let Demand := DemandSeq[t];
    let Resources := ResourcesSeq[t];

    # change probability of each stage
    if StateSequence[t] = 1 # N
        then {
            let P[1]:= 0.7;
            let P[2]:= 0.2;
            let P[3]:= 0.1;
        }
    else if StateSequence[t] = 2 # A
        then {
            let P[1]:= 0.6;
            let P[2]:= 0.3;
            let P[3]:= 0.1;
        }
    else if StateSequence[t] = 3 # M
        then {
            let P[1]:= 0.5;
            let P[2]:= 0.4;
            let P[3]:= 0.1;
        };
  solve;
  ...
}

Now what if I change this three scenarios problem into a 10 scenarios problem. I already have a 10*10 probability matrix, but I don't know how to assign prob.

victor.z...@gmail.com

unread,
Oct 30, 2014, 11:05:49 AM10/30/14
to am...@googlegroups.com
You can define a two-dimensional parameter to store probabilities:

param Probs{1..10, 1..10};

define it in the data in a compact form (or read from a database or a spreadsheet), for example:

data;
param Probs :
   1   2   3   ...  10 :=
1 0.1 0.2 0.05 ... 0.1
...

and use an iterated form of let to assign the data to P:

let {i in 1..10} P[i] := Prob[StateSequence[t], i];

or something like that.

HTH,
Victor


--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at http://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.

jinh...@gmail.com

unread,
Oct 30, 2014, 4:33:01 PM10/30/14
to am...@googlegroups.com
It helps, and one more question, how can I read a two-dimensional prob from a plain text file.
for example with size 3, I have the prob.txt to be like this:
0.7 0.2 0.1
0.6 0.3 0.1
0.5 0.4 0.1

and it should assign to Prob{1..3, 1..3}.

How to read such file?

victor.z...@gmail.com

unread,
Oct 30, 2014, 5:13:40 PM10/30/14
to am...@googlegroups.com
You can do this with the read command described in Section 9.5. Reading unformatted data: the read command of the AMPL book:

param Prob{1..3, 1..3};
read {i in 1..3, j in 1..3} Prob[i, j] < prob.txt;

HTH,
Victor

jinh...@gmail.com

unread,
Oct 30, 2014, 5:23:05 PM10/30/14
to am...@googlegroups.com
Oh,I just wrote before like 
read {i in 3, j in 3} Prob[i, j] < prob.txt;
which gave me the syntax error.
Now I learnt a lot.

Thanks.
Reply all
Reply to author
Forward
0 new messages