Re: Code problem

• To: mathgroup at smc.vnet.net
• Subject: [mg65640] Re: Code problem
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Tue, 11 Apr 2006 04:04:54 -0400 (EDT)
• Organization: The Open University, Milton Keynes, UK
• References: <e17fne\$omi\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Eoin Gleeson wrote:
> Dear Group,
>
> I am trying to execute the model below in Mathematica 5.2 (student),
> but no matter what values I input for the initial conditions, the
> output values for ST and T fail to change. I wonder if anyone would be
> so good as to have a look at where I am going wrong with the code? Or
> even suggest an appropriate forum to pursue this problem.
>
> Kind regards,
> Eoin Gleeson
> Trinity College Dublin
>
> In[1]:=
>
> Clear[PopSize];
> Clear[Wealth]
> Clear[Iteration];
> Clear[SampleSize];
> Clear[Cost];
> Clear[Benefit];
> Clear[Tol];
>
> PopSize = 100;
> Iteration =20;
> SampleSize = 10;
> Cost = 1;
> Benefit = SampleSize;
> Tol = 8;
> Tol2 =3;
>
> Wealth = Table [1, {PopSize}];
> Clear[Status]
> Status = Table [0, {PopSize}];
> Clear[PopString]
> PopString = Table[Random[], {PopSize}];
> Return["Initial Wealth"]
> Return[Wealth]
> Return["Initial Status"]
> Return[Status]
>
>
> Do[
>
>   {Clear[a];
>     a = Table[Random[Integer, {1, PopSize}], {SampleSize}];
>     Clear[S];
>     S = Table[Status[[a[[m]]]], {m, 1, SampleSize}];
>     Clear[ST];
>     SS = SampleSize*SampleSize;
>     ST=Table[0,{ SS}];
>     For[i=1, i<SampleSize+1, i++, {For[j=1, j<Samplesize+1, j++,
>           {For[k=0, k<SS+1, k++, ST[[k]]=S[[i]]-S[[j]]]}]}];
>     Clear[T];
>     T=0;
>     For[j=0,j<SS+1, j++,{If[ST[[j]]<Tol2, T++]}];
>     For[i=1, i<SampleSize+1, i++,
>       {If[T>Tol+Tol+2,
>
>                 {If[Status[[a[[i]]]]â? 5, Status[[a[[i]]]]++],
>                   Wealth[[a[[i]]]] = Wealth[[a[[i]]]] - Cost +Benefit},
>
>
>                {If[Status[[a[[i]]]]â?  -5, Status[[a[[i]]]]--]}
>            ]}]
>     }, {Iteration}]
> Return["ST"]
> Return[ST]
> Return["T"]
> Return[T]
> Return["Final Wealth"]
> Return[Wealth]
> Return["Final Status"]
> Return[Status]
>
>
>
> Out[20]=
> Return[Initial Wealth]
>
> Out[21]=
> Return[{1,1,1,
>
> 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
>
> 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
>     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}]
>
> Out[22]=
> Return[Initial Status]
>
> Out[23]=
> Return[{0,0,0,
>
> 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
>
> 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
>     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}]
>
> Out[25]=
> Return[ST]
>
> Out[26]=
> Return[{0,0,0,
>
> 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
>
> 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
>     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}]
>
> Out[27]=
> Return[T]
>
> Out[28]=
> Return[100]
>
> Out[29]=
> Return[Final Wealth]
>
> Out[30]=
> Return[{19,19,
>
> 1,28,1,10,1,37,10,19,19,1,28,1,10,28,1,10,1,28,10,19,19,28,10,46,55,10,
>     19,28,10,37,19,46,10,46,37,10,
>
> 1,28,10,28,19,19,28,10,10,19,10,37,10,28,19,28,19,19,10,28,28,1,10,10,10,
>
> 19,19,28,19,19,10,46,28,37,10,19,19,10,37,28,10,10,19,37,1,28,19,1,19,
>     28,19,28,19,19,10,10,1,28,19,37,1,19}]
>
> Out[31]=
> Return[Final Status]
>
> Out[32]=
> Return[{2,2,0,
>
> 3,0,1,0,4,1,2,2,0,3,0,1,3,0,1,0,3,1,2,2,3,1,5,5,1,2,3,1,4,2,5,1,5,4,1,0,3,
>
> 1,3,2,2,3,1,1,2,1,4,1,3,2,3,2,2,1,3,3,0,1,1,1,2,2,3,2,2,1,5,3,4,1,2,2,1,
>     4,3,1,1,2,4,0,3,2,0,2,3,2,3,2,2,1,1,0,3,2,4,0,2}]
>
Hi Eoin,

PopSize = 100;
Iteration = 20;
SampleSize = 10;
Cost = 1;
Benefit = SampleSize;
Tol = 8;
Tol2 = 3;

Print["Initial Wealth: "]
Wealth = Table[1, {PopSize}]
Print["Initial Status: "]
Status = Table[0, {PopSize}]

(* Constant values should be initialized outside a loop *)
SS = SampleSize2;
Print["SS: ", SS];

Do[{
a = Table[Random[Integer, {1, PopSize}], {SampleSize}];
Print["----------------------------------------"];
Print["a: ", a];
S = Table[Status[[a[[m]]]], {m, 1, SampleSize}];
Print["S: ", S];
ST = Table[0, {SS}];

(*For[i = 1, i <= SampleSize, i++,
(* Beware : Mathematica is case sensitive
Samplesize is not SampleSize! *)
For[j = 1, j <= SampleSize, j++,
For[k = 0, k <= SS, k++,
ST[[k]] = S[[i]] - S[[j]];
(*Print["i: ", i, "  j: ", j, "  k: ", k, "  S[[i]]: ",
S[[i]],
"  S[[j]]: ", S[[j]], "  ST[[k]]: ", ST[[k]]]; *)
]
]
];*)

(* The
triple For loop can be replaced conveniently by the \
expression below -- functional programming approach *)

ST = Flatten[Outer[Plus, S, -S]];
Print["ST: ", ST];

T = 0;
(* j should start at 1 not 0 *)
For[j = 1, j <= SS, j++, If[ST[[j]] < Tol2, T++]];
Print["T: ", T];
For[i = 1, i <= SampleSize, i++,
If[T > 2Tol + 2,
If[Status[[a[[i]]]] <=
5(* Here I have changed the test because the character was not
Status[[
a[[i]]]]++]; Wealth[[a[[i]]]] = Wealth[[a[[i]]]] - Cost + Benefit;
If[Status[[a[[i]]]] <= -5 (* Here too I
have changed the test for the same reason as above *),
Status[[a[[i]]]]--
]
]
]}, {Iteration}]

Print["ST: ", ST]
Print["T: ", T]
Print["Final Wealth: ", Wealth]
Print["Final Status: ", Status]

Hope this helps,
Jean-Marc

```

• Prev by Date: Re: Problem with compiled function (is this a bug?)
• Next by Date: Re: ViewPoint selector
• Previous by thread: Re: Code problem