📄 policeforceagent.java
字号:
// Edited By Omid Aghazadehpackage MRL.Police;import java.util.*;import java.net.*;import yab.agent.*;import MRL.Utilities.Partitioning.*;import MRL.Utilities.*;import MRL.Utilities.MessageManagement.AgentNoStationMessageManager;import MRL.Utilities.MessageManagement.AgentMessageManager;import yab.agent.object.*;public class PoliceForceAgent extends AbstractPoliceForceAgent implements ConstantConditions , PoliceConstantsAndConditions{ private final List roadsCleared = new ArrayList(); protected final Comparator REPORTED_BLOCKADES_CMP = new Comparator() { final float reportedBICCEF = BLOCKADE_COMP_CURRENT_CYCLE_COEF; final float reportedBCEF = BLOCKADE_COMP_PREV_CYCLE_COEF; float val1, val2; Path path1, path2; public int compare(Object obj1, Object obj2) { path1 = (Path) obj1; path2 = (Path) obj2; val1 = reportedBICCEF * path1.getReportedBlockadesInCycle() + reportedBCEF * path1.getTotalReportedBlockades();// val1 += world.getAgentCountLockedInPath(path1) * LOCKED_BB_COEF; val1 += world.blockData.getAgentsLockedInPathCount(path1) * LOCKED_BB_COEF; if(path1 == world.getPrevPath()) val1 += PREV_PATH_BLOCKAD_THRESH; val2 = reportedBICCEF * path2.getReportedBlockadesInCycle() + reportedBCEF * path2.getTotalReportedBlockades(); val2 += world.blockData.getAgentsLockedInPathCount(path2) * LOCKED_BB_COEF; if(path2 == world.getPrevPath()) val2 += PREV_PATH_BLOCKAD_THRESH; return (int) (val2 * 100 - val1 * 100); } }; protected final Condition HAS_BLOCK_REP_C = new Condition() { public boolean eval(Object obj) { return ((Path) obj).getTotalReportedBlockades() > 0 || ((Path) obj).getReportedBlockadesInCycle() > 0; } }; /* public Comparator Far_Comp = new Comparator(){ int dist1 , dist2; public double compare(Object obj1,Object obj2){ dist1 = self().distance((RealObject)obj1); dist2 = self().distance((RealObject)obj2);// return Double.parseDouble(Integer.toString(dist1 - dist2)); return (double) (dist1-dist2)/100; } }; */ public PoliceForceAgent(InetAddress address, int port) { super(address, port); if (world.policeOffices.isEmpty()) messageManager = new AgentNoStationMessageManager(socket(), world); else messageManager = new AgentMessageManager(socket(), world); socket().akAcknowledge(self().id); } protected Condition SHOULD_CHECK_PATH_CND = new Condition() { public boolean eval(Object obj) { return ((Path) obj).shouldCheck(); } }; /** * todo : Conditions Added To Avoid Submitting Clearing On Open Roads Which Occures Rarely , But I Don't Know WHY ! * MayBe It's Because Of Messages. */ protected void clearHere() throws ActionCommandException { MotionlessObject pos = self().motionlessPosition(); if (pos instanceof Road) { Road road = (Road) pos; if (NOT_PASSABLE_C.eval(road)) { //reportRoadCleared(road); if (!roadsCleared.contains(road)) roadsCleared.add(road);// acted = true; if (MRL.MRLConstants.PF_DEBUG_DEEP_MODE) System.out.println(self() + " Try'n To Clear Road With Blocks : " + road.block()); if(self().submittedAction()){ int roadBlock = road.block(); int prevRoadBlock = road.block(time() -1); if(roadBlock != prevRoadBlock) clear(road); else System.err.println(self() + " , Tried To Clear A Road Without Blockades In : " + time()); } else clear(road); } } } protected void clearHereCompletely() throws ActionCommandException { MotionlessObject pos = self().motionlessPosition(); if (pos instanceof Road) { Road road = (Road) pos; if (road.isClearable()) { //reportRoadCleared(road); clear(road); } } } protected MotionlessObject getFarestObjectToSelf(Collection col) { int dist, bestmatch = 0; MotionlessObject mo, res = null; for (Iterator it = col.iterator(); it.hasNext(); ) { mo = (MotionlessObject) it.next(); dist = self().distance(mo); if (dist > bestmatch) { bestmatch = dist; res = mo; } } return res; } protected void updatePrevRouteShouldChecks() { if (prevRoute == null)return; Collection col = prevRoute.getObjects(); Object obj; Path path; for (Iterator it = col.iterator(); it.hasNext(); ) { obj = it.next(); if (obj instanceof Road) { path = world.getContainingPath((Road) obj); path.checkPathIsOpen(); } } } protected Route getNeedCheckVisitingRoute(MotionlessObject pos, Path path) { if (!path.shouldCheck())throw new Error( "Path Should'nt Be Already Visited!"); Collection needCheck = SHOULD_CHECK_ROAD_CND.extract(path); if (needCheck.isEmpty()) { if (!path.checkPathIsOpen())throw new Error("WHA!??"); } Route rt; if(path.isOneWayPath()){ rt = path.goToEndFromStart(path.getFirstNodeForOneWayPaths()); rt = rt.trimStartTo(pos); return rt; }else { if(path.getEdges().contains(pos)) return path.goToEndFromStart((Node)pos); Road road,res = null; int oldest = Integer.MAX_VALUE; for(Iterator it = needCheck.iterator();it.hasNext();){ road = (Road) it.next(); if(road.time() < oldest){ oldest = road.time(); res = road; } } rt = getRouteFromCurrentPos(path.getEdges(),CLEARING_COST_FUNCTION); Node end = (Node) rt.end, start; if(!rt.isContaining(res)){ start = end; } else{ start = end == path.getFirstNode() ? path.getLastNode() : path.getFirstNode(); } rt = path.goToEndFromStart(start); rt = rt.trimStartTo(pos); return rt; } } protected Route getClearingRoute(Path path) { MotionlessObject pos = self().motionlessPosition(); if(pos instanceof Building) pos = ((Building)pos).entrance(); if (path.IsContaining(pos)) { Route rt; rt = getNeedCheckVisitingRoute(pos,path); return rt; } Route toPathRT = null; if(path.isOneWayPath()){ toPathRT = getRouteFromCurrentPos(path.getFirstNodeForOneWayPaths(),MOVING_COST_FUNCTION); } else{ toPathRT = getRouteFromCurrentPos(path.getEdges(),MOVING_COST_FUNCTION); } Node end = (Node) toPathRT.end; Route pathrt = path.goToEndFromStart(end); Route res = pathrt.merge(toPathRT);// modifyOriginToCurrentPosition(res); return res; } protected void clearPath() throws ActionCommandException { Path cPath = world.getCurrentPath(); if (cPath != null && !cPath.checkPathIsOpen()) { if (Utils.getConditionCount(cPath, SHOULD_CHECK_ROAD_CND) == 0) return; Route rp = getClearingRoute(cPath); move(rp); } } protected void clearPathCompletely() throws ActionCommandException { clearHereCompletely(); Path cPath = world.getCurrentPath(); if(cPath == null) return; Collection col = CLEARABLE_C.extract(cPath); if (col.isEmpty())return; move(col);// Road road = cPath.getFirstRoadToCheck(self().motionlessPosition());// policeMove(road); } private Path getMostValuablePathImplementingTraffic(List paths, int indx){ List notInTraff = NOT_IN_TRAFFIC_C.extract(paths); if(! notInTraff.isEmpty()){ if( indx >= notInTraff.size()) return (Path) notInTraff.get(notInTraff.size()-1); else return (Path) notInTraff.get(indx); } else{ if(indx >= paths.size()) return (Path) paths.get(paths.size()-1); else return (Path) paths.get(indx); } } private Path m_lastReportedPathTarget = null; protected void moveReportedBlockades() throws ActionCommandException { List res; Path path = null; Route rt = null; if(m_lastReportedPathTarget != null){ checkPathIsClearedOrMove(m_lastReportedPathTarget,false); m_lastReportedPathTarget = null; } if (workingPartition() != null) { res = self().reportedBlockadesIP; if (res.isEmpty())return;// List notInTraff = NOT_IN_TRAFFIC_C.extract(res);// if(!notInTraff.isEmpty())// res = notInTraff; path = (Path) res.get(0); if(path == null) return; world.setPrevPath(path); m_lastReportedPathTarget = path; rt = getClearingRoute(path); move(rt); } else { res = self().reportedBlockades; if (res.isEmpty())return; List notInTraff = NOT_IN_TRAFFIC_C.extract(res); if(!notInTraff.isEmpty()) res = notInTraff; List agents; if(self().freePolices.isEmpty() && ! self().taskHandler.isAnyTaskAssignedByCenter()){ System.out.println("Error , No Free Police In Time : " + time()); agents = world.policeForces; } else{// agents = self().freePolices; agents = taskHandler().getWithoutTaskFreePolices(); } path = choosePathToClear(res,agents); if(path == null) return; world.setPrevPath(path); m_lastReportedPathTarget = path; rt = getClearingRoute(path); move(rt); } } protected void correctReportedBlockade() { Path path; for (Iterator it = self().reportedRoads.iterator(); it.hasNext(); ) { Road road = (Road) it.next(); path = world.getContainingPath(road); self().totalReportedRoads.add(road); self().totalReportedPaths.add(path); if(workingPartition() != null && workingPartition().ML_OBJS_IN_PART_CND.eval(path)) self().totalReportedPathsIP.add(path); if (path.checkPathIsOpen()) { //System.out.println("Reported Road Cleared In Part : " + workingPartition().getID() + " , ID : " + self().id); self().reportedBlockades.remove(path); self().reportedBlockadesIP.remove(path); it.remove(); continue; } else { if (!self().reportedBlockades.contains(path)) self().reportedBlockades.add(path); if (workingPartition() != null) { if (workingPartition().ML_OBJS_IN_PART_CND.eval(path)) { //System.out.println("Reported Road NOT In My Part : " + workingPartition().getID() + " , ID : " + self().id); if(!self().reportedBlockadesIP.contains(path)) self().reportedBlockadesIP.add(path); } else{ self().reportedBlockadesIP.remove(path); } } } } if (workingPartition() != null) { Collections.sort(self().reportedBlockadesIP, REPORTED_BLOCKADES_CMP); } else{ Collections.sort(self().reportedBlockades, REPORTED_BLOCKADES_CMP); }// if(MRL.MRLConstants.ROUTING_DEBUG_DEEP_MODE)// System.out.println(self() + " , Time : " + time() + " , RepRoads : " +// self().reportedBlockades.size() +// (workingPartition() != null ?// " , RepBIP : " + self().reportedBlockadesIP.size() : "") + " , RepB : " + self().reportedBlockades.size()); } private Path m_lastTargetPath = null; protected void searchBlockades() throws ActionCommandException { if(!shouldSearchBlocks()) return;// choosePath(); Route res = null; if(m_lastTargetPath != null &&! m_lastTargetPath.checkPathIsOpen() && ! world.isInTraffic(m_lastTargetPath)){ res = getExploringRoute(m_lastTargetPath); move(res); } m_lastTargetPath = null; List toCheckPaths = null; if (workingPartition() != null) { toCheckPaths = (world.pathsIP); } else { toCheckPaths = (world.paths); }// if(Utils.getConditionCount(toCheckPaths,SHOULD_CHECK_PATH_CND)==0) return; toCheckPaths = SHOULD_CHECK_PATH_CND.extract(toCheckPaths); if(workingPartition() != null){ List edgePaths = new ArrayList(toCheckPaths); edgePaths.retainAll(workingPartition().getEdgeObjects()); if(!edgePaths.isEmpty()) toCheckPaths = edgePaths; } if(toCheckPaths.isEmpty()) return; List notInTraff = NOT_IN_TRAFFIC_C.extract(toCheckPaths); if(!notInTraff.isEmpty()) toCheckPaths = notInTraff; List ratherNear = distancePrp.gte(LB_FOR_SEARCH_BLOCKADES).and(distancePrp.lte(UB_FOR_SEARCH_BLOCKADES)).extract(toCheckPaths);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -