MASON-INTEREST-L Archives

March 2012

MASON-INTEREST-L@LISTSERV.GMU.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Randolph Latimer <[log in to unmask]>
Reply To:
MASON Multiagent Simulation Toolkit <[log in to unmask]>
Date:
Tue, 20 Mar 2012 09:05:39 -0400
Content-Type:
text/plain
Parts/Attachments:
text/plain (351 lines)
Hi Sean, thanks for all the help.
I'd like to be able to help, if I can, students in Dr. Axtell's classes 
for example that may want some guidance getting started with Mason.
Parts of how Mason's working are still a little 'magical' to me, but 
should get easier with time.

I'm modeling the CoordinationGame (from Ascape) on your Schoolyard 
example.   I also just got some code working from using the draw() 
function from AntsForaging on Mason.

Here's what I have (this is kind of hybrid code at this point)

This code now shows a mixed number of yellow and red oval agents 
(CoordinationPlayer) on a blue grid.  So I think the initial beginning 
is now working.
Sean - I'm not sure where I would insert your "MyPortrayal" suggestion. 
Is it the same thing I'm doing here, or is there an essential 
difference.  I think my version may have each agent drawing/painting 
themselves, your MyPortrayal version may be different?

See if this is a good structure to start from. I'd like to be able to 
translate easily - and show other students how to do this - from 
Ascape/Netlogo/Repast to Mason.  I'd like to think that this is possible 
to do.
Thanks
  Randy

Here's my CoordinationGame (adapting from Ascape) so far -

FILE 1:
public class CoordinationPlayer	implements Steppable
{
	protected SimpleColorMap colormap = new 
SimpleColorMap(0,1,Color.black,Color.white);
	public Color color;

	public CoordinationPlayer()
	{
		if ((int)(Math.random()*2) == 0)
			color = Color.red;// colormap.getColor(.2);
		else color = Color.yellow; //colormap.getColor(.7);
	}

	public Color getColor()
	{
		return color;
	}

	public void step(SimState state)
	{
		CoordinationPlayers students = (CoordinationPlayers) state;
		ObjectGrid2D yardgrid = students.yardgrid;
	}

    public final void draw(Object object, Graphics2D graphics, 
DrawInfo2D info)
         {
THIS IS FROM ANTS FORAGING:
         int x = (int)(info.draw.x - info.draw.width / 2.0);
         int y = (int)(info.draw.y - info.draw.height / 2.0);
         int width = (int)(info.draw.width);
         int height = (int)(info.draw.height);
	graphics.setColor(color);
         graphics.fillOval(x,y,width, height);
         }
}


FILE 2
public class CoordinationPlayers extends SimState
{
	public int yardgridWidth=10;
	public int yardgridHeight = 10;
	public ObjectGrid2D yardgrid = new 
ObjectGrid2D(yardgridWidth,yardgridHeight);
	public  int numStudents = 30;
	public int numBuyers = 30;
	double forceToSchoolMultiplier= 0.01;
	double randomMultiplier = 0.1;
	public Network buddies = new Network(false);

	public CoordinationPlayers(long seed)
	{
		super(seed);
	}

	public void start()
	{
		super.start();
		yardgrid.clear();

		for(int i = 0; i < numStudents; i++)
		{
			CoordinationPlayer student = new CoordinationPlayer();
			int x = random.nextInt(yardgridWidth);
			int y = random.nextInt(yardgridHeight);
			yardgrid.set(x,y,student);
			do {
				x = random.nextInt(yardgridWidth);
				y = random.nextInt(yardgridHeight);
			} while (yardgrid.get(x,y) != null);
			schedule.scheduleRepeating(student);
		}
	}

	public static void main(String[] args)
	{
		doLoop(CoordinationPlayers.class, args);
		// Longer version:
		/*
		SimState state = new Students(System.currentTimeMillis());
		state.start();
		do
			if (!state.schedule.step(state)) break;
		while(state.schedule.getSteps() < 5000);
		state.finish();
		*/
		System.exit(0);
	}
}

FILE 3

public class CoordinationGameWithUI extends GUIState
{
	public Display2D display;
	public JFrame displayFrame;
	//ContinuousPortrayal2D yardPortrayal = new ContinuousPortrayal2D();
	ObjectGridPortrayal2D yardgridPortrayal = new ObjectGridPortrayal2D();
	NetworkPortrayal2D buddiesPortrayal = new NetworkPortrayal2D();

	public static void main(String[] args)
	{
		CoordinationGameWithUI vid = new CoordinationGameWithUI();
		Console c = new Console(vid);
		c.setVisible(true);
	}

	public CoordinationGameWithUI() {
		super(new CoordinationPlayers(System.currentTimeMillis()));
	}

	public CoordinationGameWithUI(SimState state) {
		super(state);
	}

	public static String getName() {
		return "Coordination Game";
	}

	public void start()
	{
		super.start();
		setupPortrayals();
	}

	public void load(SimState state)
	{
		super.load(state);
		setupPortrayals();
	}

	public void setupPortrayals()
	{
		CoordinationPlayers students = (CoordinationPlayers) state;

		yardgridPortrayal.setField(students.yardgrid);
	//	yardgridPortrayal.setPortrayalForClass(CoordinationPlayer.class,new 
RectanglePortrayal2D(Color.red,true));
	//	yardgridPortrayal.setPortrayalForNull(new 
RectanglePortrayal2D(Color.blue,true));
	//	yardgridPortrayal.setPortrayalForAll(new 
RectanglePortrayal2D(Color.blue,false));
		//yardPortrayal.setField(students.yardgrid);
		//yardPortrayal.setPortrayalForAll(new OvalPortrayal2D());

		display.reset();
		display.setBackdrop(Color.blue);

		display.repaint();
	}

	public void init(Controller c)
	{
		super.init(c);
		display = new Display2D(600,600,this);
		display.setClipping(false);

		displayFrame = display.createFrame();
		displayFrame.setTitle("Schoolyard Display");
		c.registerFrame(displayFrame);
		displayFrame.setVisible(true);
		//display.attach(buddiesPortrayal, "Buddies");
		display.attach(yardgridPortrayal, "Yard");
	}

	public void quit()
	{
		super.quit();
		if (displayFrame != null)
			displayFrame.dispose();
		displayFrame = null;
		display = null;
	}
}






On 20.03.2012 08:07, Sean Luke wrote:
> Hi Randy.  Richard's suggestion assumed that your agents are
> portraying themselves.  I won't make that assumption.  Let's say that
> each agent has a function called myColor() which returns a Color.
> Then you could simply do:
>
> public class MyPortrayal
>         extends RectanglePortrayal2D {
>
>     public draw(Object obj Graphics2D g, DrawInfo2D inf)
> 	{
> 	MyAgent agent = (MyAgent) obj;
> 	g.setPaint(agent.myColor());
> 	super.draw();
> 	}
>    }
> }
>
>
>
>
> On Mar 20, 2012, at 7:39 AM, Randolph Latimer wrote:
>
>> Thanks Richard,
>>  I'm trying to have each agent portrayed with it's own particular 
>> color.  I tried your code suggestion (I may not be doing something 
>> correct), and all the CoordinationPlayer agents have the same color, 
>> either red or yellow, in this case.
>>  I'd like a blend of different colored agents portrayed, and the 
>> color is determined within the single agent class, like 
>> CoordinationPlayer.
>> Thanks for your help,
>> Randy Latimer
>>
>> On 20.03.2012 04:20, Richard O. Legendi wrote:
>>> Hi Randy,
>>>
>>> I'm a bit unsure if it helps you, but you can simply subclass
>>> _RectanglePortrayal2D_ directly with _CoordinationPlayers_, 
>>> something
>>> like the following code (haven't tested).
>>>
>>> This way you'll have a _color_ variable (called _paint_) by 
>>> default.
>>>
>>>> public class CoordinationPlayer
>>>>         extends RectanglePortrayal2D
>>>>         implements Steppable {
>>>>
>>>>     private static final double DIAMETER = 6.0;
>>>>
>>>>     public CoordinationPlayer() {
>>>>         super(DIAMETER);
>>>>         paint = (random.nextInt(2) == 0) ? Color.RED :
>>>> Color.YELLOW;
>>>>     }
>>>>
>>>>     ...
>>>> }
>>> This way you don't need the magic in the _setupPortroyals()_
>>> function.
>>>
>>> HTH,
>>> Richard
>>>
>>> --
>>> Richard O. Legendi
>>> Software developer
>>> Intelligent Applications and Web Services
>>> AITIA International, Inc.
>>> http://people.inf.elte.hu/legendi/ [2]
>>>
>>> On 2012.03.19. 14:53, Randolph Latimer wrote:
>>>
>>>> I'm starting out again with Mason and will appreciate some help 
>>>> with
>>>> what I expect are fairly simple questions.
>>>>
>>>> I'm transferring a Coordination Game model from Ascape to Mason.
>>>> The Ascape code is described here, if anyone's interested -
>>>> http://ascape.sourceforge.net/manual/Section3.html [1]
>>>>
>>>> In Mason, I'm using the Schoolyard model as a starter.
>>>> In Coordination Game, each agent needs a variable for its color.
>>>> I'm trying something like this (I don't think I'm implementing
>>>> Mason SimpleColorMap correctly):
>>>>
>>>> public class CoordinationPlayer implements Steppable
>>>> {
>>>>     //public static final double MAX_FORCE = 3.0;
>>>>     protected SimpleColorMap colormap = new
>>>> SimpleColorMap(0,1,Color.black,Color.white);
>>>>     public Color color;
>>>>     public  MersenneTwisterFast random = new
>>>> MersenneTwisterFast();
>>>>
>>>>     public CoordinationPlayer()
>>>>     {
>>>>         if (random.nextInt(2) == 0)
>>>>             color = Color.red;// colormap.getColor(.2);
>>>>         else color = Color.yellow; //colormap.getColor(.7);
>>>>     }
>>>>
>>>>     public Color getColor()
>>>>     {
>>>>          return color;
>>>>     }
>>>>
>>>> In CoordinationGameWithUI, - the color is displayed here, in this
>>>> hybrid code from the Schoolyard model.
>>>> I'm trying to set the color with setPortryalForClass, where each
>>>> class object has its own color.
>>>> This isn't working correctly:
>>>>
>>>>     public void setupPortrayals()
>>>>     {
>>>>         CoordinationPlayers students = (CoordinationPlayers)
>>>> state;
>>>>
>>>>         yardgridPortrayal.setField(students.yardgrid);
>>>> I'M TRYING TO SET THE COLOR HERE:
>>>>
>>>> yardgridPortrayal.setPortrayalForClass(CoordinationPlayer.class,
>>>>                      new
>>>> RectanglePortrayal2D(CoordinationPlayer.getColor(),true)); //new
>>>>
>>>
>>>> RectanglePortrayal2D(Color.red,true));
>>>>         yardgridPortrayal.setPortrayalForNull(new
>>>> RectanglePortrayal2D(Color.blue,true));
>>>>
>>>> Thanks for any help -
>>>> Randy Latimer
>>>
>>>
>>> Links:
>>> ------
>>> [1] http://ascape.sourceforge.net/manual/Section3.html
>>> [2] http://people.inf.elte.hu/legendi/

ATOM RSS1 RSS2