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

📄 agent.java

📁 2004年robotcup世界冠军源代码
💻 JAVA
字号:
// Copyright (C) 2002 Takeshi Morimoto <morimoto@takopen.cs.uec.ac.jp>// All rights reserved.// Edited By Omid Aghazadeh.package yab.agent;import java.net.*;import yab.agent.*;import yab.agent.object.*;import yab.io.*;import java.util.Random;import java.util.*;import MRL.Utilities.Partitioning.Path;import MRL.Utilities.*;import MRL.Utilities.Partitioning.*;public abstract class Agent extends Thread implements Constants,ConstantConditions{  private final RCRSSProtocolSocket m_socket;  protected final DisasterSpace world;  protected final Random random;  private long maxHalfCycleTime = 1000;  private long cycleBegin;  protected Agent(int agentType, InetAddress kernelAddress, int kernelPort) {    m_socket = new RCRSSProtocolSocket(kernelAddress, kernelPort);    socket().akConnect(0, agentType);    Object data = socket().receive();    if (data instanceof KaConnectError) {      throw new Error(((KaConnectError) data).reason);    }    KaConnectOk ok = (KaConnectOk) data;    world = new DisasterSpace(ok.selfId, ok.selfAndMap);    System.out.println(self());    random = new Random((long) self().id);  }  /** This method decides action of the agent at each cycle. */  protected abstract void act() throws ActionCommandException;  /** This method decides reaction to hearing <code>message</code>   *  from <code>sender</code>.   */  RealObject origSender;  protected void hearTell(RealObject sender, String msg) {}  protected void hearSay(RealObject sender, String msg) {    if (MRL.MRLConstants.MESSAGE_DEBUG_DEEP_MODE) System.out.println(self() +        " Heared : " + msg);  }  protected abstract void hear(RealObject sender, String msg);  protected void hearTellBin(RealObject sender, byte[] message) {};  protected void prepareForAct() {  }//  protected abstract int utteranceLimit();  protected final int utteranceLimit(){ return HEARING_LIMIT_OF_HUMANOID;}//  protected abstract int hearingLimit();  protected abstract int hearingLimit();  protected RCRSSProtocolSocket socket() {    return m_socket;  }  protected int time() {    return world.time();  }  private RealObject self() {    return world.self;  }  protected final Property distancePrp = new Property() {    public Object eval(Object obj) {      if(obj instanceof RealObject)        return new Integer(self().distance((RealObject) obj));      else if (obj instanceof Path)return new Integer(self().distance(((Path) obj).          position()));      else if (obj instanceof Partition) return new Integer(((Partition)obj).getDistanceToCenter((Partitionable)self().motionlessPosition()));      return null;    }  };  protected boolean submittedAction = false;  protected boolean submittedMoveAct = false;  private int m_numUtterance;  private int m_numHearing;  public void run() {    if (MRL.MRLConstants.WRITE_MODE) MRL.Utilities.Utils.initWriting();    boolean actSubmitted;    while (true) {      try{          Object data = socket().receive();          if (data instanceof KaSense) {            try{                if(MRL.MRLConstants.MESSAGE_DEBUG_MODE){                    System.out.println(self() + " , Heared : " + m_numHearing + " Messages In : " + time());                    System.out.println(self() + " , Said : " + m_numUtterance + " Messages In : " + time());                }                long point1=0, point2=0, point3=0;                cycleBegin = System.currentTimeMillis();                KaSense sense = (KaSense) data;                world.update(sense.selfAndMap, sense.time);                actSubmitted = false;                m_numUtterance = 0;                m_numHearing = 0;                point1 = System.currentTimeMillis();                prepareForAct();                point2 = System.currentTimeMillis();                try {                  act();                  point3 = System.currentTimeMillis();                  if (MRL.MRLConstants.DEBUG_MODE) System.out.println(self() +                      " , RESTED : " + world.time());                  rest();                }                catch (ActionCommandException sce)                {                    world.addReportedBICToPaths();                    world.clearReportedBlockadesInCycle();                    actSubmitted = true;                    hearedCivilianICC = false;                }                long ms = System.currentTimeMillis() - cycleBegin;                if (ms > maxHalfCycleTime)                        System.err.println("Agent("                                + getClass().getName()                                + "): "                                + self().id                                + " needs "                                + ms                                + " ms in Round "                                                    + time());                if(time() >= SIMULATING_TIME) break;            }catch(Exception ex){                System.err.println(ex);                socket().akRest(self().id);            }catch(Error err){                System.err.println(err);                socket().akRest(self().id);            }          }          else if (data instanceof KaHear)          {              KaHear hear = (KaHear) data;              RealObject sender = world.get(hear.senderId);              if (sender != self()&& m_numHearing++ < hearingLimit())                hear(sender, hear.message);          }          else if (data instanceof KaHearTell)          {                KaHearTell hearTell = (KaHearTell) data;                RealObject sender = world.get(hearTell.senderId);                if (sender != self() ) {                          hearTell(sender, hearTell.message);                          hearTellBin(sender, hearTell.messageBin);                }          }          else if (data instanceof KaHearSay)          {                KaHearSay hearSay = (KaHearSay) data;                RealObject sender = world.get(hearSay.senderId);                if (sender == null) {                    sender = world.AddCivilian(hearSay.senderId);                    if(MRL.MRLConstants.CIV_DEBUG_MODE) System.out.println(self() + " Heared A Civilian In : " + time());                }                if (sender != self()){                                    if(! (sender instanceof Civilian ) || mustHearMessage((Civilian)sender)){                                        if(m_numHearing ++ < hearingLimit()){                                            hearSay(sender, hearSay.message);                                        }                                    }                                }          }          else          {            throw new Error("receives an illegal packet.");          }        } catch(Exception ex){          System.err.println(ex);        }        catch (Error err){            System.err.println(err);        }    }  }  private boolean mustHearMessage(Civilian civ){      if(civ.position() != null) return false;      MotionlessObject mo = self().motionlessPosition();      if(mo instanceof Road){          return true;      }      else {          if(civ.selfHearedPositions.contains(mo) || civ.messageHearedPositions.contains(mo))            return false;          else              return true;      }  }  protected boolean hearedCivilianICC = false;  /** This exception is thrown in order to exit from {@link   *  act} method.   */  public static class ActionCommandException extends Exception {    public ActionCommandException() {      super();    }  }  protected void rest() throws ActionCommandException {    socket().akRest(self().id);    throw new ActionCommandException();  }  protected void say(String message) { // Does Constraint Should Be On Both SAY And TELL ?    if (MRL.MRLConstants.MESSAGE_DEBUG_DEEP_MODE) {      System.out.println(self() + " , Say : " + message);    }    if (message.length() > 248)throw new Error("Invalid Say Request : \n" +                                               message);    if (m_numUtterance++ < utteranceLimit()) {      socket().akSay(self().id, message);    }    else if (MRL.MRLConstants.MESSAGE_DEBUG_DEEP_MODE) {      System.out.println(self() + " , Say : " + message + " , Didn't Sent");    }  }  protected void tell(String message) {    if (MRL.MRLConstants.MESSAGE_DEBUG_DEEP_MODE) {      System.out.println(self() + " , Tell : " + message);    }    if (message.length() > 248) {      throw new Error("Invalid Tell Request : \n" + message);    }    if (m_numUtterance++ < utteranceLimit()) {      socket().akTell(self().id, message);    }    else if (MRL.MRLConstants.MESSAGE_DEBUG_DEEP_MODE) {      System.out.println(self() + " , Tell : " + message + " , Didn't Sent");    }  }    public final Condition IN_VISIBLE_DIST = new Condition (){        final int VISIBLE_DIST = MRL.Utilities.SimulatorsConstants.VISIBLE_DISTANCE;        public boolean eval(Object obj){            if(obj instanceof RealObject)                return Util.distance(self(), (RealObject)obj) <= VISIBLE_DIST;            else                throw new Error("Invalid Usage.");        }    };}

⌨️ 快捷键说明

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