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

📄 sugarspace.java

📁 本源码应用于Java的JDK1.4.2版本+Swarm2.2 程序在Cygwin终端运行
💻 JAVA
字号:
import swarm.Globals;
import swarm.defobj.Zone;
//import swarm.defobj.HDF5ArchiverImpl;

import swarm.space.Grid2d;
import swarm.space.Grid2dImpl;
import swarm.space.Discrete2dImpl;
import swarm.space.Discrete2d;
import swarm.objectbase.SwarmObjectImpl;
import  swarm.SwarmEnvironmentImpl;
//import  swarm.space.Int2dFilerImpl;
import swarm.space.Diffuse2dImpl;

import java.awt.*;
import java.util.ArrayList;
import java.io.*;

public class SugarSpace extends Discrete2dImpl
{
  public int xsize ;
  public int ysize ;
 // public Discrete2dImpl sugar; // sugar at each spot
  public Grid2dImpl agentGrid; // agent positions
  public Discrete2dImpl maxSugar; // max sugar at each spot
  String maxSugarDataFile="env100.pmg"; // const //Also set the maximum sugar datafile
  public int sugarGrowRate;
  public int globalMaxSugar=5; //  absolute maximum for space

  public SugarSpace(Zone aZone, int x, int y){
    super(aZone,x,y);
    System.out.println("SugarSpace: After super(aZone)");
    fastFillWithValue(0);
    xsize = x;
    ysize = y;
    // Check that we have a reasonable size
    if (xsize <= 0 || ysize <= 0)

      //InvalidCombination.raiseEvent("SugarSpace was created with an invalid size\n.");
      System.err.println("SugarSpace was created with an invalid size\n.");
    if (maxSugarDataFile == null)
      // InvalidCombination.raiseEvent("SugarSpace was created without a data file for maxsugar\n.");
      System.err.println(
          "SugarSpace was created without a data file for maxsugar\n.");

    String s="";
    File  f1 = new File(maxSugarDataFile);
    try{
            FileReader fin = new FileReader(f1);
            BufferedReader br = new BufferedReader(fin);
            String rs;
          while((rs=br.readLine())!=null)
               s = s+rs;
            br.close();
            fin.close();
      }catch(java.io.FileNotFoundException e){
      }catch(IOException e){System.out.println("IOException File not found");
    }
    int xPos,yPos;
     xPos=-1;yPos=0;

    for(int i=0;i<s.length();i++){
            char m=s.charAt(i);
          if(m=='+'){
                    xPos=-1;
                    yPos++;}

               if((m>='0')&&(m<='9')){
                    int v=(int)m-48;
                       xPos++;
                  //  System.out.print("("+x+","+y+")"+" ");
                       putValue$atX$Y(v , xPos, yPos);
                      }
               }
               System.out.println("read after file");
  // agentGrid=new Grid2dImpl(getZone(),xsize,ysize);

  }

  public int findGlobalMaxSugar(Discrete2dImpl discrete2d){
    int maxval;
    int x, y;
    maxval = discrete2d.getValueAtX$Y(0, 0); //return the integer at (x,y).
    for (y = 0; y < ysize; y++)
      for (x = 0; x < xsize; x++) {
        int val = discrete2d.getValueAtX$Y(x, y);
        if (val > maxval)
          maxval = val;
      }
    return maxval + 1;
  }

// Read out the maximum value of sugar from the space
  public int getGlobalMaxSugar() {
    return globalMaxSugar;
  }
public void setMaxSugar(Discrete2dImpl discrete2d){
    maxSugar=discrete2d;
  }
  public Object setSugarGrowRate(int r) {
    sugarGrowRate = r;
    return this;
  }

  public int getSugarGrowRate() {
    return sugarGrowRate;
  }

// Also set the maximum sugar datafile
  public Object setAgentGrid(Grid2dImpl world) {
     agentGrid=world;
     return this;
  }

  public Object setSizeX$Y(int x, int y) {
    xsize = x;
    ysize = y;
    return this;
  }

  public int getSizeX() {
    return xsize;
  }

  public int getSizeY() {
    return ysize;
  }

// Dynamics; add more sugar to the world. This is rule G_alpha of the book.
  public Object updateSugar() {
    int x, y;
    for (x = 0; x < xsize; x++) {
      for (y = 0; y < ysize; y++) {
        int sugarHere = getValueAtX$Y(x, y);
        int maxSugarHere = getValueAtX$Y(x, y);
        if (sugarHere + sugarGrowRate < maxSugarHere)
          sugarHere = sugarHere + sugarGrowRate;
        else
          sugarHere = maxSugarHere;
          putValue$atX$Y(sugarHere, x, y);
      }
    }
    return this;
  }

  protected int getSugarAtX$Y(int x, int y) {
    x = this.xnorm(x);
    y = this.ynorm(y);
    return (int) getValueAtX$Y(x, y);
  }

  public int takeSugarAtX$Y(int x, int y) {
    int sugarHere;
    x = this.xnorm(x);
    y = this.ynorm(y);
    sugarHere = getValueAtX$Y(x, y);
    putValue$atX$Y(0, x, y);
    return sugarHere;
  }

//  public Discrete2dImpl getSugarValues() {
  //  return ;
  //}

  public Object getAgentAtX$Y(int x, int y) {
    return agentGrid.getObjectAtX$Y(this.xnorm(x), this.ynorm(y));
  }

  public Object addAgent$atX$Y(SugarAgent agent, int x, int y) {
    x = this.xnorm(x);
    y = this.ynorm(y);
    agent.xPos = x;
    agent.yPos = y;
    agentGrid.putObject$atX$Y(agent, x, y);
    return this;
  }

  protected Object removeAgent(SugarAgent agent) {
    int x, y;
    x = this.xnorm(agent.getX());
    y = this.ynorm(agent.getY());
    if (this.getAgentAtX$Y(x, y) == agent)
      agentGrid.putObject$atX$Y(null, x, y);
    return this;
  }

  protected Object moveAgent(SugarAgent agent, int x, int y) {
    this.removeAgent(agent);
    this.addAgent$atX$Y(agent, x, y);
    return this;
  }

  protected Grid2dImpl getAgentGrid() { // Get the array of all the agents in the space.
    return agentGrid;
  }

  public int xnorm(int x) {
    if (x < 0)
      return (x + xsize * 128) % xsize; //negative?make positive,round
    else if (x >= xsize)
      return x % xsize;
    else
      return x;
  }

  public int ynorm(int y) {
    if (y < 0)
      return (y + ysize * 128) % ysize; //negative?make positive,round
    else if (y >= ysize)
      return y % ysize;
    else
      return y;
  }


//read an array from a file.

public int setDiscrete2d$toFile(Discrete2d a, String filename)
{     int maxValue=0;
      String s="";
      File  f1 = new File(filename);
      try{
              FileReader fin = new FileReader(f1);
              BufferedReader br = new BufferedReader(fin);
              String rs;
            while((rs=br.readLine())!=null)
                 s = s+rs;
              br.close();
              fin.close();
        }catch(java.io.FileNotFoundException e){
        }catch(IOException e){
      }
      int x,y;
       x=-1;y=0;

      for(int i=0;i<s.length();i++){
              char m=s.charAt(i);
            if(m=='+'){
                      x=-1;
                      y++;}

                 if((m>='0')&&(m<='9')){
                      int v=(int)m-48;
                      if(v > maxValue)maxValue=v;
                         x++;
                    //  System.out.print("("+x+","+y+")"+" ");
                       //  a.putValue$atX$Y(v , x, y);
                        }
                 }
           return maxValue;
}
public void setDiscrete2d$fromFile(Discrete2d a, String filename)
{
      String s="";
      File  f1 = new File(filename);
      try{
              FileReader fin = new FileReader(f1);
              BufferedReader br = new BufferedReader(fin);
              String rs;
            while((rs=br.readLine())!=null)
                 s = s+rs;
              br.close();
              fin.close();
        }catch(java.io.FileNotFoundException e){
        }catch(IOException e){
      }
      int x,y;
       x=-1;y=0;

      for(int i=0;i<s.length();i++){
              char m=s.charAt(i);
            if(m=='+'){
                      x=-1;
                      y++;}

                 if((m>='0')&&(m<='9')){
                      int v=(int)m-48;
                         x++;
                    //  System.out.print("("+x+","+y+")"+" ");
                         a.putValue$atX$Y(v , x, y);
                        }
                 }
     //    return a;
}

public Object copyDiscrete2d$toDicrete2d(Discrete2d a  , Discrete2d b)
{
   int x,y;
   if(a.getSizeX()!=b.getSizeX()||a.getSizeY()!=b.getSizeY())
     for(x=0;x<a.getSizeX();x++)
       for(y=0;y<b.getSizeY();y++)
         b.putValue$atX$Y(a.getValueAtX$Y(x,y),x,y);
         return  this;
}

}






⌨️ 快捷键说明

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