/* Ideology Simulator Written by: Shai Ophir Date: June 1998 Email: shaio@glx.chief.co.il Parameters for running the Ideology Simulator: >sim.exe "ev-non-linear" 250 "not-in-use" 0 1 Where sim.exe is the program itself "ev-non-linear" not currently in use, but needs to remain as is. 250 the number of years (can be 1-250) "not-in-use" curently can be any dummy string 0 not currently in use 1 the sequential number of the experiment As one can see,the only two parameters currently in use are the second and the last one. */ #include #include #include #include #include #include /* System routine definitions */ #include /* Library (LIB$) routine definitions */ #include /* Math function definitions */ #include /* Time function definitions */ #define MAX_AGENTS 800000 /* max number of agents */ #define NUM_OF_YEARS 250 /* number of years to run the simulation for */ #define INIT_AGENTS 1000 /* number of agents to initialize */ #define MAX_INIT_PROP 100 /* max initialized property */ #define ANNUAL_INCREASE 0 /* annual increase of the public property */ #define YEAR_PROP_DELTA 50 /* property delta per year (max) */ #define LOW_PROP_LIMIT 0 /* lowest property limit */ #define PUBLIC_LOAN 50 #define PRIVATE_LOAN 5 #define PUBLIC_PAYMENT 1 #define ANNUAL_BONUS 25 #define LINEAR_PROFIT_TALENT 1 #define NON_LINEAR_PROFIT_TALENT 2 #define NON_LINEAR_PROFIT_PROTAL 3 #define EV_LINEAR_PROFIT_TALENT 4 #define EV_NON_LINEAR_PROFIT_TALENT 5 #define EV_NON_LINEAR_PROFIT_PROTAL 6 /* Social types */ #define CAPITALIST 1 #define SOCIALIST 2 #define ALTRUIST 3 #define MARXIST 4 #define CAPITALIST2 5 #define SOCIALIST2 6 #define ALTRUIST2 7 #define MARXIST2 8 #define CAPITALIST3 9 #define SOCIALIST3 10 #define ALTRUIST3 11 #define MARXIST3 12 #define HIGHEST_TYPE 12 main (int argc, char *argv[]) { long i,j,k,l,idx; long rand_num; long next_child_idx; long actual_num_agents; long spouse; int num_childs; int talent_change; int age,die_age; int rand100, rand2; long property_change; long average_property; long public_property; long private_property; long private_pos_property; long private_neg_property; long private_zer_property; long private_pos_agents; long private_neg_agents; long private_zer_agents; int profit_mode; int num_of_years; long highest_prop; long lowest_prop; long range; long class_width; int class[20]; long class_prop[20]; long class_talent[20]; char* soc_mode; char* soc_mode2; long tax; int done; long created_soc_types[HIGHEST_TYPE+1]; long soc_types[HIGHEST_TYPE+1]; int next_soc_type=0; int amount,loan; time_t t; int soc_mode_i,soc_mode_n; int repeat_times; struct agent{ int alive; /* 1-alive, 0-dead/not exist */ int age; /* in years (cycles) */ long property; /* money/property */ int talent; /* talent for making money (-100 to 100)*/ int married; /* 1-married, 0-not married*/ long spouse; /* index of spouse */ int num_childs; /* no. of child (1-5). 0-no childs */ long child[5]; /* pointers to children */ long father; /* pointer to father */ long mother; /* pointer to mother */ int soc_type; /* social type of agent */ }; struct agent agent_array[MAX_AGENTS]; /* initialization */ /* loop over the agents */ j=0; while (j < MAX_AGENTS) { agent_array[j].alive=0; agent_array[j].age=0; agent_array[j].property=0; agent_array[j].talent=0; agent_array[j].married=0; agent_array[j].spouse=0; agent_array[j].num_childs=0; agent_array[j].child[0]=0; agent_array[j].child[1]=0; agent_array[j].child[2]=0; agent_array[j].child[3]=0; agent_array[j].child[4]=0; agent_array[j].father=0; agent_array[j].mother=0; agent_array[j].soc_type=0; j++; } /* start to create children after the first INIT_AGENTS agents */ next_child_idx = INIT_AGENTS; actual_num_agents = next_child_idx; public_property = INIT_AGENTS * MAX_INIT_PROP * 10; private_property = 0; private_pos_property = 0; private_neg_property = 0; private_zer_property = 0; private_pos_agents = 0; private_neg_agents = 0; private_zer_agents = 0; highest_prop = 0; lowest_prop = 0; i=1; while (i < HIGHEST_TYPE+1) { created_soc_types[i]=0; i++; } printf ("---------- INITIALIZATION ----------- \n"); printf ("MAX NUM OF AGENTS: %d\n",MAX_AGENTS); printf ("NUM OF INIT AGENTS: %d\n",INIT_AGENTS); printf ("AGENT MAX INIT PROP: %d\n",MAX_INIT_PROP); printf ("PUBLIC PROPERTY: %d\n",public_property); printf ("PRIVATE PROPERTY: %d\n",private_property); printf ("ANNUAL INCREASE(PUBLIC): %d\n",ANNUAL_INCREASE); printf ("LOW PROP LIMIT: %d\n",LOW_PROP_LIMIT); printf ("PUBLIC LOAN: %d\n",PUBLIC_LOAN); printf ("PRIVATE LOAN: %d\n",PRIVATE_LOAN); if (argc > 2) num_of_years = atoi(argv[2]); else num_of_years = NUM_OF_YEARS; printf ("NUM OF YEARS: %d\n", num_of_years); soc_mode = ""; if (argc > 3) soc_mode = argv[3]; printf ("SOC MODE: %s\n", soc_mode); soc_mode2 = ""; if (argc > 4) soc_mode2 = argv[4]; printf ("SOC MODE2: %s\n", soc_mode2); if (strcmp(soc_mode,"capitalist") == 0) { soc_mode_i = CAPITALIST; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"socialist") == 0) { soc_mode_i = SOCIALIST; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"altruist") == 0) { soc_mode_i = ALTRUIST; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"marxist") == 0) { soc_mode_i = MARXIST; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"capitalist2") == 0) { soc_mode_i = CAPITALIST2; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"socialist2") == 0) { soc_mode_i = SOCIALIST2; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"altruist2") == 0) { soc_mode_i = ALTRUIST2; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"marxist2") == 0) { soc_mode_i = MARXIST2; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"capitalist3") == 0) { soc_mode_i = CAPITALIST3; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"socialist3") == 0) { soc_mode_i = SOCIALIST3; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"altruist3") == 0) { soc_mode_i = ALTRUIST3; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"marxist3") == 0) { soc_mode_i = MARXIST3; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"CAPITALIST") == 0) { soc_mode_i = CAPITALIST; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"SOCIALIST") == 0) { soc_mode_i = SOCIALIST; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"ALTRUIST") == 0) { soc_mode_i = ALTRUIST; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"MARXIST") == 0) { soc_mode_i = MARXIST; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"CAPITALIST2") == 0) { soc_mode_i = CAPITALIST2; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"SOCIALIST2") == 0) { soc_mode_i = SOCIALIST2; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"ALTRUIST2") == 0) { soc_mode_i = ALTRUIST2; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"MARXIST2") == 0) { soc_mode_i = MARXIST2; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"CAPITALIST3") == 0) { soc_mode_i = CAPITALIST3; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"SOCIALIST3") == 0) { soc_mode_i = SOCIALIST3; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"ALTRUIST3") == 0) { soc_mode_i = ALTRUIST3; soc_mode_n = atoi (argv[4]); } if (strcmp(soc_mode,"MARXIST3") == 0) { soc_mode_i = MARXIST3; soc_mode_n = atoi (argv[4]); } printf (" >>> soc_mode_i: %d\n", soc_mode_i); printf (" >>> soc_mode_n: %d\n", soc_mode_n); repeat_times = soc_mode_n; j=0; while (j < INIT_AGENTS) { /* set alive indicator */ agent_array[j].alive = 1; /* get a random number from the system */ srand((unsigned) time(&t)); rand_num = rand(); /* set soc type (1 to HIGHEST_TYPE ) */ /* but first check current type*/ if (next_soc_type == soc_mode_i) { if (repeat_times > 0) { repeat_times--; } else { next_soc_type++; /* and re-initiate repeat_times */ repeat_times = soc_mode_n; } } else { next_soc_type++; } if (next_soc_type > HIGHEST_TYPE) next_soc_type=1; /* agent_array[j].soc_type = HIGHEST_TYPE + 1 - (rand_num - (rand_num/HIGHEST_TYPE * HIGHEST_TYPE) + 1); */ agent_array[j].soc_type = next_soc_type; created_soc_types[agent_array[j].soc_type]++; /* convert it to the range 0-100 (for age) */ /* at first, convert to the age 0-70, then add 30 so we won't have childs (less than 30) */ age = rand_num - (rand_num/70 * 70); age = age + 30; agent_array[j].age = age; /* init talent */ rand2 = rand_num - (rand_num/2 * 2); if ( rand2 == 0 ) { agent_array[j].talent = (-1) * age; } else { agent_array[j].talent = age; } /* init property */ agent_array[j].property = rand_num - (rand_num/MAX_INIT_PROP * MAX_INIT_PROP); /* START WITH POSITIVE VALUES ONLY*/ /*if (agent_array[j].talent < 0) agent_array[j].property = (-1) * agent_array[j].property; */ public_property = public_property - agent_array[j].property; /* init marriage */ /* Check if the agent needsd to get married */ if ((agent_array[j].married == 0) && (agent_array[j].age > 30)) { /* look for a spouse */ /* choose from the already initialized agents */ spouse =0; while ((spouse < j) && !((agent_array[spouse].alive == 1) && (agent_array[spouse].married == 0) && (agent_array[spouse].age > 30))) { spouse++; } if (spouse < j) { agent_array[j].married = 1; agent_array[j].spouse = spouse; agent_array[spouse].married = 1; agent_array[spouse].spouse = j; /* divide the property... */ agent_array[j].property = (agent_array[j].property + agent_array[spouse].property) /2; agent_array[spouse].property = agent_array[j].property; /* how many children they have? (0-5) */ num_childs = rand_num - (rand_num/6 * 6); agent_array[j].num_childs = num_childs; agent_array[spouse].num_childs = num_childs; /* create children (age 0-30) */ k=0; while (k < num_childs) { agent_array[j].child[k] = next_child_idx; agent_array[spouse].child[k] = next_child_idx; agent_array[next_child_idx].alive = 1; agent_array[next_child_idx].married = 0; age = rand_num - (rand_num/30 * 30); agent_array[next_child_idx].age = age; /* set soc type */ /* but first check current type*/ if (next_soc_type == soc_mode_i) { if (repeat_times > 0) { repeat_times--; } else { next_soc_type++; /* and re-initiate repeat_times */ repeat_times = soc_mode_n; } } else { next_soc_type++; } if (next_soc_type > HIGHEST_TYPE) next_soc_type=1; /* agent_array[next_child_idx].soc_type = HIGHEST_TYPE + 1 - (rand_num - (rand_num/HIGHEST_TYPE * HIGHEST_TYPE) + 1); */ agent_array[j].soc_type = next_soc_type; created_soc_types[agent_array[j].soc_type]++; /* divide the property between the parents and all childs */ agent_array[next_child_idx].property = (agent_array[j].property + agent_array[spouse].property) / (num_childs+2) ; /* init talent */ rand100 = rand_num - (rand_num/100 * 100); rand2 = rand_num - (rand_num/2 * 2); if ( rand2 == 0 ) { agent_array[j].talent = (-1) * rand100; } else { agent_array[j].talent = rand100; } /* make the new talent the average between the new child and his parents */ agent_array[next_child_idx].talent = (agent_array[next_child_idx].talent + agent_array[j].talent + agent_array[spouse].talent)/3; agent_array[next_child_idx].father = j; agent_array[next_child_idx].mother = spouse; next_child_idx++; if (next_child_idx == MAX_AGENTS) { printf ("*** NUMBER OF AGENTS EXCEEDED, PROGRAM STOPS ***\n"); sys$exit(3); } actual_num_agents++; k++; } /* update the parents property */ agent_array[j].property = agent_array[j].property - agent_array[next_child_idx-1].property * num_childs /2; agent_array[spouse].property = agent_array[spouse].property - agent_array[next_child_idx-1].property * num_childs /2; } } j++; } /* main loop - loop over the years */ i=0; while (i < num_of_years) { /* increase public property in a certain percent */ public_property = public_property + public_property * ANNUAL_INCREASE / 100; /* init private property. this information will be re-caldulated every year */ private_property=0; private_pos_property=0; private_neg_property=0; private_zer_property=0; private_pos_agents = 0; private_neg_agents = 0; private_zer_agents = 0; highest_prop = 0; lowest_prop = 0; /* loop over the agents */ j=0; while (j < MAX_AGENTS) { /* check if the agent is alive at all. if not, skip it */ if (agent_array[j].alive == 1) { /* get a random number from the system */ srand((unsigned) time(&t)); rand_num = rand(); /* ACTION 1: make or loose money. */ /* set profit mode according to the command line parameter */ if (strcmp ("linear", argv[1]) == 0) profit_mode = LINEAR_PROFIT_TALENT; if (strcmp ("non-linear", argv[1]) == 0) profit_mode = NON_LINEAR_PROFIT_TALENT; if (strcmp ("non-linear-protal", argv[1]) == 0) profit_mode = NON_LINEAR_PROFIT_PROTAL; if (strcmp ("ev-linear", argv[1]) == 0) profit_mode = LINEAR_PROFIT_TALENT; if (strcmp ("ev-non-linear", argv[1]) == 0) profit_mode = NON_LINEAR_PROFIT_TALENT; if (strcmp ("ev-non-linear-protal", argv[1]) == 0) profit_mode = NON_LINEAR_PROFIT_PROTAL; /* Every year the agent is able to increase/decrease his property. The change is a function of talent and/or the current property. The one who has more talent/property will make more money. Note that talent is in the range of (-100..100), therefore the change can be negative. There is no direct relation between the agents, only between an agent and the public property. */ /* First, update talent - it should not be fixed */ /* THIS IS MAYBE THE PLACE TO PUT EVOLUTION IN PLACE */ /* MEANWHILE - THE CHANGE WILL BE RANDOM */ talent_change = rand_num - (rand_num/10 * 10); if ( (talent_change > 2) && (talent_change < 8) ) { talent_change = (-1) * talent_change; } agent_array[j].talent = agent_array[j].talent + talent_change; switch(profit_mode) { case LINEAR_PROFIT_TALENT: /* the relation between talent and profit is linear */ property_change = agent_array[j].talent / 10 ; break; case NON_LINEAR_PROFIT_TALENT: /* the relation between talent and profit is non-linear */ /* the relation is geometrical (power of 2) */ property_change = pow(agent_array[j].talent,2)/MAX_INIT_PROP/10; break; case NON_LINEAR_PROFIT_PROTAL: /* the profit depends on profit and talent (non-linear) */ property_change = sqrt(abs(agent_array[j].property*10)) * agent_array[j].talent/100 ; break; } /* End of Case */ /* if talent is negative, and the change is positive, the change should be negative */ if ((agent_array[j].talent <0) && (property_change>0)) property_change = (-1) * property_change; /* pay taxes - 30% from profit */ /* only if negative */ if (property_change >0) { tax = property_change * 3 / 10; property_change = property_change - tax; public_property = public_property + tax; } /* soc2: increase change_property for the poors */ if (strcmp(soc_mode2,"soc2") == 0) if (property_change <= 0) property_change = property_change + abs(agent_array[j].property)/20; /* NOW handle the property change according to the social type of the agent */ /* initialize loan */ loan = PRIVATE_LOAN; switch(agent_array[j].soc_type) { case CAPITALIST: /* do nothing */ break; case SOCIALIST: /* if you have a negative change, find someone to help you (someone SOCIALIST) */ /* but do this only if your property is negative */ /* and only if he has more than you in general */ /* if (strcmp(soc_mode,"socialist") == 0) { amount = atoi (argv[4]); loan = PRIVATE_LOAN + amount; } */ if ((property_change <= 0) && (agent_array[j].property <= 0)) { /* loop over the agents (from this index up)*/ l=j+1; done=0; while ((l < actual_num_agents) && (done == 0)) { if ((agent_array[l].alive == 1) && (agent_array[l].soc_type == SOCIALIST) && (agent_array[l].property > loan) && (agent_array[l].property > agent_array[j].property)) { property_change = property_change + loan; agent_array[l].property = agent_array[l].property - loan; done =1; } l++; } } break; case MARXIST: /* find another MARXIST and share your property with him */ /* loop over the agents (from this index up)*/ l=j+1; done=0; while ((l < actual_num_agents) && (done == 0)) { if ((agent_array[l].alive == 1) && (agent_array[l].soc_type == MARXIST)) { average_property = (agent_array[j].property + agent_array[l].property)/2; property_change = average_property - agent_array[j].property; agent_array[l].property = average_property; done =1; } l++; } break; case ALTRUIST: /* if you have a positive change, find someone to help him (in this case the someone need not be an ALTRUIST...) */ /* give him some money */ /* if (strcmp(soc_mode,"altruist") == 0) { amount = atoi (argv[4]); loan = PRIVATE_LOAN + amount; } */ if (property_change > loan) { /* loop over the agents (from this index up)*/ l=j+1; done=0; while ((l < actual_num_agents) && (done == 0)) { if ((agent_array[l].alive == 1) && (agent_array[l].property <= 0)) { property_change = property_change - loan; agent_array[l].property = agent_array[l].property + loan; done =1; } l++; } } break; case CAPITALIST2: /* MORE EXTREME: find someone with a lower talent, and take some money from him */ /* THE VICTIM NEED NOT BE A CAPITALIST */ /* (SAME AS FOR ALTRUISTS) */ /* if (strcmp(soc_mode,"capitalist2") == 0) { amount = atoi (argv[4]); loan = PRIVATE_LOAN + amount; } */ /* loop over the agents (from this index up)*/ l=j+1; done=0; while ((l < actual_num_agents) && (done == 0)) { if ((agent_array[l].alive == 1) && (agent_array[l].talent < agent_array[j].talent)) { property_change = property_change + loan; agent_array[l].property = agent_array[l].property - loan; done =1; } l++; } break; case SOCIALIST2: /* MORE EXTREME: same as before, with A DOUBLE LOAN */ /* if (strcmp(soc_mode,"socialist2") == 0) { amount = atoi (argv[4]); loan = PRIVATE_LOAN + amount; } */ if ((property_change <= 0) && (agent_array[j].property <= 0)) { /* loop over the agents (from this index up)*/ l=j+1; done=0; while ((l < actual_num_agents) && (done == 0)) { if ((agent_array[l].alive == 1) && (agent_array[l].soc_type == SOCIALIST2) && (agent_array[l].property > 2*loan) && (agent_array[l].property > agent_array[j].property)) { property_change = property_change + 2*loan; agent_array[l].property = agent_array[l].property - 2*loan; done =1; } l++; } } break; case MARXIST2: /* MORE EXTREME: give 1/3 to the public */ /* loop over the agents (from this index up)*/ l=j+1; done=0; while ((l < actual_num_agents) && (done == 0)) { if ((agent_array[l].alive == 1) && (agent_array[l].soc_type == MARXIST2)) { average_property = (agent_array[j].property + agent_array[l].property)/2; property_change = average_property*2/3 - agent_array[j].property; agent_array[l].property = average_property*2/3; public_property = public_property + average_property*2/3; done =1; } l++; } break; case ALTRUIST2: /* MORE EXTREME: give half of your profit to the poor */ if (property_change > 0) { /* loop over the agents (from this index up)*/ l=j+1; done=0; while ((l < actual_num_agents) && (done == 0)) { if ((agent_array[l].alive == 1) && (agent_array[l].property <= 0)) { property_change = property_change/2; agent_array[l].property = agent_array[l].property + property_change; done =1; } l++; } } break; case CAPITALIST3: /* CAPITALIST3 will get money from the other only if he is REACHER and has more talent */ /* if (strcmp(soc_mode,"capitalist3") == 0) { amount = atoi (argv[4]); loan = PRIVATE_LOAN + amount; } */ /* loop over the agents (from this index up)*/ l=j+1; done=0; while ((l < actual_num_agents) && (done == 0)) { if ((agent_array[l].alive == 1) && (agent_array[l].property > agent_array[j].property) && (agent_array[l].talent > agent_array[j].talent)) { property_change = property_change + loan; agent_array[l].property = agent_array[l].property - loan; done =1; } l++; } break; case SOCIALIST3: /* SOCIALIST3 can have relations with anyone except a CAPITALIST */ /* if (strcmp(soc_mode,"socialist3") == 0) { amount = atoi (argv[4]); loan = PRIVATE_LOAN + amount; } */ if ((property_change <= 0) && (agent_array[j].property <= 0)) { /* loop over the agents (from this index up)*/ l=j+1; done=0; while ((l < actual_num_agents) && (done == 0)) { if ((agent_array[l].alive == 1) && (agent_array[l].soc_type != CAPITALIST) && (agent_array[l].soc_type != CAPITALIST2) && (agent_array[l].soc_type != CAPITALIST3) && (agent_array[l].property > loan) && (agent_array[l].property > agent_array[j].property)) { property_change = property_change + loan; agent_array[l].property = agent_array[l].property - loan; done =1; } l++; } } break; case MARXIST3: /* MARXIST3 can have relations with anybody */ /* THE REVOLUTION... */ /* this - in addition to giving 1/3 to the public */ /* loop over the agents (from this index up)*/ l=j+1; done=0; while ((l < actual_num_agents) && (done == 0)) { if (agent_array[l].alive == 1) { average_property = (agent_array[j].property + agent_array[l].property)/2; property_change = average_property*2/3 - agent_array[j].property; agent_array[l].property = average_property*2/3; public_property = public_property + average_property*2/3; done =1; } l++; } break; case ALTRUIST3: /* MORE EXTREME: give all your profit to the poor */ if (property_change > 0) { /* loop over the agents (from this index up)*/ l=j+1; done=0; while ((l < actual_num_agents) && (done == 0)) { if ((agent_array[l].alive == 1) && (agent_array[l].property <= 0)) { /*property_change = property_change/2;*/ agent_array[l].property = agent_array[l].property + property_change; property_change=0; done =1; } l++; } } break; } agent_array[j].property = agent_array[j].property + property_change; public_property = public_property - property_change; /* ACTION 2: check if the agent needs to get married. if so, look (randomally) for a mate. get married only if the mate has a similar property and/or a similar talent. in this case the properties of the couple will be divided to 2 */ if ((agent_array[j].married == 0) && (agent_array[j].age > 30)) { /* look for a spouse */ /* choose from the already initialized agents */ spouse =0; while ((spouse < j) && !((agent_array[spouse].alive == 1) && (agent_array[spouse].married == 0) && (agent_array[spouse].age > 30))) { spouse++; } if (spouse < j) { agent_array[j].married = 1; agent_array[j].spouse = spouse; agent_array[spouse].married = 1; agent_array[spouse].spouse = j; /* divide the property... */ agent_array[j].property = (agent_array[j].property + agent_array[spouse].property) /2; agent_array[spouse].property = agent_array[j].property; /* how many children they have? (0-5) */ num_childs = rand_num - (rand_num/6 * 6); agent_array[j].num_childs = num_childs; agent_array[spouse].num_childs = num_childs; /* create children (age 0-30) */ k=0; while (k < num_childs) { agent_array[j].child[k] = next_child_idx; agent_array[spouse].child[k] = next_child_idx; agent_array[next_child_idx].alive = 1; agent_array[next_child_idx].married = 0; age = rand_num - (rand_num/30 * 30); agent_array[next_child_idx].age = age; /* set soc type */ /* but first check current type*/ if (next_soc_type == soc_mode_i) { if (repeat_times > 0) { repeat_times--; } else { next_soc_type++; /* and re-initiate repeat_times */ repeat_times = soc_mode_n; } } else { next_soc_type++; } if (next_soc_type > HIGHEST_TYPE) next_soc_type=1; /* agent_array[next_child_idx].soc_type = HIGHEST_TYPE + 1 - (rand_num - (rand_num/HIGHEST_TYPE * HIGHEST_TYPE) + 1); */ agent_array[j].soc_type = next_soc_type; created_soc_types[agent_array[j].soc_type]++; /* divide the property between the parents and all childs */ agent_array[next_child_idx].property = (agent_array[j].property + agent_array[spouse].property) / (num_childs+2) ; /* init talent */ rand100 = rand_num - (rand_num/100 * 100); rand2 = rand_num - (rand_num/2 * 2); if ( rand2 == 0 ) { agent_array[j].talent = (-1) * rand100; } else { agent_array[j].talent = rand100; } /* make the new talent the average between the new child and his parents */ agent_array[next_child_idx].talent = (agent_array[next_child_idx].talent + agent_array[j].talent + agent_array[spouse].talent)/3; agent_array[next_child_idx].father = j; agent_array[next_child_idx].mother = spouse; next_child_idx++; if (next_child_idx == MAX_AGENTS) { printf ("*** NUMBER OF AGENTS EXCEEDED, PROGRAM STOPS ***\n"); sys$exit(3); } actual_num_agents++; k++; } /* update the parents property */ agent_array[j].property = agent_array[j].property - agent_array[next_child_idx-1].property * num_childs /2; agent_array[spouse].property = agent_array[spouse].property - agent_array[next_child_idx-1].property * num_childs /2; } } /* ACTION 3: check if the agent is to die. in this case, the property will be inheritted to the spouse. if spouse is dead, the money will be inheritted to the children. If there are no children - to the public */ /* at first, update age */ agent_array[j].age++; /* die age will be in the range of (50..100) */ die_age = rand_num - (rand_num/60 * 60); die_age = die_age + 40; /* another condition for death - if we are in EVolutionary mode and the profit is below a certain minimum */ if (((strcmp ("ev-linear", argv[1]) == 0) || (strcmp ("ev-non-linear", argv[1]) == 0) || (strcmp ("ev-non-linear-protal", argv[1]) == 0)) && (agent_array[j].property < LOW_PROP_LIMIT)) { if (strcmp(soc_mode,"soc1") == 0) { agent_array[j].property = agent_array[j].property + PUBLIC_LOAN; public_property = public_property - PUBLIC_LOAN; private_property = private_property + PUBLIC_LOAN; } else { die_age = agent_array[j].age -1; } } if ( agent_array[j].age > die_age ) { /* turn off life indicator */ agent_array[j].alive = 0; actual_num_agents--; /* divide the property */ spouse = agent_array[j].spouse; if (agent_array[spouse].alive ==1) { agent_array[spouse].property = agent_array[spouse].property + agent_array[j].property; } else { if (agent_array[j].num_childs > 0) { /* loop over the childs */ k=0; num_childs = agent_array[j].num_childs; while (k < num_childs) { idx = agent_array[j].child[k]; agent_array[idx].property = agent_array[idx].property + agent_array[j].property / num_childs ; k++; } } else /* no children */ { public_property = public_property + agent_array[j].property; } } } /* if agent still alive */ if (agent_array[j].alive == 1) { /* accumulate private property */ private_property = private_property + agent_array[j].property; if (agent_array[j].property > 0) { private_pos_property = private_pos_property + agent_array[j].property; private_pos_agents++; } else if (agent_array[j].property == 0) { private_zer_property = private_zer_property + agent_array[j].property; private_zer_agents++; } else { private_neg_property = private_neg_property + agent_array[j].property; private_neg_agents++; } /* update highest/lowest */ if (agent_array[j].property > highest_prop) { highest_prop = agent_array[j].property; /* printf ("******YEAR *******: %d\n",i); printf ("******HIGHEST prop *******: %d\n",agent_array[j].property); printf ("******HIGHEST agent *******: %d\n",j); */ } if (agent_array[j].property < lowest_prop) { lowest_prop = agent_array[j].property; /* printf ("******YEAR *******: %d\n",i); printf ("******LOWEST prop *******: %d\n",agent_array[j].property); printf ("******LOWEST agent *******: %d\n",j); */ } } } /* end HANDLING A LIVE AGENT */ j++; } /* end LOOP OVER THE AGENTS */ /* print statistics (per year) */ /* printf ("---------- Year: %d\n",i); printf ("Next child idx: %d\n",next_child_idx); printf ("Actual #agents: %d\n",actual_num_agents); printf ("Public property: %d\n",public_property); printf ("Private property: %d\n",private_property); printf ("Private positive property: %d\n",private_pos_property); printf ("Private positive #agents: %d\n",private_pos_agents); printf ("Private negative property: %d\n",private_neg_property); printf ("Private negative #agents: %d\n",private_neg_agents); printf ("Private zero property: %d\n",private_zer_property); printf ("Private zero #agents: %d\n",private_zer_agents); */ /* MID-TERM RESULTS */ /* PRINT EVERY 40 YEARS */ if ((i/40*40 == i) && (i > 0)) { long ii,jj; printf (" ******* MID-TERM STATISTICS for %s MODE ***** \n", argv[1]); printf ("Number of years: %d\n",i); range = highest_prop - lowest_prop; class_width = range/20; printf ("Higtest property of agent: %d\n",highest_prop); printf ("Lowest property of agent: %d\n",lowest_prop); printf ("Classd width: %d\n",class_width); ii=0; while (ii < 20) { class[ii]=0; class_prop[ii]=0; class_talent[ii]=0; ii++; } ii=1; while (ii < HIGHEST_TYPE+1) { soc_types[ii]=0; ii++; } /* loop over the agents */ jj=0; while (jj < MAX_AGENTS) { if (agent_array[jj].alive == 1) { ii = (agent_array[jj].property - lowest_prop) / class_width; if (ii>19) ii=19; if (ii<0) ii=0; class[ii]++; class_prop[ii] = class_prop[ii] + agent_array[jj].property; class_talent[ii] = class_talent[ii] + agent_array[jj].talent; soc_types[agent_array[jj].soc_type]++; } jj++; } printf ("\n\n\n"); printf ("\n\n *********** MID-TERM Social Types ********** \n"); printf ("\n\n\n"); ii=1; while (ii < HIGHEST_TYPE+1) { printf ("Type: %3d Created: %6d Actuals: %6d Percent: %5d Exper: %2d Year: %3d\n", ii, created_soc_types[ii], soc_types[ii], (soc_types[ii] * 100000) / created_soc_types[ii], atoi(argv[5]), i); ii++; } } i++; } /* end LOOP OVER THE YEARS */ printf ("\n\n\n"); printf (" ******* FINAL STATISTICS for %s MODE ***** \n", argv[1]); printf ("Number of years: %d\n",i); printf ("Number of live agents: %d\n",actual_num_agents); printf ("Public property: %d\n",public_property); printf ("Private property: %d\n",private_property); printf ("Private positive property: %d\n",private_pos_property); printf ("Number of positive agents: %d\n",private_pos_agents); printf ("Private negative property: %d\n",private_neg_property); printf ("Number of negative agents: %d\n",private_neg_agents); printf ("Private zero property: %d\n",private_zer_property); printf ("Private zero #agents: %d\n",private_zer_agents); printf ("\n\n\n"); range = highest_prop - lowest_prop; class_width = range/20; printf ("Higtest property of agent: %d\n",highest_prop); printf ("Lowest property of agent: %d\n",lowest_prop); printf ("Classd width: %d\n",class_width); i=0; while (i < 20) { class[i]=0; class_prop[i]=0; class_talent[i]=0; i++; } i=1; while (i < HIGHEST_TYPE+1) { soc_types[i]=0; i++; } /* loop over the agents */ j=0; while (j < MAX_AGENTS) { if (agent_array[j].alive == 1) { i = (agent_array[j].property - lowest_prop) / class_width; if (i>19) i=19; if (i<0) i=0; class[i]++; class_prop[i] = class_prop[i] + agent_array[j].property; class_talent[i] = class_talent[i] + agent_array[j].talent; soc_types[agent_array[j].soc_type]++; } j++; } printf ("\n\n\n"); printf ("\n\n *********** Social Types ********** \n"); printf ("\n\n\n"); i=1; while (i < HIGHEST_TYPE+1) { printf ("Type: %3d Created: %6d Actuals: %6d Percent: %5d Exper: %2d FINAL\n", i, created_soc_types[i], soc_types[i], (soc_types[i] * 100000) / created_soc_types[i], atoi(argv[5]) ); i++; } /* printf ("CAPITALIST created: %06 actuals: %06\n", created_soc_types[1], soc_types[1]); printf ("SOCIALIST created: %06 actuals: %06\n", created_soc_types[2], soc_types[2]); printf ("ALTRUIST created: %06 actuals: %06\n", created_soc_types[3], soc_types[3]); printf ("MARXIST created: %06 actuals: %06\n", created_soc_types[4], soc_types[4]); printf ("CAPITALIST2 created: %06 actuals: %06\n", created_soc_types[5], soc_types[5]); printf ("SOCIALIST2 created: %06 actuals: %06\n", created_soc_types[6], soc_types[6]); printf ("ALTRUIST2 created: %06 actuals: %06\n", created_soc_types[7], soc_types[7]); printf ("MARXIST2 created: %06 actuals: %06\n", created_soc_types[8], soc_types[8]); printf ("CAPITALIST3 created: %06 actuals: %06\n", created_soc_types[9], soc_types[9]); printf ("SOCIALIST3 created: %06 actuals: %06\n", created_soc_types[10], soc_types[10]); printf ("ALTRUIST3 created: %06 actuals: %06\n", created_soc_types[11], soc_types[11]); printf ("MARXIST3 created: %06 actuals: %06\n", created_soc_types[12], soc_types[12]); */ printf ("\n\n\n"); printf ("\n\n *********** Population per class ********** \n"); printf ("\n\n\n"); printf ("Highest class #agents: (%d to %d) %d\n", lowest_prop+class_width*20,lowest_prop+class_width*19,class[19]); printf ("Second class #agents: (%d to %d) %d\n", lowest_prop+class_width*19,lowest_prop+class_width*18,class[18]); printf ("Third class #agents: (%d to %d) %d\n", lowest_prop+class_width*18,lowest_prop+class_width*17,class[17]); printf ("Forth class (%d to %d) %d\n", lowest_prop+class_width*17,lowest_prop+class_width*16,class[16]); printf ("Fifth class #agents: (%d to %d) %d\n", lowest_prop+class_width*16,lowest_prop+class_width*15,class[15]); printf ("Sixth class #agents: (%d to %d) %d\n", lowest_prop+class_width*15,lowest_prop+class_width*14,class[14]); printf ("Seventh class #agents: (%d to %d) %d\n", lowest_prop+class_width*14,lowest_prop+class_width*13,class[13]); printf ("Eighth class #agents: (%d to %d) %d\n", lowest_prop+class_width*13,lowest_prop+class_width*12,class[12]); printf ("Ninth class #agents: (%d to %d) %d\n", lowest_prop+class_width*12,lowest_prop+class_width*11,class[11]); printf ("10 class #agents (%d to %d) %d\n", lowest_prop+class_width*11,lowest_prop+class_width*10,class[10]); printf ("11 class #agents: (%d to %d) %d\n", lowest_prop+class_width*10,lowest_prop+class_width*9,class[9]); printf ("12 class #agents: (%d to %d) %d\n", lowest_prop+class_width*9,lowest_prop+class_width*8,class[8]); printf ("13 class #agents: (%d to %d) %d\n", lowest_prop+class_width*8,lowest_prop+class_width*7,class[7]); printf ("14 class #agents: (%d to %d) %d\n", lowest_prop+class_width*7,lowest_prop+class_width*6,class[6]); printf ("15 class #agents: (%d to %d) %d\n", lowest_prop+class_width*6,lowest_prop+class_width*5,class[5]); printf ("16 class #agents: (%d to %d) %d\n", lowest_prop+class_width*5,lowest_prop+class_width*4,class[4]); printf ("17 class #agents: (%d to %d) %d\n", lowest_prop+class_width*4,lowest_prop+class_width*3,class[3]); printf ("18 class #agents: (%d to %d) %d\n", lowest_prop+class_width*3,lowest_prop+class_width*2,class[2]); printf ("19 class #agents: (%d to %d) %d\n", lowest_prop+class_width*2,lowest_prop+class_width*1,class[1]); printf ("20 class #agents: (%d to %d) %d\n", lowest_prop+class_width*1,lowest_prop+class_width*0,class[0]); printf ("\n\n\n"); printf ("\n\n *********** Average property of agent ********** \n"); printf ("\n\n\n"); printf ("Total average: %d\n",private_property/actual_num_agents); printf ("\n\n"); if (class[19] != 0) printf ("Highest class average property: %d\n",class_prop[19]/class[19]); if (class[18] != 0) printf ("Second class average property: %d\n",class_prop[18]/class[18]); if (class[17] != 0) printf ("Third class average property: %d\n",class_prop[17]/class[17]); if (class[16] != 0) printf ("Forth class average property: %d\n",class_prop[16]/class[16]); if (class[15] != 0) printf ("Fifth class average property: %d\n",class_prop[15]/class[15]); if (class[14] != 0) printf ("Sixth class average property: %d\n",class_prop[14]/class[14]); if (class[13] != 0) printf ("Seventh class average property: %d\n",class_prop[13]/class[13]); if (class[12] != 0) printf ("Eighth class average property: %d\n",class_prop[12]/class[12]); if (class[11] != 0) printf ("Ninth class average property: %d\n",class_prop[11]/class[11]); if (class[10] != 0) printf ("10 class average property: %d\n",class_prop[10]/class[10]); if (class[9] != 0) printf ("11 class average property: %d\n",class_prop[9]/class[9]); if (class[8] != 0) printf ("12 class average property: %d\n",class_prop[8]/class[8]); if (class[7] != 0) printf ("13 class average property: %d\n",class_prop[7]/class[7]); if (class[6] != 0) printf ("14 class average property: %d\n",class_prop[6]/class[6]); if (class[5] != 0) printf ("15 class average property: %d\n",class_prop[5]/class[5]); if (class[4] != 0) printf ("16 class average property: %d\n",class_prop[4]/class[4]); if (class[3] != 0) printf ("17 class average property: %d\n",class_prop[3]/class[3]); if (class[2] != 0) printf ("18 class average property: %d\n",class_prop[2]/class[2]); if (class[1] != 0) printf ("19 class average property: %d\n",class_prop[1]/class[1]); if (class[0] != 0) printf ("20 class average property: %d\n",class_prop[0]/class[0]); printf ("\n\n\n"); printf ("\n\n *********** Average talent of agent ********** \n"); printf ("\n\n\n"); if (class[19] != 0) printf ("Highest class average talent: %d\n",class_talent[19]/class[19]); if (class[18] != 0) printf ("Second class average talent: %d\n",class_talent[18]/class[18]); if (class[17] != 0) printf ("Third class average talent: %d\n",class_talent[17]/class[17]); if (class[16] != 0) printf ("Forth class average talent: %d\n",class_talent[16]/class[16]); if (class[15] != 0) printf ("Fifth class average talent: %d\n",class_talent[15]/class[15]); if (class[14] != 0) printf ("Sixth class average talent: %d\n",class_talent[14]/class[14]); if (class[13] != 0) printf ("Seventh class average talent: %d\n",class_talent[13]/class[13]); if (class[12] != 0) printf ("Eighth class average talent: %d\n",class_talent[12]/class[12]); if (class[11] != 0) printf ("Ninth class average talent: %d\n",class_talent[11]/class[11]); if (class[10] != 0) printf ("10 class average talent: %d\n",class_talent[10]/class[10]); if (class[9] != 0) printf ("11 class average talent: %d\n",class_talent[9]/class[9]); if (class[8] != 0) printf ("12 class average talent: %d\n",class_talent[8]/class[8]); if (class[7] != 0) printf ("13 class average talent: %d\n",class_talent[7]/class[7]); if (class[6] != 0) printf ("14 class average talent: %d\n",class_talent[6]/class[6]); if (class[5] != 0) printf ("15 class average talent: %d\n",class_talent[5]/class[5]); if (class[4] != 0) printf ("16 class average talent: %d\n",class_talent[4]/class[4]); if (class[3] != 0) printf ("17 class average talent: %d\n",class_talent[3]/class[3]); if (class[2] != 0) printf ("18 class average talent: %d\n",class_talent[2]/class[2]); if (class[1] != 0) printf ("19 class average talent: %d\n",class_talent[1]/class[1]); if (class[0] != 0) printf ("20 class average talent: %d\n",class_talent[0]/class[0]); printf ("\n\n\n"); }