swarm-support
[Top][All Lists]
Advanced

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

Can you help me understand Qsort and indexes?


From: Paul E. Johnson
Subject: Can you help me understand Qsort and indexes?
Date: Tue, 30 Jun 1998 17:41:06 -0500 (CDT)

Here is my objective.  I have a method that takes a list as input.
The agent evaluates each object on the list and assigns it a value.
Then I want to pick the best one, or best two, or three, depending
on how many that agent is allwed to pick.  I've got the main part
of the code working, just don't undestand how to use the Qsort procedure
from simtools.h.

To be concrete, here is the top part of the method (which works).

-(int) reportX: (id) offerings {   

   int i, j;

    dimarray idealCoordinates;
    double  offrMatrx[[offerings getCount]][DIMENSION]; 
    double  newvalue[[offerings getCount]];

  //Comment: take a list of offerings.  Get coordinates for each, evaluate
them.  
    for(j=0;j<2;j++){
      idealCoordinates[j]= [ideal getCoordinate:j];}

   for(i=0; i<[offerings getCount]; i++){
     newvalue[i]=0;
     for (j=0;j<DIMENSION;j++){
      offrMatrx[i][j] = [[offerings atOffset: i] getCoordinate: j] ;
      newvalue[i] += pow((offrMatrx[i][j]-idealCoordinates[j]), 2.00);
     }}

So I have an array "newvalue" which tells the actor how far from his own
ideal each offering is. 

I have written a method that makes the objects in the offerings list take
in the "newvalue" as a double, and a getValue method grabs that double
back.   I believe code like this will cause the
offerings list to be sorted:

 [QSort sortObjectsIn: offerings using: M(getValue)] ;

Now the part I dont understand.

   First, I want to leave the "offerings" object unchanged as it exists
in the modelSwarm, so I can't just apply Qsort to it, right?  If I sorted
offerings, then it wouldn't be the same as it was before in modelSwarm?
And then I couldn't pass it to other agents to see how they like it?

  Second, I don't want the biggest or smallest value as calculated in the
sorting, I want the identity of the object that had the highest value.
Know what I mean? Pass in 6 objects to the method, find their values are
(10,2,3,1,8,7), sort them as (1,2,3,7,8,10) and then you find that the
smallest value, a 1, belonged to object 3, if they were numbered
0,1,2,3,4,5 on the way in.

Thanks in advance

Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ukans.edu/~pauljohn
University of Kansas                  Office: (913) 864-9086
Lawrence, Kansas 66045                FAX: (913) 864-5700


                  ==================================
   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]