{pr03.p Sun Mar 10 06:05:18 PST 2002} program mts3(input,output); {const2 Sat Mar 9 17:06:23 PST 2002} const n = 156; minindex = -468; {must be at most -3*n} maxindex = 468; {must be at least 3*n} three = 3; accuracy = 0.0001; {competitiveness = 2.5276;} maxboost = 100; maxiter = 20; pixel = 4; width = 20.0; big = 20; inithicomp = 3; initlocomp = 2; {type Sat Mar 9 17:06:23 PST 2002} type wfreal = array[minindex..maxindex,minindex..maxindex]of real; wfboolean = array[minindex..maxindex,minindex..maxindex]of boolean; {glbls3 Sat Mar 9 14:33:37 PST 2002} var sharpA,sharpB,sharpC:wfreal; hicomp,locomp,competitiveness:real; procedure initglobals; begin if (minindex > -three*n) or (maxindex < three*n) then begin writeln('Constants out of bounds; halt.'); halt; end; hicomp := inithicomp; locomp := initlocomp; competitiveness := hicomp; end; {initglobals} procedure initsharps; var i,j:integer; begin for i := minindex to maxindex do for j := minindex to maxindex do begin sharpA[i,j] := -big; sharpB[i,j] := -big; sharpC[i,j] := -big; end; sharpA[-2*n,n] := 0; sharpB[n,-2*n] := 0; sharpC[n,n] := 0; end; {initsharps} {updts2 Sat Mar 9 03:28:52 PST 2002} function inrange(i,j:integer):boolean; begin if (i-j <> (i-j)div three * three) then inrange := false else if abs(i-j) > three*n then inrange := false else if abs(2*i+j) > three*n then inrange := false else if abs(2*j+i) > three*n then inrange := false else inrange := true; end; {inrange} function maxA(i,j:integer):boolean; begin if (i-j = three*n) or (2*i+j = three*n) then maxA := true else maxA := false; end; {maxA} function maxB(i,j:integer):boolean; begin if (j-i = three*n) or (2*j+i = three*n) then maxB := true else maxB := false; end; {maxB} function maxC(i,j:integer):boolean; begin if (-2*i-j = three*n) or (-2*j-i = three*n) then maxC := true else maxC := false; end; {maxC} function newsharpA(i,j:integer):real; {input condition: inrange(i,j)} begin if maxA(i,j) then newsharpA := (sharpB[i,j]+sharpC[i,j]+three*n)/2 else newsharpA := sharpA[i+2,j-1] + 1 - competitiveness + (sharpB[i+2,j-1]+sharpC[i+2,j-1]-2*sharpA[i+2,j-1])/(three*n) end; {newsharpA} function newsharpB(i,j:integer):real; {input condition: inrange(i,j)} begin if maxB(i,j) then newsharpB := (sharpA[i,j]+sharpC[i,j]+three*n)/2 else newsharpB := sharpB[i-1,j+2] + 1 - competitiveness + (sharpA[i-1,j+2]+sharpC[i-1,j+2]-2*sharpB[i-1,j+2])/(three*n) end; {newsharpB} function newsharpC(i,j:integer):real; {input condition: inrange(i,j)} begin if maxC(i,j) then newsharpC := (sharpA[i,j]+sharpB[i,j]+three*n)/2 else newsharpC := sharpC[i-1,j-1] + 1 - competitiveness + (sharpA[i-1,j-1]+sharpB[i-1,j-1]-2*sharpC[i-1,j-1])/(three*n) end; {newsharpC} function updatedA(i,j:integer):boolean; var temp:real; begin temp := newsharpA(i,j); updatedA := temp - sharpA[i,j] > accuracy; if temp > sharpA[i,j] then sharpA[i,j] := temp; end; {updatedA} function updatedB(i,j:integer):boolean; var temp:real; begin temp := newsharpB(i,j); updatedB := temp - sharpB[i,j] > accuracy; if temp > sharpB[i,j] then sharpB[i,j] := temp; end; {updatedB} function updatedC(i,j:integer):boolean; var temp:real; begin temp := newsharpC(i,j); updatedC := temp - sharpC[i,j] > accuracy; if temp > sharpC[i,j] then sharpC[i,j] := temp; end; {updatedC} function updatedABC(i,j:integer):boolean; var result:boolean; begin result := updatedA(i,j); if updatedB(i,j) then result := true; if updatedC(i,j) then result := true; updatedABC := result; end; {updatedABC} function boosted:boolean; var i,j:integer; result:boolean; begin result := false; for i := -three*n to three*n do for j := -three*n to three*n do if inrange(i,j) then if updatedABC(i,j) then result := true; for i := three*n downto -three*n do for j := -three*n to three*n do if inrange(i,j) then if updatedABC(i,j) then result := true; for i := -three*n to three*n do for j := three*n downto -three*n do if inrange(i,j) then if updatedABC(i,j) then result := true; for i := three*n downto -three*n do for j := three*n downto -three*n do if inrange(i,j) then if updatedABC(i,j) then result := true; boosted := result; end; {boosted} { function diverges:boolean; var i,j:integer; result:boolean; begin result := true; for i := -three*n to three*n do for j := -three*n to three*n do if inrange(i,j) then if sharpA[i,j] < accuracy then result := false else if sharpB[i,j] < accuracy then result := false else if sharpC[i,j] < accuracy then result := false; diverges := result; end; diverges} function diverges:boolean; begin diverges := sharpA[-2*n,n] > accuracy; end; {diverges} {outs2 Sun Mar 10 05:53:45 PST 2002} procedure writesharp; var i,j,k:integer; begin for i := -three*n to three*n do for j := -three*n to three*n do if inrange(i,j) then begin k := -i-j; write('(',i:2,',',j:2,',',k:2,')#A = ');write(sharpA[i,j]:5:2); write(' (',i:2,',',j:2,',',k:2,')#B = ');write(sharpB[i,j]:5:2); write(' (',i:2,',',j:2,',',k:2,')#C = ');writeln(sharpC[i,j]:5:2); end; end; {writesharp} function chrcode(x:real):char; begin if abs(x) < accuracy then chrcode := 'A' else if x > 0 then chrcode := chr(ord('B')+trunc(x/width)) else chrcode := chr(ord('b')+trunc((-x)/width)) end; {chrcode} function probcode(x:real):char; const divisor = 24; begin if x < 0 then probcode := '-' else if x > 1 then probcode := '+' else if x < accuracy then probcode := 'a' else if x > 1-accuracy then probcode := 'z' else probcode := chr(trunc(divisor*x)+ord('b')); end; {probcode} function minsharpA:real; var i,j:integer; min:real; begin min := big; for i := -three*n to three*n do for j := -three*n to three*n do if inrange(i,j) then if sharpA[i,j] < min then min := sharpA[i,j]; minsharpA := min; end; {minsharpA} function assymetry:real; var i,j:integer; result,d1,d2,d3:real; begin result := 0; for i := -three*n to three*n do for j := -three*n to three*n do if inrange(i,j) then begin d1 := abs(sharpA[i,j]-sharpA[i,-i-j]); d2 := abs(sharpA[i,j]-sharpB[j,i]); d3 := abs(sharpA[i,j]-sharpC[-i-j,j]); if d1 > result then result := d1; if d2 > result then result := d2; if d3 > result then result := d3; end; assymetry := result; end; {assymetry} procedure trapezoid; var u,v:integer; s:real; begin u := 0; while u <= n do begin v := 0; while v <= u + n do begin s := sharpA[-n-u+2*v,-n+2*u-v]; write(chrcode(s)); v := v + pixel; end; writeln; u := u + pixel; end; end; {trapezoid} procedure hexagon; var u,v:integer; s:real; begin u := 0; while u <= 2*n do begin v := 0; while (v <= u + n) and (v <= 2*n) do begin s := sharpA[-n-u+2*v,-n+2*u-v]; if u - v <= n then write(chrcode(s)) else write(' '); v := v+pixel; end; writeln; u := u+pixel; end; end; {hexagon} procedure hexprob; var u,v:integer; s:real; begin u := 0; while u <= 2*n do begin v := 0; while (v <= u + n) and (v <= 2*n) do begin s := (three*n - 2*sharpA[-n-u+2*v,-n+2*u-v] + sharpB[-n-u+2*v,-n+2*u-v] + sharpC[-n-u+2*v,-n+2*u-v])/(three*three*n); if u - v <= n then write(probcode(s)) else write(' '); v := v+pixel; end; writeln; u := u+pixel; end; end; {hexprob} procedure writehex(var h:wfreal); {automatically scales} var minval,maxval,s:real; i,j,u,v:integer; begin minval := h[0,0]; for i := -three*n to three*n do for j := -three*n to three*n do if inrange(i,j) then if h[i,j] < minval then minval := h[i,j]; maxval := h[0,0]; for i := -three*n to three*n do for j := -three*n to three*n do if inrange(i,j) then if h[i,j] > maxval then maxval := h[i,j]; writeln; write('Minval = ',minval:0:8); writeln(' Maxval = ',maxval:0:8); u := 0; if maxval > minval then while u <= 2*n do begin v := 0; while (v <= u + n) and (v <= 2*n) do begin if u - v <= n then write(probcode((h[-n-u+2*v,-n+2*u-v]-minval)/(maxval-minval))) else write(' '); v := v+pixel; end; writeln; u := u+pixel; end; end; {writehex} {pots Sun Mar 10 05:53:45 PST 2002} procedure potential(var pot:wfreal); var i,j:integer; begin for i := -three*n to three*n do for j := -three*n to three*n do if inrange(i,j) then pot[i,j] := (sharpA[i,j]+sharpB[i,j]+sharpC[i,j])/(three*three*n)-2/3; end; {potential} {tstr03 Sat Mar 9 17:06:23 PST 2002} procedure tester03; var iter,boo:integer; done:boolean; savehex:wfreal; begin initglobals; write('N = ',n:0); write(' Pixel = ',pixel:0); writeln(' Accuracy = ',accuracy:0:8); for iter := 1 to maxiter do begin competitiveness := (locomp+hicomp)/2; writeln('Competitiveness = ',competitiveness:0:8); initsharps; done := false; for boo := 1 to maxboost do if not done and not diverges then begin writeln('Iteration ',iter:3,' Boost ',boo:3);flush(output); done := not boosted; end; write('Iteration ',iter:3,' '); if done then write('Done') else write('Not done'); if diverges then writeln(' Diverges') else writeln(' Does not diverge'); if diverges then locomp := competitiveness else hicomp := competitiveness; if not diverges then savehex := sharpA; end; sharpA := savehex; competitiveness := hicomp; writeln('Best competitiveness found = ',competitiveness:0:8); writeln('Minsharp = ',minsharpA:0:2); write('Assymetry = ',assymetry:0:8); writeln(' Slack at base point = ',-newsharpA(-2*n,n):0:8); hexagon; writeln('If Stable Distribution Hypothesis holds then Probability of A:'); hexprob; potential(savehex); writeln;writeln('Potential at SD:'); writehex(savehex); writeln;writeln('sharpA:');writehex(sharpA); writeln;writeln('sharpB:');writehex(sharpB); writeln;writeln('sharpC:');writehex(sharpC); end; {tester03} begin tester03; end.