Print

Print


Randall:

Without seeing all of your code it is a bit tricky to say what your problem
is for sure. Typically the errors in Java are pretty good though, and
casting between types (e.g., PotatoProducers and PotatoDistrutors) tends to
be possible when your classes are designed correctly. So PotatoProducers
and PotatoDistrubtors would need to share a common base class (e.g.,
PotatoAgent) that has the attributes that they share. Then you can pass the
base class around as needed and then cast to the child class as needed.

However, I tend to be very carefully about fully leveraging OOP when
writing ABMs since it can introduce a new layer of bugs in the code and
complicate model verification. If you just need to get access to common
attributes so the agents can be portrayed then having that information in
the base class still works, but I tend to include a virtual method that the
child implements that gets the type of agent. The portrayal code can then
be called using the the parent class as the parameter and just access that
information without full knowledge of the child agent (i.e., leverage
separation of concerns).

Regards,
Robert Zupko

On Tue, Jun 11, 2019 at 11:46 PM Randall Boone <[log in to unmask]>
wrote:

> Folks,
>
> I am new to MASON and learning Java as well, but a grizzled agent-based
> modeler.  I am leaning heavily on the tutorial in the manual (Student
> cliques), but would be in a better place if I could reference the code from
> the other tutorials.  Are those available?
>
> I'm hesitant to post a basic question to a list with sparse and advanced
> questions, but here goes.  I am seeking to add two unrelated agents to a
> simulation and a single dispaly.  Ultimately I will build a model with
> several agent types and several displays.
>
> I've adopted the language in the example, and in setupPortrayals i have
> included the code below (potato producers and potato distributors as
> agents).  The code checks out, but gives a runtime "potatoDistributors
> cannot be cast to potatoProducers" error.  Searches suggest that is because
> the agents differ, but I do need them to have different attributes.
>
> I wish to add two agent types to a single display, and have them interact,
> such as buy and selling potatoes between them.  I have searched for the
> code for the tutorials, but found messages on Github saying they have been
> removed.  Are old examples available that will show me general techniques?
> I suspect "Asteroids" or "Keep-away Soccer" would go a long way to
> clarifying things for me.
>
> Thank you for any help, and pardon if this query isn't correctly formatted,
> Randy Boone
>
>
> ***************
>
>     ContinuousPortrayal2D landPortrayal = new ContinuousPortrayal2D();
>     ContinuousPortrayal2D potatoDistributorsPortrayal = new
> ContinuousPortrayal2D();
>
> *************
>
>
>         if (cSystem.includePotatoes == true) {
>           PotatoProducers potatoProducers = (PotatoProducers) state;
>
>           landPortrayal.setField( potatoProducers.land );
>           landPortrayal.setPortrayalForAll(new MovablePortrayal2D(
>                 new CircledPortrayal2D(
>                     new LabelledPortrayal2D(
>                         new OvalPortrayal2D()
>                         { public void draw(Object object, Graphics2D
> graphics, DrawInfo2D info)
>                             { PotatoProducer potatoProducer =
> (PotatoProducer)object;
>                             paint = new Color(0, 0, 255);
>                             super.draw(object, graphics, info);
>                             } }, 5.0, null, Color.black, false), 0, 5.0,
> Color.green, true)));
>         }
>
>           PotatoDistributors potatoDistributors = (PotatoDistributors)
> state;
>           potatoDistributorsPortrayal.setField(potatoDistributors.land2);
>           potatoDistributorsPortrayal.setPortrayalForAll(new
> MovablePortrayal2D(
>                 new CircledPortrayal2D(
>                     new LabelledPortrayal2D(
>                         new OvalPortrayal2D()
>                         { public void draw(Object object, Graphics2D
> graphics, DrawInfo2D info)
>                             { PotatoDistributor potatoDistributor =
> (PotatoDistributor)object;
>                             paint = new Color(0, 255, 0);
>                             super.draw(object, graphics, info);
>                             } }, 5.0, null, Color.black, false), 0, 5.0,
> Color.green, true)));
>         }
>
>