swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Voting 1.0


From: Darren Schreiber
Subject: Re: Voting 1.0
Date: Tue, 11 Apr 2000 12:55:41 -0700

Thanks for the help Marcus.  I haven't gotten to sit down with that code
since that version went up.  I am planning to return to work on it this
summer.  So, your pointers are a good head start.  I do think there are
some serious questions as to why the messages go to nil.  I found that this
happened usually a number of iterations into the model, and at the time
didn't have time to track them down.

I do wonder if the more experienced developers could offer their
suggestions on what to do in order to clean code.  Obviously, you want to
remove unneeded variables and add comments to explain things better.

But, I have to admit I've never learned how to:

1.  "profile" a program -- I've seen people use this term here and am not
sure I know precisely what it means.
2.  check for memory leaks -- I believe I have one in my most recent
Schelling model, but don't know how to find them
3.  figure out where the biggest slow downs in the program are -- (I think
this is what profiling can help with, right?)
4.  think systematically about how to improve code

I know that there are some very experienced and trained programmers on this
list.  I've been finding myself doing a lot of work in Swarm over the last
year, but I have not had very much formal education in programming.  What
are the tools or books that you find guidance from in the polishing stages
of programming?

        Darren

(I would think that some of these issues (along with the discussions on
optimizing) would be interesting material for a final chapter or appendix
to the Users Guide)


>>>>>> "DC" == David Conti <address@hidden> writes:
>
>DC> I'm trying to compile the Schelling model from Benedict
>DC> Stefansson's UCLA website as a test program.  The error I get is:
>DC> "C:\Swarm-2.1\etc\swarm: Permission denied"
>
>I had a few problems with Voting1.0.  The problem you ran into
>is just that the include was hardcoded instead of using SWARMHOME.
>
>There were a few places where messages were sent to nil.  I hacked
>around them.  Those cases should probably be given more scrutiny.
>There were some dead variables I removed in the diffs below, one
>missing include file (for method declarations in Coalitions.h), and
>usages of some obsolete uniform distribution names.
>
>diff -c Voting1.0/Agent.m Voting1.0.1/Agent.m
>*** Voting1.0/Agent.m  Wed Oct 27 17:44:15 1999
>--- Voting1.0.1/Agent.m        Tue Apr 11 11:48:18 2000
>***************
>*** 6,12 ****
>  // Started on May 1999
>
>  #import "Agent.h"
>! #import <simtools.h>
>
>  @implementation Agent
>
>--- 6,12 ----
>  // Started on May 1999
>
>  #import "Agent.h"
>! #import "Coalition.h"
>
>  @implementation Agent
>
>***************
>*** 143,151 ****
>  //  issue positions and A is a matrix of weights (salience on the diagonal,
>  //  separability off diagonal).
>  -(double) estimateDistance: (id) otherAgent {
>!   int i,j,k;
>    double distance;
>-   double sum;
>    double w, y1, y2, z1, z2;
>
>    distance=0;
>--- 143,150 ----
>  //  issue positions and A is a matrix of weights (salience on the diagonal,
>  //  separability off diagonal).
>  -(double) estimateDistance: (id) otherAgent {
>!   int i,j;
>    double distance;
>    double w, y1, y2, z1, z2;
>
>    distance=0;
>diff -c Voting1.0/Coalition.m Voting1.0.1/Coalition.m
>*** Voting1.0/Coalition.m      Wed Oct 27 17:46:49 1999
>--- Voting1.0.1/Coalition.m    Tue Apr 11 11:53:41 2000
>***************
>*** 131,137 ****
>    newLink=[newLink createEnd];
>    [newLink setColor: "pink"];
>    inferiorNum=[anAgent getAgentID];
>!   closestNum=[closestNeighbor getAgentID];
>    //superiorNum=[superior getAgentID];
>    [inferiorLinksList addLast: newLink];
>
>--- 131,138 ----
>    newLink=[newLink createEnd];
>    [newLink setColor: "pink"];
>    inferiorNum=[anAgent getAgentID];
>!   if (closestNeighbor)
>!     closestNum=[closestNeighbor getAgentID];
>    //superiorNum=[superior getAgentID];
>    [inferiorLinksList addLast: newLink];
>
>***************
>*** 140,146 ****
>
>  -updateLinks { // Added for Version0.7
>    int i;
>!   id anAgent, aLink;
>
>    [inferiorLinksList deleteAll];
>    for (i=0; i<[inferiorsList getCount]; i++) {
>--- 141,147 ----
>
>  -updateLinks { // Added for Version0.7
>    int i;
>!   id anAgent;
>
>    [inferiorLinksList deleteAll];
>    for (i=0; i<[inferiorsList getCount]; i++) {
>***************
>*** 151,158 ****
>  }
>  -changeMyBorderColor: (int) aColor {  // Coalitions version
>    const char * cColor;
>-   int i;
>-   id aLink;
>
>    cColor=[colorSelector getColorName: aColor];
>    [agentNode setBorderColor: cColor];
>--- 152,157 ----
>***************
>*** 166,172 ****
>  -movePlatform; {  // CHANGE THIS method for WeightedEuclideanDistance
>    int i;
>    float deltaX, deltaY;
>!   id anAgent, aLink;
>
>    anAgent=[self getClosestNeighbor];
>    if (anAgent==nil)
>--- 165,171 ----
>  -movePlatform; {  // CHANGE THIS method for WeightedEuclideanDistance
>    int i;
>    float deltaX, deltaY;
>!   id anAgent;
>
>    anAgent=[self getClosestNeighbor];
>    if (anAgent==nil)
>diff -c Voting1.0/Makefile Voting1.0.1/Makefile
>*** Voting1.0/Makefile Thu Oct 14 13:38:17 1999
>--- Voting1.0.1/Makefile       Tue Apr 11 11:45:47 2000
>***************
>*** 2,8 ****
>  OBJECTS=main.o ObserverSwarm.o Parameters.o ModelSwarm.o Agent.o Voter.o
>Coalition.o ColorSelector.o
>  APPLIBS=-lspace
>
>! include /usr/etc/swarm/Makefile.appl
>
>  main.o: main.m ObserverSwarm.h
>  ObserverSwarm.o: ObserverSwarm.h ObserverSwarm.m
>--- 2,8 ----
>  OBJECTS=main.o ObserverSwarm.o Parameters.o ModelSwarm.o Agent.o Voter.o
>Coalition.o ColorSelector.o
>  APPLIBS=-lspace
>
>! include $(SWARMHOME)/etc/swarm/Makefile.appl
>
>  main.o: main.m ObserverSwarm.h
>  ObserverSwarm.o: ObserverSwarm.h ObserverSwarm.m
>diff -c Voting1.0/ModelSwarm.m Voting1.0.1/ModelSwarm.m
>*** Voting1.0/ModelSwarm.m     Thu Oct 21 21:21:05 1999
>--- Voting1.0.1/ModelSwarm.m   Tue Apr 11 12:04:07 2000
>***************
>*** 82,88 ****
>
>  -(id)formNewCoalition: (id) agent1 two: (id) agent2 {
>    id newCoalition;
>-   int number;
>
>    // form the new Coalition
>     newCoalition=[Coalition createBegin: self];
>--- 82,87 ----
>***************
>*** 142,148 ****
>      //printf("The number is %d. ",i);
>      agent1=[availableList atOffset: i];
>      agent2=[agent1 getClosestNeighbor];
>!     agent3=[agent2 getClosestNeighbor];
>      if (agent1==agent3)
>        {
>       newCoalition=[self formNewCoalition: agent1 two: agent2];
>--- 141,147 ----
>      //printf("The number is %d. ",i);
>      agent1=[availableList atOffset: i];
>      agent2=[agent1 getClosestNeighbor];
>!     agent3=agent2 ? [agent2 getClosestNeighbor] : nil;
>      if (agent1==agent3)
>        {
>       newCoalition=[self formNewCoalition: agent1 two: agent2];
>***************
>*** 202,208 ****
>      mySuperior=[anAgent getSuperior];
>      if  ((myNeighbor!=mySuperior) &&
>       ([anAgent isMyInferior: myNeighbor]==0) &&
>!      ([myNeighbor isACoalition]==1))
>        if (([anAgent hasMajority]==0) &&
>         ((([anAgent getHierarchyLevel]==0) && ([availableList
>getCount]==2))==0))
>       {
>--- 201,207 ----
>      mySuperior=[anAgent getSuperior];
>      if  ((myNeighbor!=mySuperior) &&
>       ([anAgent isMyInferior: myNeighbor]==0) &&
>!          myNeighbor && ([myNeighbor isACoalition]==1))
>        if (([anAgent hasMajority]==0) &&
>         ((([anAgent getHierarchyLevel]==0) && ([availableList
>getCount]==2))==0))
>       {
>***************
>*** 247,256 ****
>  -findTheWinner {
>    int i;
>    int mostVotes;
>!   id agentWithMostVotes;
>    id anAgent;
>    BOOL tie;
>!   id winnerNode;
>
>    mostVotes=0;
>    tie=0;
>--- 246,255 ----
>  -findTheWinner {
>    int i;
>    int mostVotes;
>!   id agentWithMostVotes = nil;
>    id anAgent;
>    BOOL tie;
>!   id winnerNode = nil;
>
>    mostVotes=0;
>    tie=0;
>***************
>*** 338,344 ****
>  -takeAPicture  {  // Added for Version0.7.6
>    char filename[40];
>
>!   sprintf(filename, "pic%07d.png", getCurrentTime());
>    //[actionCache doTkEvents];
>    [[[[[[Pixmap createBegin: [self getZone]]
>       setWidget: graphCanvas]
>--- 337,343 ----
>  -takeAPicture  {  // Added for Version0.7.6
>    char filename[40];
>
>!   sprintf(filename, "pic%07ld.png", getCurrentTime());
>    //[actionCache doTkEvents];
>    [[[[[[Pixmap createBegin: [self getZone]]
>       setWidget: graphCanvas]
>***************
>*** 350,356 ****
>
>  -buildObjects {
>    id aVoter;
>!   int pt,i;
>
>    [super buildObjects];
>
>--- 349,355 ----
>
>  -buildObjects {
>    id aVoter;
>!   int i;
>
>    [super buildObjects];
>
>***************
>*** 365,372 ****
>
>    // Also some random number generators  //PMMLCG1
>    aGenerator=[MT19937gen create: [self getZone] setStateFromSeed:
>randomSeed];
>!   uniformDouble=[UniformDouble create: [self getZone] setGenerator:
>aGenerator];
>!   uniformInteger=[UniformInteger create: [self getZone] setGenerator:
>aGenerator];
>
>    // Each pass creates a voter and adds him to the voter and available lists
>    for (i=0; i<numVoters; i++) {
>--- 364,371 ----
>
>    // Also some random number generators  //PMMLCG1
>    aGenerator=[MT19937gen create: [self getZone] setStateFromSeed:
>randomSeed];
>!   uniformDouble=[UniformDoubleDist create: [self getZone] setGenerator:
>aGenerator];
>!   uniformInteger=[UniformIntegerDist create: [self getZone]
>setGenerator: aGenerator];
>
>    // Each pass creates a voter and adds him to the voter and available lists
>    for (i=0; i<numVoters; i++) {
>***************
>*** 422,438 ****
>  -step {
>    [self updateSalience];
>
>!   if ((movePlatforms==1) && ([winnerAgent getNumberOfVoters]>(numVoters/2)))
>      {
>!       [coalitionList forEach: M(findClosestNeighbor:) : agentList];
>!       [coalitionList forEach: M(movePlatform)];
>!
>!       // See if the agents want to defect and then do the defections
>!       [self checkForDefections];
>!
>!       //[self textOutputDendogram];
>      }
>-
>    // Find your closest neighbor
>    [availableList forEach: M(findClosestNeighbor:) : (id) availableList];
>    // Merge with your closest neighbor if he agrees
>--- 421,440 ----
>  -step {
>    [self updateSalience];
>
>!   if (winnerAgent)
>      {
>!       if ((movePlatforms == 1)
>!           && [winnerAgent getNumberOfVoters] > numVoters / 2)
>!         {
>!           [coalitionList forEach: M(findClosestNeighbor:) : agentList];
>!           [coalitionList forEach: M(movePlatform)];
>!
>!           // See if the agents want to defect and then do the defections
>!           [self checkForDefections];
>!
>!           //[self textOutputDendogram];
>!         }
>      }
>    // Find your closest neighbor
>    [availableList forEach: M(findClosestNeighbor:) : (id) availableList];
>    // Merge with your closest neighbor if he agrees
>
>                  ==================================
>   Swarm-Support is for discussion of the technical details of the day
>   to day usage of Swarm.  For list administration needs (esp.
>   [un]subscribing), please send a message to <address@hidden>
>   with "help" in the body of the message.


_____________________________________________

                 Darren Schreiber
                  Attorney at Law
                 Graduate Student
             Political Science, UCLA
                address@hidden
        http://www.bol.ucla.edu/~dschreib

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]