//* This is the simple model in which agents are either male of female and have an //ability in each of k areas they also have a tolerance for race. Each sorts into the profession */ //* that they like best */ // To compile type "javac tipS.java" at the command prompt // To run after compiling type "java tipS" import java.awt.*; import java.awt.event.*; import javax.swing.*; public class tipS extends JFrame { GridPanel news = new GridPanel(); public tipS() { super("tipS"); setSize(1100, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel pane = new JPanel(); pane.setLayout(new GridLayout(1,1,2,2)); pane.add(news); setContentPane(pane); show(); news.scroll(); } public static void main(String[] arguments) { tipS grid = new tipS(); } } class GridPanel extends JPanel { int profmax=20; //* number of professions*/ int popsize=1000; //* number of agents */ int iterations=50; int races=2; int trials=25; //* gender, race, choice of profession, tolerance for others, and ability */ int[] agGender=new int[popsize]; int[] agRace=new int[popsize]; double[] agTol=new double[popsize]; int[] agPro=new int[popsize]; double[][] agAbil=new double[popsize][profmax]; double[] gdist=new double[profmax]; double[][] rdist=new double[profmax][races]; double[] tdist=new double[profmax]; //* total number in each profession */ double[] pdist=new double[profmax]; //* performance distribution across professions */ //void initialize(); //* starts up all of the pops */ double nmrand(); /* random number generator */ //void choosefirst(); //* agents initiallly allocate to profession*/ //void rechoose(); //* play the pairwise games */ //void printout(int); //* prints out the agents */ int seed; int mt; int tries; int profs; double prop; //* proportion of majority in each profession */ double eff; //* total efficiency */ //* main part of the program */ void scroll(){ int t; int ty; int nv; int delay=500; seed = 7621; System.out.println("number of trials: " + trials); for (profs = 20; profs <=20; profs++) { eff = 0.0; prop = 0.0; System.out.println("number of professions: " + profs); System.out.println("number of agents: " + popsize); for (tries =0; tries < trials; tries++) { initialize(); choosefirst(); repaint(); System.out.println("Newly Initialized"); try{ Thread.sleep(delay*10); } catch (InterruptedException e) {}; for (mt= 0; mt < iterations; mt++) { rechoose(); repaint(); try{ Thread.sleep(delay); } catch (InterruptedException e) {}; } printout(0); } printout(2); } } // ends scroll public void paintComponent(Graphics comp) { int x,y,z,p; Graphics2D comp2D = (Graphics2D)comp; // comp2D.setColor(getBackground()); // comp2D.drawString("Percent Women", 10,500); comp2D.setColor(Color.white); for (p=0;p=trials-1)comp2D.setColor(Color.black); if (x==2)comp2D.setColor(Color.white); if (y==100)comp2D.setColor(Color.white); comp2D.fillRect((100+(p)*50+(x*4)),y*4,(100+(p)*50+(x+1)*4),(y+1)*4); } } } comp2D.setColor(Color.black); comp2D.drawString("100%", 20,10); comp2D.drawString(" 75%", 20,100); comp2D.drawString(" 50%", 20,200); comp2D.drawString(" 25%", 20,300); comp2D.drawString(" 0%", 20,400); comp2D.drawString("Percent Women", 10,450); comp2D.drawString("Profession", 500,450); comp2D.drawString("A", 100,420); comp2D.drawString("B", 150,420); comp2D.drawString("C", 200,420); comp2D.drawString("D", 250,420); comp2D.drawString("E", 300,420); comp2D.drawString("F", 350,420); comp2D.drawString("G", 400,420); comp2D.drawString("H", 450,420); comp2D.drawString("I", 500,420); comp2D.drawString("J", 550,420); comp2D.drawString("K", 600,420); comp2D.drawString("L", 650,420); comp2D.drawString("M", 700,420); comp2D.drawString("N", 750,420); comp2D.drawString("O", 800,420); comp2D.drawString("P", 850,420); comp2D.drawString("Q", 900,420); comp2D.drawString("R", 950,420); comp2D.drawString("S", 1000,420); comp2D.drawString("T", 1050,420); } //* random number generator */ public double nmrand() { seed = (int)((seed * 25713 + 113849.0) % 65536.0); return ((double)seed / 65536.0); } //* randomly assigns values to all agents */ public void initialize() { int j,k; for (k = 0; k < popsize; k++) { agGender[k] = (int)(2.0*nmrand()); agRace[k] = (int)(races*nmrand()); agTol[k] = nmrand(); for (j = 0; j < profs; j++) { agAbil[k][j] = nmrand(); } } } //* sorts into first profession by ability */ public void choosefirst() { int i,j,jj,k,ch; double val; for (j = 0; j < profs; j++) { tdist[j] = 0.0; gdist[j] = 0.0; pdist[j] = 0.0; for (jj = 0; jj < races; jj++) { rdist[j][jj] = 0.0; rdist[j][jj] = 0.0; } } for (k = 0; k < popsize; k++) { val = -100.0; ch = 0; for (i = 0; i < profs; i++) { if (agAbil[k][i] > val) { ch = i; val = agAbil[k][i]; } } agPro[k] = ch; tdist[ch] = tdist[ch]+1.0; if (agGender[k] == 1) {gdist[ch] += 1.0;} rdist[ch][agRace[k]] = rdist[ch][agRace[k]]+1.0; pdist[ch] = pdist[ch] + agAbil[k][ch]; } } //* sorts into profession by ability, tolerance, and profession makeup*/ public void rechoose() { int i,k,old,ch; double rn=0.0; double val; double temp2=0.0; double temp,climate; /* equals one if the ratio is above your threshold 1-difference if it is not */ double totmove; totmove = 0.0; for (k = 0; k < popsize; k++) { rn=nmrand(); if (rn>.75){ old = agPro[k]; ch = old; temp = gdist[old]/tdist[old]; //* percentage of men in the profession */ if (agGender[k] == 1) { temp2 = agTol[k] + (1-agTol[k])*temp; } if (agGender[k] == 0) { temp2 = agTol[k] + (1-agTol[k])*(1-temp); } val = temp2*agAbil[k][old]; for (i = 0; i < profs; i++) { temp = 1.0*gdist[i]/tdist[i]; //* percentage of men in the profession */ if (agGender[k] == 1) { temp2 = agTol[k] + (1-agTol[k])*temp; } if (agGender[k] == 0) { temp2 = agTol[k] + (1-agTol[k])*(1-temp); } if (temp2*agAbil[k][i] > val) { ch = i; val = agAbil[k][i]; } } agPro[k] = ch; tdist[ch] += 1; tdist[old] -=1; if (agGender[k] == 1){ gdist[ch] += 1.0;} if (agGender[k] == 1){ gdist[old] -= 1.0;} rdist[ch][agRace[k]] += 1.0; rdist[old][agRace[k]] -= 1.0; pdist[ch] += agAbil[k][ch]; pdist[old] -= agAbil[k][old]; if (old != ch) {totmove += 1.0;} } //end if random > x } //* printf(" it %5d moves %7.2f\n", mt,totmove); */ } //* prints out relevant data */ public void printout(int k){ int i; double r1,r2; if (k > 1){ System.out.println(" profession ability gender race ");} for (i = 0; i < profs; i++) { eff += pdist[i]; r1 = gdist[i]/tdist[i]; if (r1 > 0.5) {prop += r1;} else{prop += (1-r1);} r2 = rdist[i][0]/tdist[i]; if ( k > 1) {System.out.println(i + " " + tdist[i] + "\t" + pdist[i] + "\t" + r1 + "\t" +r2);} } if (k > 1) System.out.println("total efficiency: " + eff/trials); if (k > 1) System.out.println("segregation proportion: " + prop/(profs*trials)); } } //ends "public class Tips"