%Sugarscape1 %Modified by Alan Mehlenbacher %sugarscape: (two-peak sugarscape, rule: Ginf) %agents: (moving sequence: random, view: four directions, rule: M) %Initialize model parameters nruns = 6; size = 50; %even number metabolismv = 4; visionv = 6; %set always smaller than size maxsugar = 20; %Initialize sugarscape and display. The sugarscape grid is represented by %the s matrix. s = initsugarscape(nruns, size, maxsugar); %Initialize agents population. The agents are represented by the structure %matrix a_str a_str = initagents(size, s, visionv, metabolismv); %Open an output file fid1 = fopen('SugarscapeOutput.txt','w+'); %Print header fprintf(fid1,'run,avgvision,avgmetabolism\n'); % %Main loop (run) for run = 1:nruns; %Display agent locations dispagentloc(a_str, size, nruns, run, fid1); %Process sugarscape cells in random order for i = randperm(size); for j = randperm(size); %Check if there is an agent in this cell if (a_str(i,j).active == 1) %Agent explores sugarscape in random directions and selects best location temps = s(i,j); %temps contains the level of sugar in cell (i,j) tempi = i; tempj = j; %Start looking at the farthest horizon within the agent's vision for k = a_str(i,j).vision : -1 : 1; [temps, tempi, tempj] = see(i,j,k,a_str,s,size,temps,tempi,tempj); end %The best move is to cell (tempi, tempj) with sugar stock of temps %Agent moves to this best location, updates sugar stock, and eats sugar a_str = moveagent(a_str, s, i, j, temps, tempi, tempj); end % end if end % end j loop end % end i loop end % end runs loop fclose(fid1); %close the output file