⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modelswarm.java.bak

📁 本源码应用于Java的JDK1.4.2版本+Swarm2.2 程序在Cygwin终端运行
💻 BAK
字号:
package sugarscape;

import swarm.Globals;
import swarm.Selector;
import swarm.defobj.Zone;
import swarm.defobj.SymbolImpl;

import swarm.defobj.FArguments;
import swarm.defobj.ArgumentsImpl;
import swarm.defobj.FCall;
import swarm.defobj.FCallImpl;

import swarm.activity.Activity;
import swarm.activity.ScheduleActivity;
import swarm.activity.ActionGroup;
import swarm.activity.ActionGroupImpl;
import swarm.activity.Schedule;
import swarm.activity.ScheduleImpl;
import swarm.activity.FActionForEach;

import swarm.objectbase.Swarm;
import swarm.objectbase.SwarmImpl;
//import swarm.objectbase.SwarmObjectImpl;

import swarm.objectbase.VarProbe;
import swarm.objectbase.MessageProbe;
import swarm.objectbase.EmptyProbeMapImpl;

import java.util.ArrayList;
import  swarm.collections.ListImpl;
import  swarm.collections.Index;
import  swarm.collections.ListShufflerImpl;
import swarm.SwarmEnvironmentImpl;
//import swarm.random.UniformIntegerDistImpl;

import swarm.space.Grid2d;
import swarm.space.Grid2dImpl;
//import swarm.space.Discrete2dImpl;
//import swarm.space.Discrete2d;

public class ModelSwarm extends SwarmImpl
{
// The model swarm encapsulates all the objects that go into the Sugarscape
// model. In the simplest case, this is basically a list of agents, a
// sugarspace world, and various parameters.
//parameter and method for the model
  public int numAgents;

  public int alpha;//growth rate for sugar
  public int replacement;//use replacement rule?
  public int maxVision , maxMetabolism;
  public int minInitialSugar,maxInitialSugar;
  public int deathAgeMin,deathAgeMax;
  public int worldXSize,worldYSize;
  String datafile;//const char
  //Objects in the list
  protected ListImpl  agentList;
  protected Shuffle  _shuffler;
  protected SugarSpace  _sugarspace;
  ListImpl reaperQueue;

  //schedule stuff.
  protected Schedule    _modelSchedule;
  protected ActionGroup _modelActions;
 //methods to handle the agents in the world.
 public ModelSwarm(Zone aZone)
 {
   super(aZone);
    worldXSize = 50;
    worldYSize = 50;
    numAgents = 400;
    alpha = 1;
    replacement = 0;
    maxVision = 6;
    minInitialSugar = 5;
    maxInitialSugar =25;
    deathAgeMin = 99998;
    deathAgeMax = 100000;
    
    ArgumentsImpl arg=new ArgumentsImpl();
   final String dataPath =arg.getAppDataPath();//const char*
   final String filename = "sugarspace.pgm";//const char*
     String p,buf = new String("dataPath"+ "filename"+ 1);
    //  p = stpcpy(buf,dataPath);
  //   stpcpy(p,filename);
   //  datafile = buf;
   datafile=dataPath +"/"+ filename;

     EmptyProbeMapImpl  _probeMap = new EmptyProbeMapImpl(aZone,getClass());
    _probeMap.addProbe(probeVariable("numAgents"));
    _probeMap.addProbe(probeVariable("alpha"));
    _probeMap.addProbe(probeVariable("replacement"));
    _probeMap.addProbe(probeVariable("maxMetabolism"));
    _probeMap.addProbe(probeVariable("maxVision"));
    _probeMap.addProbe(probeVariable("minInitialSugar"));
    _probeMap.addProbe(probeVariable("maxInitialSugar"));
    _probeMap.addProbe(probeVariable("deathAgeMin"));
    _probeMap.addProbe(probeVariable("deathAgeMax"));
    _probeMap.addProbe(probeVariable("datafile"));

     Globals.env.probeLibrary.setProbeMap$For
                                 (_probeMap, getClass ());
}
// Create a new agent at random and put it in the world.
public Object addNewRandomAgent()
{
   int x,y;
    SugarAgent agent;
    //turn off these warnings;
    _sugarspace.getAgentGrid().setOverwriteWarnings(true);
    //Create the agent object
     agent = new SugarAgent(getZone());
     agent.setModelSwarm(this);
    // Give the agent a random initial position and parameters.
     x = Globals.env.uniformIntRand.getIntegerWithMin$withMax(0,_sugarspace.getSizeX());
     y = Globals.env.uniformIntRand.getIntegerWithMin$withMax(0,_sugarspace.getSizeY());
     _sugarspace.addAgent$atX$Y(agent,x,y);
     agent.setcurrentsugar(Globals.env.uniformIntRand.getIntegerWithMin$withMax(minInitialSugar,maxInitialSugar));
     agent.setMetabolism(Globals.env.uniformIntRand.getIntegerWithMin$withMax(1,maxMetabolism));
     agent.setVision(Globals.env.uniformIntRand.getIntegerWithMin$withMax(1,maxVision));
     agent.setDeathAge(Globals.env.uniformIntRand.getIntegerWithMin$withMax(deathAgeMin,deathAgeMax));
    this.agentBirth(agent);
      //turn the warnings back on
      _sugarspace.getAgentGrid().setOverwriteWarnings(false);
      return this;
}
 //methods to handle the birth and death of agents
 public Object	agentBirth(SugarAgent agent)
 {
    agentList.addLast(agent);
    return this;
 }
 public Object agentDeath(SugarAgent agent)
 {
    reaperQueue.addLast(agent);
    if(replacement==0)//replacement rule R
    this.addNewRandomAgent();
    return this;
 }
 // remove all the agents on the reaperQueue from the agentList
// This allows us to defer the death of an agent until it's safe to
// remove it from the list.
public Object reapAgents()//考虑这个函数如何实现
{
        Index agent;//ListIndex include begin
        Index index;//ListImpl include next/drop
        index = reaperQueue.begin(getZone());//new ListImpl(getZone());
        while((agent=(Index) index.next())!=null)
        {
                agentList.remove(agent);
                agent.drop();
        }
        reaperQueue.removeAll();//members not exist,but resource hold
       return this;
}
//Accessor functions
protected SugarSpace getSugarSpace()
{
  return _sugarspace;
}
protected Object getAgentList()
{
   return  agentList;
}
public  Object  buildObjects()
{
       super.buildObjects();
          //first,set up the object that is the sugarscape-environment.
          _sugarspace = new  SugarSpace(getZone(),
                                       worldXSize,
                                       worldYSize,
                                       datafile);
          _sugarspace.setSugarGrowRate(alpha);
          //create a List to store all the  agents
          ListImpl agentList = new ListImpl(getZone());
          for (int i = 0; i < numAgents; i++)
            this.addNewRandomAgent();
          // Create a "reaper queue" to manage agent deaths
          reaperQueue = new ListImpl(getZone());
          // And create a shuffler object to randomize agent order
      /*    _shuffler = new Shuffle() ;//
         _shuffler.setUniformRandom(Globals.env.uniformIntRand);
      //  _shuffler.shuffleList(reaperQueue);//
           _shuffler.createEnd();*/
            return this;
 }
public Object   buildActions()
{
     super.buildActions();
    //   One time tick, a set of several actions:
    //   randomize the order of agent updates (to be fair)
    //   update all the agents
    //   kill off the agents who just died
    //   update the sugar on the world
    ActionGroup modelActions = new ActionGroupImpl(getZone());
   try
    {
      modelActions.createActionTo$message
       (_sugarspace,new Selector (_sugarspace.getClass(),"updateSugar",false));
     }catch(Exception e)
           {  System.err.println("Exception updatesugar:"+ e.getMessage());}

    /*  try
      {
        modelActions.createActionTo$message
         (_shuffler,new Selector(_shuffler.shuffleList(agentList).getClass()," _shuffler.shuffleList(agentList)",false));
      }catch(Exception e)
           {  System.err.println("_shuffler.shuffleList(agentList)"+e.getMessage());
              System.exit(1);
           }*/
      try
      {	  modelActions.createActionForEach$message
              (agentList, new Selector(agentList.getClass(), "step", false));
      }catch(Exception e)
      {System.err.println("agentList.getClass"+"returns"+e.getMessage());  }
     try
     {      modelActions.createActionTo$message
                (this,new Selector(this.reapAgents().getClass(),"reapAgents",false));
     }catch(Exception e)
     {  System.err.println("reapAgents()"+"returns"+e.getMessage()); }

     // The schedule is just running our actions over and over again
       _modelSchedule = new ScheduleImpl(getZone(),1);
       _modelSchedule.at$createAction(0,  modelActions);
        // ... The Schedule will execute ActionGroup modelActions at time
      // value 0 relative to the beginning of the Schedule.
       return this;
       }
public Activity activateIn(Swarm swarmContext)
{
   super.activateIn(swarmContext);
   // Then activate the schedule in ourselves.
    _modelSchedule.activateIn(this);
   return getActivity();
}

protected MessageProbe probeMessage (String name)
 {
    return Globals.env.probeLibrary.getProbeForMessage$inClass
     (name, ModelSwarm.this.getClass ());
 }

protected VarProbe probeVariable (String name)
 {
    return Globals.env.probeLibrary.getProbeForVariable$inClass
     (name, ModelSwarm.this.getClass ());
 }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -