Created
February 23, 2011 15:00
-
-
Save rafapolo/840512 to your computer and use it in GitHub Desktop.
decompiled AXELROD'S MODEL FOR DISSEMINATION OF CULTURE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Decompiled by Jad v1.5.8c. Copyright 2001 Pavel Kouznetsov. | |
// Jad home page: http://www.geocities.com/kpdus/jad.html | |
// Decompiler options: packimports(3) | |
// Source File Name: culture.java | |
import java.applet.Applet; | |
import java.awt.*; | |
import java.text.DecimalFormat; | |
import java.text.NumberFormat; | |
public class culture extends Applet | |
implements Runnable | |
{ | |
public double ran0() | |
{ | |
int i = rannum / 0x1f31d; | |
rannum = 16807 * (rannum - i * 0x1f31d) - 2836 * i; | |
if(rannum < 0) | |
rannum += 0x7fffffff; | |
double d = 4.6566128752457969E-10D * (double)rannum; | |
return d; | |
} | |
public void init() | |
{ | |
read_input_parameters(); | |
setLayout(null); | |
setBackground(Color.white); | |
LP_B = new Button("+"); | |
LP_B.reshape(400, 10, 20, 20); | |
add(LP_B); | |
LM_B = new Button("-"); | |
LM_B.reshape(425, 10, 20, 20); | |
add(LM_B); | |
FP_B = new Button("+"); | |
FP_B.reshape(400, 40, 20, 20); | |
add(FP_B); | |
FM_B = new Button("-"); | |
FM_B.reshape(425, 40, 20, 20); | |
add(FM_B); | |
QP_B = new Button("+"); | |
QP_B.reshape(400, 70, 20, 20); | |
add(QP_B); | |
QM_B = new Button("-"); | |
QM_B.reshape(425, 70, 20, 20); | |
add(QM_B); | |
RP_B = new Button("+"); | |
RP_B.reshape(400, 100, 20, 20); | |
add(RP_B); | |
RM_B = new Button("-"); | |
RM_B.reshape(425, 100, 20, 20); | |
add(RM_B); | |
PP_B = new Button("+"); | |
PP_B.reshape(400, 130, 20, 20); | |
add(PP_B); | |
PM_B = new Button("-"); | |
PM_B.reshape(425, 130, 20, 20); | |
add(PM_B); | |
stop_go_B = new Button("Stop/Go"); | |
stop_go_B.reshape(400, 200, 90, 20); | |
add(stop_go_B); | |
PERT_B = new Button("Perturbation"); | |
PERT_B.reshape(500, 200, 90, 20); | |
add(PERT_B); | |
INITH_B = new Button("homogeneous"); | |
INITH_B.reshape(400, 260, 90, 20); | |
add(INITH_B); | |
INITR_B = new Button("random"); | |
INITR_B.reshape(500, 260, 90, 20); | |
add(INITR_B); | |
INITB_B = new Button("bubble"); | |
INITB_B.reshape(400, 290, 90, 20); | |
add(INITB_B); | |
CMODE_B = new Button("Colours"); | |
CMODE_B.reshape(400, 338, 58, 20); | |
add(CMODE_B); | |
BMODE_B = new Button("Borders"); | |
BMODE_B.reshape(466, 338, 58, 20); | |
add(BMODE_B); | |
NMODE_B = new Button("Lattice"); | |
NMODE_B.reshape(532, 338, 58, 20); | |
add(NMODE_B); | |
FAST_B = new Button("Fast"); | |
FAST_B.reshape(400, 368, 58, 20); | |
add(FAST_B); | |
MEDIUM_B = new Button("Medium"); | |
MEDIUM_B.reshape(466, 368, 58, 20); | |
add(MEDIUM_B); | |
SLOW_B = new Button("Slow"); | |
SLOW_B.reshape(532, 368, 58, 20); | |
add(SLOW_B); | |
SEED_B = new Button("<"); | |
SEED_B.reshape(400, 400, 20, 20); | |
add(SEED_B); | |
colour_code[0] = 0; | |
colour_code[1] = 255; | |
colour_code[2] = 128; | |
int l = 64; | |
int j1 = 32; | |
for(int i = 3; i < 250; i++) | |
{ | |
colour_code[i] = j1; | |
j1 += l; | |
if(j1 > 255) | |
{ | |
l /= 2; | |
j1 = l / 2; | |
} | |
} | |
for(int j = 0; j < 16384; j++) | |
{ | |
for(int i1 = 0; i1 < 32; i1++) | |
s[j][i1] = 0; | |
} | |
make_lattice(); | |
if(flagstring.indexOf("o") != -1) | |
init_bubble(); | |
else | |
if(flagstring.indexOf("h") != -1) | |
init_homogeneous(); | |
else | |
init_random(); | |
} | |
public void read_input_parameters() | |
{ | |
String s1; | |
if((s1 = getParameter("L")) != null) | |
try | |
{ | |
r = Double.valueOf(s1).doubleValue(); | |
} | |
catch(NumberFormatException numberformatexception) { } | |
if(L != 8 && L != 16 && L != 32 && L != 64 && L != 128) | |
L = 32; | |
if((s1 = getParameter("F")) != null) | |
F = (int)Double.parseDouble(s1); | |
if(F < 2 || F > 32) | |
F = 3; | |
if((s1 = getParameter("q")) != null) | |
q = (int)Double.parseDouble(s1); | |
if(q < 2) | |
q = 2; | |
if((s1 = getParameter("r")) != null) | |
r = Double.parseDouble(s1); | |
if(r < 0.0D || r > 0.5D) | |
r = 0.0D; | |
if((s1 = getParameter("p")) != null) | |
p = Double.parseDouble(s1); | |
if(p < 0.0D || p > 0.5D) | |
p = 0.0D; | |
if((s1 = getParameter("seed")) != null) | |
ranseed = (int)Double.parseDouble(s1) - 1; | |
if(ranseed < 0) | |
ranseed = 0; | |
if((s1 = getParameter("slowmo")) != null) | |
slowmo = Double.parseDouble(s1); | |
if(slowmo < 0.0D || slowmo > 100000D) | |
slowmo = 0.0D; | |
flagstring = getParameter("flags"); | |
if(flagstring == null) | |
flagstring = "0"; | |
if(flagstring.indexOf("c") != -1) | |
gfx_mode = 0; | |
if(flagstring.indexOf("b") != -1) | |
gfx_mode = 1; | |
if(flagstring.indexOf("l") != -1) | |
gfx_mode = 2; | |
if(flagstring.indexOf("p") != -1) | |
pause = 1; | |
} | |
public void start() | |
{ | |
if(t == null) | |
{ | |
t = new Thread(this); | |
t.start(); | |
} | |
} | |
public void stop() | |
{ | |
t = null; | |
} | |
public void paint(Graphics g) | |
{ | |
draw_all(); | |
print(); | |
g.setColor(Color.black); | |
g.fillRect(400, 235, 190, 2); | |
g.drawString("Set initial configuration", 400, 250); | |
g.fillRect(400, 313, 190, 2); | |
g.drawString("Display", 400, 328); | |
} | |
void make_lattice() | |
{ | |
rannum = ranseed + 1; | |
k = 384 / L; | |
N = L * L; | |
for(int i = 0; i < N; i++) | |
{ | |
degree[i] = 0; | |
int l1 = i % L; | |
int i2 = i / L; | |
if(l1 > 0) | |
lt[i][degree[i]++] = (l1 - 1) + i2 * L; | |
if(l1 < L - 1) | |
lt[i][degree[i]++] = l1 + 1 + i2 * L; | |
if(i2 > 0) | |
lt[i][degree[i]++] = l1 + (i2 - 1) * L; | |
if(i2 < L - 1) | |
lt[i][degree[i]++] = l1 + (i2 + 1) * L; | |
} | |
for(int j = 0; j < N; j++) | |
{ | |
for(int l = 0; l < degree[j]; l++) | |
{ | |
if(ran0() >= p) | |
continue; | |
int k1 = lt[j][l]; | |
int i1; | |
for(i1 = 0; lt[k1][i1] != j; i1++); | |
lt[k1][i1] = lt[k1][--degree[k1]]; | |
boolean flag; | |
do | |
{ | |
k1 = (int)(ran0() * (double)N); | |
for(int j1 = 0; flag = false; j1 < degree[k1]; j1++) | |
if(lt[k1][j1] == j) | |
flag = true; | |
} while(k1 == j || flag); | |
lt[j][l] = k1; | |
lt[k1][degree[k1++]] = j; | |
} | |
} | |
} | |
public void init_random() | |
{ | |
rannum = ++ranseed + 1000; | |
for(int i = 0; i < N; i++) | |
{ | |
for(int j = 0; j < F; j++) | |
s[i][j] = (int)((double)q * ran0()); | |
} | |
update_alist(true); | |
otime = time = nnext = 0.0D; | |
} | |
public void init_homogeneous() | |
{ | |
rannum = ++ranseed + 1000; | |
for(int i = 0; i < N; i++) | |
{ | |
for(int j = 0; j < F; j++) | |
s[i][j] = 0; | |
} | |
update_alist(true); | |
otime = time = nnext = 0.0D; | |
} | |
public void init_bubble() | |
{ | |
rannum = ++ranseed + 1000; | |
for(int i = 0; i < N; i++) | |
{ | |
int i1 = i % L - L / 2; | |
int j1 = i / L - L / 2; | |
if(i1 * i1 + j1 * j1 < (L * L) / 16) | |
{ | |
for(int j = 0; j < F; j++) | |
s[i][j] = j != 0 ? 1 : 0; | |
continue; | |
} | |
for(int l = 0; l < F; l++) | |
s[i][l] = 0; | |
} | |
update_alist(true); | |
otime = time = nnext = 0.0D; | |
} | |
public void print() | |
{ | |
Graphics g = getGraphics(); | |
g.setColor(Color.white); | |
g.fillRect(450, 10, 150, 170); | |
g.fillRect(425, 400, 175, 20); | |
g.setColor(Color.black); | |
g.drawString("L = " + String.valueOf(L), 455, 25); | |
g.drawString("F = " + String.valueOf(F), 455, 55); | |
g.drawString("q = " + String.valueOf(q), 455, 85); | |
g.drawString("r = 0." + noiseFormatter.format((int)(r * 1000000D)), 455, 115); | |
g.drawString("p = 0." + noiseFormatter.format((int)(p * 1000000D)), 455, 145); | |
if(pause == 0) | |
g.drawString(" (running)", 500, 175); | |
else | |
g.drawString(act != 0 ? " (paused)" : " (frozen)", 500, 175); | |
g.drawString("seed = " + String.valueOf(ranseed), 425, 415); | |
print_time(); | |
} | |
public void print_time() | |
{ | |
Graphics g = getGraphics(); | |
g.setColor(Color.white); | |
g.fillRect(400, 160, 100, 20); | |
g.setColor(Color.black); | |
g.drawString("time = " + String.valueOf((long)time), 405, 175); | |
} | |
public void set_border_colour(int i, int j, Graphics g) | |
{ | |
int i1; | |
for(int l = i1 = 0; l < F; l++) | |
if(s[i][l] == s[j][l]) | |
i1++; | |
if(i1 == 0) | |
g.setColor(Color.black); | |
else | |
if(i1 != F) | |
g.setColor(Color.red); | |
else | |
g.setColor(Color.white); | |
} | |
public void draw_site(int i, Graphics g) | |
{ | |
int k1 = i % L; | |
int l1 = i / L; | |
if(gfx_mode == 0) | |
{ | |
if(F == 2) | |
g.setColor(new Color(colour_code[s[i][0]], colour_code[s[i][1]], 128)); | |
else | |
g.setColor(new Color(colour_code[s[i][0]], colour_code[s[i][1]], colour_code[s[i][2]])); | |
g.fillRect(2 + k1 * k, 2 + l1 * k, k, k); | |
} else | |
{ | |
if(k1 > 0) | |
{ | |
int j = i - 1; | |
set_border_colour(i, j, g); | |
g.fillRect((2 + k1 * k) - 1, 2 + l1 * k, 2, k); | |
} | |
if(l1 > 0) | |
{ | |
int l = i - L; | |
set_border_colour(i, l, g); | |
g.fillRect(2 + k1 * k, (2 + l1 * k) - 1, k, 2); | |
} | |
if(k1 < L - 1) | |
{ | |
int i1 = i + 1; | |
set_border_colour(i, i1, g); | |
g.fillRect((2 + (k1 + 1) * k) - 1, 2 + l1 * k, 2, k); | |
} | |
if(l1 < L - 1) | |
{ | |
int j1 = i + L; | |
set_border_colour(i, j1, g); | |
g.fillRect(2 + k1 * k, (2 + (l1 + 1) * k) - 1, k, 2); | |
} | |
} | |
} | |
public void draw_all() | |
{ | |
Graphics g = getGraphics(); | |
g.setColor(Color.white); | |
g.fillRect(0, 0, k * L + 4, k * L + 4); | |
g.setColor(Color.black); | |
g.fillRect(0, 0, 2, k * L + 4); | |
g.fillRect(0, 0, k * L + 4, 2); | |
g.fillRect(2 + k * L, 0, 2, k * L + 4); | |
g.fillRect(0, 2 + k * L, k * L + 4, 2); | |
if(gfx_mode != 2) | |
{ | |
for(int i = 0; i < N; i++) | |
draw_site(i, g); | |
} else | |
{ | |
g.setColor(Color.blue); | |
for(int j = 0; j < N; j++) | |
{ | |
for(int l = 0; l < degree[j]; l++) | |
{ | |
int i2 = lt[j][l]; | |
int i1 = j % L; | |
int j1 = j / L; | |
int k1 = i2 % L; | |
int l1 = i2 / L; | |
g.drawLine(2 + k / 2 + k * i1, 2 + k / 2 + k * j1, 2 + k / 2 + k * k1, 2 + k / 2 + k * l1); | |
} | |
} | |
} | |
print(); | |
} | |
public boolean action(Event event, Object obj) | |
{ | |
recent_target = event.target; | |
start(); | |
return true; | |
} | |
public void handle_event() | |
{ | |
if(recent_target == FP_B && F < 32) | |
{ | |
F++; | |
update_alist(true); | |
draw_all(); | |
} | |
if(recent_target == FM_B && F > 2) | |
{ | |
F--; | |
update_alist(true); | |
draw_all(); | |
} | |
if(recent_target == QP_B) | |
{ | |
q++; | |
print(); | |
} | |
if(recent_target == QM_B && q > 2) | |
{ | |
q--; | |
print(); | |
} | |
if(recent_target == RP_B && r < 0.5D) | |
{ | |
if(r == 0.0D) | |
r = 9.9999999999999995E-07D; | |
else | |
r *= 1.7782794200000001D; | |
nnext = time; | |
print(); | |
} | |
if(recent_target == RM_B) | |
{ | |
if((r /= 1.7782794200000001D) < 9.9999999999999995E-07D) | |
r = 0.0D; | |
print(); | |
} | |
if(recent_target == PP_B && p < 0.5D) | |
{ | |
if(p == 0.0D) | |
p = 9.9999999999999995E-07D; | |
else | |
p *= 1.7782794200000001D; | |
make_lattice(); | |
update_alist(true); | |
draw_all(); | |
} | |
if(recent_target == PM_B) | |
{ | |
if((p /= 1.7782794200000001D) < 9.9999999999999995E-07D) | |
p = 0.0D; | |
make_lattice(); | |
update_alist(true); | |
draw_all(); | |
} | |
if(recent_target == LP_B && L < 128) | |
{ | |
L *= 2; | |
make_lattice(); | |
for(int l = L - 1; l >= 0; l--) | |
{ | |
for(int i = L - 1; i >= 0; i--) | |
{ | |
for(int j1 = 0; j1 < F; j1++) | |
s[l * L + i][j1] = s[(l / 2) * (L / 2) + i / 2][j1]; | |
} | |
} | |
update_alist(true); | |
draw_all(); | |
} | |
if(recent_target == LM_B && L > 8) | |
{ | |
L /= 2; | |
make_lattice(); | |
for(int i1 = 0; i1 < L; i1++) | |
{ | |
for(int j = 0; j < L; j++) | |
{ | |
for(int k1 = 0; k1 < F; k1++) | |
s[i1 * L + j][k1] = s[i1 * L * 4 + j * 2][k1]; | |
} | |
} | |
update_alist(true); | |
draw_all(); | |
} | |
if(recent_target == INITR_B) | |
{ | |
init_random(); | |
draw_all(); | |
} | |
if(recent_target == INITH_B) | |
{ | |
init_homogeneous(); | |
draw_all(); | |
} | |
if(recent_target == INITB_B) | |
{ | |
init_bubble(); | |
draw_all(); | |
} | |
if(recent_target == stop_go_B) | |
{ | |
pause = 1 - pause; | |
if(pause == 0) | |
stime = System.currentTimeMillis(); | |
print(); | |
} | |
if(recent_target == PERT_B) | |
{ | |
perturbation(); | |
update_alist(false); | |
draw_all(); | |
} | |
if(recent_target == CMODE_B) | |
{ | |
gfx_mode = 0; | |
draw_all(); | |
} | |
if(recent_target == BMODE_B) | |
{ | |
gfx_mode = 1; | |
draw_all(); | |
} | |
if(recent_target == NMODE_B) | |
{ | |
gfx_mode = 2; | |
draw_all(); | |
} | |
if(recent_target == FAST_B) | |
slowmo = 0.0D; | |
if(recent_target == MEDIUM_B) | |
slowmo = 100D; | |
if(recent_target == SLOW_B) | |
slowmo = 1000D; | |
if(recent_target == SEED_B && ranseed > 0) | |
{ | |
ranseed--; | |
print(); | |
} | |
recent_target = null; | |
} | |
public void update_alist(boolean flag) | |
{ | |
act = 0; | |
for(int i = 0; i < N; i++) | |
{ | |
if(flag || a[i] != 0) | |
{ | |
a[i] = 0; | |
int j = 0; | |
do | |
{ | |
if(j >= degree[i]) | |
break; | |
int i1 = 0; | |
int j1 = lt[i][j]; | |
for(int l = 0; l < F; l++) | |
if(s[i][l] == s[j1][l]) | |
i1++; | |
if(i1 != 0 && i1 < F) | |
{ | |
a[i] = 1; | |
break; | |
} | |
j++; | |
} while(true); | |
} | |
if(a[i] != 0) | |
ali[act++] = i; | |
} | |
} | |
public int perturbation() | |
{ | |
int i = (int)(ran0() * (double)N); | |
int j = (int)(ran0() * (double)F); | |
s[i][j] = (int)(ran0() * (double)q); | |
for(int l = 0; l < degree[i]; l++) | |
{ | |
int i1; | |
if(a[i1 = lt[i][l]] == 0) | |
{ | |
a[i1] = 1; | |
ali[act++] = i1; | |
} | |
} | |
if(a[i] == 0) | |
{ | |
a[i] = 1; | |
ali[act++] = i; | |
} | |
return i; | |
} | |
public void run() | |
{ | |
Graphics g = getGraphics(); | |
print(); | |
int l2 = 0; | |
int i3 = 10 * F; | |
label0: | |
for(; t != null; otime = time) | |
{ | |
if(recent_target != null) | |
handle_event(); | |
if(pause == 0) | |
{ | |
for(; nnext <= time && r != 0.0D; nnext -= Math.log(ran0()) / r / (double)N) | |
if(gfx_mode != 2) | |
draw_site(perturbation(), g); | |
else | |
perturbation(); | |
if(act != 0) | |
{ | |
time += 1.0D / (double)act; | |
int i = (int)(ran0() * (double)act); | |
int i2 = ali[i]; | |
int j = (int)(ran0() * (double)degree[i2]); | |
int j1 = (int)(ran0() * (double)F); | |
int j2; | |
if(s[i2][j1] == s[j2 = lt[i2][j]][j1]) | |
{ | |
int l; | |
for(int k1 = l = 0; k1 < F; k1++) | |
if(s[i2][k1] != s[j2][k1]) | |
agrl[l++] = k1; | |
if(l != 0) | |
{ | |
l = (int)(ran0() * (double)l); | |
int l1 = agrl[l]; | |
s[i2][l1] = s[j2][l1]; | |
if(gfx_mode != 2) | |
draw_site(i2, g); | |
for(int i1 = 0; i1 < degree[i2]; i1++) | |
{ | |
int k2; | |
if(a[k2 = lt[i2][i1]] == 0) | |
{ | |
a[k2] = 1; | |
ali[act++] = k2; | |
} | |
} | |
l2 = 0; | |
} | |
} | |
if(l2++ > i3) | |
{ | |
update_alist(false); | |
l2 = 0; | |
} | |
} | |
} else | |
{ | |
t = null; | |
} | |
if(act == 0) | |
{ | |
if(r > 0.0D) | |
{ | |
time = nnext - time <= 1.0D ? nnext : time + 1.0D; | |
} else | |
{ | |
pause = 1; | |
print(); | |
} | |
print_time(); | |
} | |
if((int)otime != (int)time) | |
print_time(); | |
if(slowmo <= 0.0D) | |
continue; | |
stime += (long)((time - otime) * slowmo); | |
if(Math.abs(stime - System.currentTimeMillis()) > 0L) | |
stime = System.currentTimeMillis(); | |
do | |
{ | |
if(System.currentTimeMillis() >= stime) | |
continue label0; | |
try | |
{ | |
Thread.sleep(0L); | |
} | |
catch(InterruptedException interruptedexception) { } | |
} while(true); | |
} | |
} | |
public culture() | |
{ | |
t = null; | |
s = new int[16384][32]; | |
lt = new int[16384][8]; | |
degree = new int[16384]; | |
a = new int[16384]; | |
ali = new int[16384]; | |
agrl = new int[32]; | |
L = 32; | |
N = L * L; | |
F = 3; | |
q = 15; | |
r = 0.0D; | |
p = 0.0D; | |
slowmo = 1.0D; | |
pause = 0; | |
gfx_mode = 1; | |
colour_code = new int[250]; | |
recent_target = null; | |
noiseFormatter = new DecimalFormat("000000"); | |
ranseed = 0; | |
} | |
final int L_MAX = 128; | |
final int N_MAX = 16384; | |
final int L_MIN = 8; | |
final int F_MAX = 32; | |
final int q_MAX = 250; | |
final int degree_max = 8; | |
Thread t; | |
int s[][]; | |
int lt[][]; | |
int degree[]; | |
int a[]; | |
int ali[]; | |
int act; | |
int agrl[]; | |
int L; | |
int N; | |
int F; | |
int q; | |
double r; | |
double p; | |
double time; | |
double nnext; | |
double slowmo; | |
double otime; | |
long stime; | |
int k; | |
int pause; | |
int gfx_mode; | |
int colour_code[]; | |
Object recent_target; | |
Button FP_B; | |
Button FM_B; | |
Button QP_B; | |
Button QM_B; | |
Button LP_B; | |
Button LM_B; | |
Button RP_B; | |
Button RM_B; | |
Button PP_B; | |
Button PM_B; | |
Button stop_go_B; | |
Button INITH_B; | |
Button INITR_B; | |
Button INITB_B; | |
Button PERT_B; | |
Button CMODE_B; | |
Button BMODE_B; | |
Button NMODE_B; | |
Button FAST_B; | |
Button MEDIUM_B; | |
Button SLOW_B; | |
Button SEED_B; | |
DecimalFormat noiseFormatter; | |
String flagstring; | |
int rannum; | |
int ranseed; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment