swarm-hackers
[Top][All Lists]
Advanced

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

Re: [swarm-hackers] Re: Swarm, Heatbugs, and forEach loops...


From: Nima Talebi
Subject: Re: [swarm-hackers] Re: Swarm, Heatbugs, and forEach loops...
Date: Wed, 11 Nov 2009 16:45:09 +1100

Please ignore my comment on the color - I misread that line.



On Wed, Nov 11, 2009 at 4:40 PM, Nima Talebi <address@hidden> wrote:
I like that last option too, but I'm not brave enough to mess with that just yet.

Meanwhile, I ran heatbugs in a 180x180 (instead of 80x80), and noticed that for such as simple program, it's running rather chuggy.

A profiler shows that 29% of the time is spent in [Value2dDisplay updateDisplay]...

Now, do we still need all these explicite autorelease pools to be setup and destroyed every single iteration?


 258 - (void)updateDisplay
 259 {
 260   if (!image) [self createImage];
 261
 262   if (image) {
 263     NSAutoreleasePool *pool = [NSAutoreleasePool new];
 264     NSSize aSize = [image size];
 265     //printf("Value2dDisplay display: %f %f\n", aSize.width, aSize.height);
 266     NSRect aRect = NSMakeRect(0, 0, aSize.width, aSize.height);
 267  
 268     [image lockFocus];
 269    
 270     [[NSColor clearColor] set];
 271     NSRectFill(aRect);
 272
 273     int x, y;
 274     // get max value
 275     modFactor = 0;
 276     for (x = 0; x < aSize.width; ++x)
 277       for (y = 0; y < aSize.height; ++y) {
 278         float color = (float)[discrete2d getValueAtX: x Y: y];
 279         if (color > modFactor) modFactor = color;
 280       }
 281
 282     for (x = 0; x < aSize.width; ++x)
 283       for (y = 0; y < aSize.height; ++y)
 284         {
 285         aRect = NSMakeRect(x, y, 1, 1);
 286         long color = [discrete2d getValueAtX: x Y: y];
 287         float redValue = (float)color / (float)modFactor;
 288         NSColor *aColor = [NSColor colorWithDeviceRed: redValue green: 0.0
 289           blue: 0.0 alpha: 1.0];
 290         [aColor set];
 291         NSRectFill(aRect);
 292         }
 293     [image unlockFocus];
 294     [pool release];
 295   }
 296 }

...furthermore, things like the creation of the same "aColor" in every loop, can that change to a static variable?

And probably where the most money is - would it be possible to mark pixels as "dirty", and have this method only redraw the dirty cells?

Sorry for all the stupid questions, I'm still trying to get used to this, and your responses often leak lots of good wholesome details that you may have taken for granted, but is very useful to a newb like myself :)

Nima


On Wed, Nov 11, 2009 at 1:13 PM, Scott Christley <address@hidden> wrote:
Well this works but I wouldn't encourage it.

This issue illustrates the fundamental question of how can Swarm support both its internal collections classes as well as the OpenStep collections at the same time.  I don't have a good answer for this.

One thing that could be done is to use the reflection capability of ObjC to query the class and do the appropriate code, such as:

if ([anObj isKindOfClass: [List class]]) // do List code
if ([anObj isKindOfClass: [NSArray class]]) // do NSArray code

But the use of collections is pervasive, so this would require lots of code changes.

A second idea is to have separate implementations of the Swarm classes, one that uses Swarm collections and the other that is OpenStep.

A third idea is to change the Swarm's List class to actually be implemented by an NSArray underneath.  Then programs using List will actually be using NSArray without knowing it.  Then additional methods would be added to the NSArray class so that it responds to List methods.  Then they could be used interchangeably.

The last idea is probably the least intrusive but still might have quite a few challenges to make work correctly.

Scott

On Nov 10, 2009, at 5:06 PM, Nima Talebi wrote:

Hi Scott,

Okay I now have a working solution - I did so by implementing a new ADT called SwarmColony (SwarmCollection was taken, and SwarmList and SwarmArray leaked too much about the "how" which I want to hide).

http://git.autonomy.net.au/?p=heatbugs;a=summary (I've created an experimental branch there called exp-ListToADT).

...this new type acts as both a id<List>, as well as a NSArray, but all that is hidden.  The only thing that needs to be changed in other code is this...

actionForEach = [modelActions createActionForEach:[heatbugList selector] message:M(step)];

...Next, I will change the variable name heatbugList to hbugColony.

Thoughts?

Nima


On Wed, Nov 11, 2009 at 10:57 AM, Nima Talebi <address@hidden> wrote:
Hi Scott,

Regarding NSArray - I did consider doing that, but then I hit the following problem...

- (id <ActionForEach>)createActionForEach: target message: (SEL)aSel;

...we call that when creating an action in the HeatbugModel class as follows...

actionForEach = [modelActions createActionForEach:heatbugList message:M(step)];

...however if heatbugList is converted to an NSArray, it will no longer be a valid SEL.

How do you suggest I tackle this?  My guess is that you don't want any changes made to the framework, as this will probably break everything, but perhapse there's a way I can create a heatbugList object that conforms to <List> as well as being an NSArray if that's even possible?

Nima


On Wed, Nov 11, 2009 at 3:46 AM, Scott Christley <address@hidden> wrote:
Hey Nima,

Swarm was a written a long time ago before there was a framework like OpenStep available, so it had to implement many of its own collection classes, like List, and as you can see the interface seems somewhat foreign.

My suggestion would be to try changing Heatbugs so that it uses an NSArray instead of List.  Then you can do you operations using standard OpenStep code.  I think making the Heatbugs code look as close as possible to a normal OpenStep application is good.

Otherwise, I think your code is ok.  One major functionality that is missing from the OpenStep version of Swarm is the probe display.  This essentially provides a popup window to do very similar to what you have done, though the probe display is more general and provides display and edit capability to all of a Heatbug's instance variables.

Scott

On Nov 10, 2009, at 5:44 AM, Nima Talebi wrote:

Nevermind, I figured it out.

I've updated my clone of Heatbugs (git tree), which now allows user to interact with the state of the Heatbug agents, changing their idealTemp and outputHeat minimums and maximums.

I did that like so...

- (void)reconfigureHeatbugs {
    [heatbugList forEach:M(setRandomIdealTemperatureInRange:to:) :minIdealTemp :maxIdealTemp];
    [heatbugList forEach:M(setRandomOutputHeatInRange:to:) :minOutputHeat :maxOutputHeat];
}

...and each time the user moves the sliders, MyDocument calls this method...

- (IBAction)setMaxOutputHeat:(id)sender {
    ...
        int tA = min([sender intValue], (int)[sender maxValue] - 100);
        [swarm setMaxOutputHeat:NSInt(tA)];
        
        int tB = min(tA, [[swarm minOutputHeat] intValue]);
        [swarm setMinOutputHeat:NSInt(tB)];
        [minOutputHeat setIntValue:tB];
        [minOutputHeat setNeedsDisplay];
        
        [swarm reconfigureHeatbugs];
   ....
}


...does anyone see anything wrong with that?  I ask because I still feel a little daunted by the amount of code in this framework, so I'm never quite certain that I'm going about things the right way.

Nima

On Tue, Nov 10, 2009 at 9:52 PM, Nima Talebi <address@hidden> wrote:
Hi, I'm a little confused (as I usually am looking at Swarm) on how iteration is supposed to work...

I have added some sliders and input widgets to my own branch of heatbugs, and so each time the user acts on these input widgets, I need to update the state variables stored in each Heatbug object.

For example, I would like to have something like this...

- (void)reconfigureHeatbug:(Heatbug *)hbug;

- (void)reconfigureHeatbugs {
    int i;
    Heatbug *hbug;
    for(i=0; i<numBugs; i++) {
        hbug = [heatbugList objectAtIndex:i];
        [self reconfigureHeatbug:hbug];
    }
}    

Of course that's not possible, as the heatbugList is not an NSArray, it is an id conforming to the <List> protocol.  Looking at surrounding code, I think maybe I'm supposed to do something along the lines of...

- (void)reconfigureHeatbugs {
    id fEAction = [modelActions createActionForEach:heatbugList
                                            message:M(reconfigureHeatbug:)];
}

..but that's obviously wrong since I have no idea what I'm doing - and what is M?

As a side-question - What would make life easier is being able to highlight a function/method/class/whatever in Xcode - say M - then right-click and select 'Jump to definition' - Anyone know how I can get that working?

Nima



--
Nima Talebi
web: http://ai.autonomy.net.au/People/Nima
gpg: B51D 1F18 D8E2 B702 B027 23A4 E06B DAC1 BE70 ADC0
_______________________________________________
swarm-hackers mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/swarm-hackers


_______________________________________________
swarm-hackers mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/swarm-hackers




--
Nima Talebi
web: http://ai.autonomy.net.au/People/Nima
gpg: B51D 1F18 D8E2 B702 B027 23A4 E06B DAC1 BE70 ADC0



--
Nima Talebi
web: http://ai.autonomy.net.au/People/Nima
gpg: B51D 1F18 D8E2 B702 B027 23A4 E06B DAC1 BE70 ADC0
_______________________________________________
swarm-hackers mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/swarm-hackers


_______________________________________________
swarm-hackers mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/swarm-hackers




--
Nima Talebi
web: http://ai.autonomy.net.au/People/Nima
gpg: B51D 1F18 D8E2 B702 B027 23A4 E06B DAC1 BE70 ADC0



--
Nima Talebi
web: http://ai.autonomy.net.au/People/Nima
gpg: B51D 1F18 D8E2 B702 B027 23A4 E06B DAC1 BE70 ADC0

reply via email to

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