📄 policeofficeagent.java
字号:
package MRL.Police;import java.net.*;import yab.agent.*;import yab.agent.object.*;import java.util.*;import MRL.Utilities.Partitioning.*;import MRL.Utilities.MessageManagement.CenterMessageManager;import MRL.Utilities.Utils;public class PoliceOfficeAgent extends AbstractPoliceOfficeAgent implements PoliceConstantsAndConditions { private CenterMessageManager messageManager; public PoliceOfficeAgent(InetAddress address, int port) { super(address, port); messageManager = new CenterMessageManager (socket (), world); socket().akAcknowledge(self().id); } protected void hear(RealObject sender , String msg) { // do nothing } protected void hearTellBin (RealObject sender, byte[] message) { if (!(sender instanceof PoliceOffice)) messageManager.importMessage (message, time (), sender); } //HEAVY MAPS : LATER //EASY MAPS : SOONER protected void prepareForAct() {// if(time() > 160 ) return; super.prepareForAct();// messageManager.sendMessages (); self().blockData.updateLockedByBlockade(); correctAvailablePolices(); switch (world.time()) { case 4: partitioningInit(); break; case PARTITIONING_START_TIME: int count = AssignEachPartitionToNearestPoliceForce();// int count = AssignPartitionToPoliceForces();// count += AssignRemainingPolicesToCurrentPartition();// DynamicAssign();// if (count != world.policeForces.size()) {// throw new Error("Partitioning Failed");// } if (MRL.MRLConstants.PARTITION_DEBUG_MODE) { System.out.println("Assignling Partitions Done"); ShowPolicePartitions(); } break; case TASK_ASSIGN_START_TIME: AssignTasks(); break; } messageManager.sendMessages (); } // ---------------------------------------------------------------- OMID protected void hearTell(RealObject sender , String msg) {// RealObject oSender = world.get(msg.Sender_ID()); /* switch (msg.type()) { case MSG_PARTITION_CLEARED: if(!(sender instanceof PoliceForce)) return; int newPartID = getNewPartIDUsingUnclearedParts(msg.sender_INDX()); // Assigns Police Force To Partition With Max ScoreDelta And Locked Agents if (newPartID != -1) { Partition partition = world.partitioner.getPartition(newPartID); PoliceForce pf = (PoliceForce) sender;// messagepool.SetPartition(pf,partition); } break; case MSG_PARTITION_CLEARED_AND_SEARCHED: if(!(sender instanceof PoliceForce)) return; int nPartID = getNewPartIDUsingUnSearchedParts(msg.sender_INDX()); if(nPartID == -1){ nPartID =getNewPartIDUsingPolicePopulation(msg.sender_INDX()); } if (nPartID != -1) { Partition partition = world.partitioner.getPartition(nPartID); PoliceForce pf = (PoliceForce) sender; world.messagepool.SetPartition(pf,partition); } break; default: break; } */ } public Condition hasMinPolice_C = new Condition(){ public boolean eval(Object obj){ if(getPoliceInPartCount((Partition)obj)>=MAX_POLICE_IN_PARTITION) return true; else return false; } }; public Condition hasMaxPolice_C = new Condition(){ public boolean eval(Object obj){ if(getPoliceInPartCount((Partition)obj)>=MAX_POLICE_IN_PARTITION) return true; else return false; } }; public void partitioningInit() { self().unassignedpartitions = new ArrayList(world. partitioner.getPartitions()); self().remainingpolices = new HashSet(world.policeForces); } private final Comparator PART_SCORE_COMP = new Comparator() { Partition part1 , part2 ; double p1s , p2s; int time; final Collection livings=world.humanoids ; public int compare(Object obj1 , Object obj2){ part1 = (Partition) obj1; part2 = (Partition) obj2; time = time(); p1s = part1.getScore(livings,time); p2s = part2.getScore(livings,time); if(p1s == p2s) return 0; else if( p1s > p2s) return 1; else return -1; } }; private final Comparator NUM_LIVING_COMP = new Comparator() { Partition part1 , part2 ; int p1l , p2l , time;// final Collection livings=world.humanoids ; public int compare(Object obj1 , Object obj2){ part1 = (Partition) obj1; part2 = (Partition) obj2; time = time();// p1l = Utils.getConditionCount(livings,part1.MO_OBJS_IN_PART_CND);// p2l = Utils.getConditionCount(livings,part2.MO_OBJS_IN_PART_CND); p1l = part1.getNumLiving(); p2l = part2.getNumLiving(); if(p1l == p2l) return 0; else if( p1l > p2l) return 1; else return -1; } }; private int AssignPartitionToPoliceForces(){ int res = 0; //Message msg; Partition part; PoliceForce pf; Partitionable p; for (Iterator it = self().availablePolices.iterator(); it.hasNext(); ) { pf = (PoliceForce) it.next(); if (pf.position() == null) { if (MRL.MRLConstants.PARTITION_DEBUG_MODE) { System.err.println( "Police Office Agent : Error , Null Police Force Position , Continuing"); } continue; } p = (Partitionable) pf.motionlessPosition(); part = world.partitioner.getPartition(p); if (part == null) { if (MRL.MRLConstants.PARTITION_DEBUG_MODE) { System.err.println( "Police Office Agent : Error , Cannot Find A Part , Continuing"); } continue; } if (self().policePartitionsMap.containsValue(part)) continue; //msg = world.messagepool.SetPartition(pf, part); messageManager.reportSetPartition(pf, part); self().policePartitionsMap.put(pf,part); self().unassignedpartitions.remove(part); res++; self().remainingpolices.remove(pf); it.remove(); //if (MRL.MRLConstants.PARTITION_DEBUG_MODE) { // System.out.println("Object Partitioned In POA : " + msg); //} } for(Iterator it = self().unassignedpartitions.iterator();it.hasNext();){ part = (Partition) it.next(); //do{ pf = (PoliceForce) part.getNearestMovingObject( self().availablePolices); if (pf == null) { if (MRL.MRLConstants.PARTITION_DEBUG_MODE) { System.err.println( "Police Office Agent : Error , Null Police Force , Continuing"); continue; } } //} while((pf == null||pf.buriedness()>0) &&! remainingpolices.isEmpty()); if(pf == null || pf.buriedness() > 0){ System.err.println("Partitioning Failed , No More Available Agents!"); return res; } //msg = world.messagepool.SetPartition(pf,part); messageManager.reportSetPartition(pf, part); //if (MRL.MRLConstants.PARTITION_DEBUG_MODE) { // System.out.println("Object Partitioned In POA : " + msg); //} self().policePartitionsMap.put(pf,part); self().remainingpolices.remove(pf); res++; self().availablePolices.remove(pf); it.remove(); } return res; } private int AssignEachPartitionToNearestPoliceForce() { // Returns Number Of Agent Assigned To Partitions int res = 0; Partition partition; PoliceForce policeforce; Collections.sort(self().unassignedpartitions,NUM_LIVING_COMP); for (Iterator it = self().unassignedpartitions.iterator(); it.hasNext(); ) { partition = (Partition) it.next(); policeforce = (PoliceForce) partition.getNearestMovingObject( self().availablePolices); if (policeforce == null) { if (MRL.MRLConstants.PARTITION_DEBUG_MODE) { System.err.println( "Police Office Agent : Error , Null Police Force , Continuing"); } continue; } //Message msg; //msg = world.messagepool.SetPartition(policeforce,partition); messageManager.reportSetPartition(policeforce, partition); //if (MRL.MRLConstants.PARTITION_DEBUG_MODE) { // System.out.println("Object Partitioned In POA : " + msg); //} self().policePartitionsMap.put(policeforce, partition); self().remainingpolices.remove(policeforce); self().availablePolices.remove(policeforce); res++; it.remove(); } return res; } /* private int DynamicAssign(){ int res = 0; // Must Detect Map Here. // Default Map For KobeStandard int rc = world.roads.size(); if(rc > 810 && rc < 830 ) { // KOBE// System.out.println(remainingpolices.size()); KobePartition(); } return res; } private boolean KobePartition(){ // remainingpolices , Partition part; PoliceForce pol; part = world.partitioner.getPartition(1); pol = (PoliceForce) part.getNearestMovingObject(remainingpolices); //AssignPartitionToPoliceForce(part,pol); return true; } private boolean AssignPartitionToPoliceForce(Partition part, PoliceForce pol) { // Returns Number Of Agent Assigned To Partitions //Message msg; //msg = world.messagepool.SetPartition(pol,part); messageManager.reportSetPartition(pol, part); //if (MRL.MRLConstants.PARTITION_DEBUG_MODE) { // System.out.println("Object Partitioned In POA : " + msg); //} policePartitionsMap.put(pol, part); remainingpolices.remove(pol); unassignedpartitions.remove(pol); return true; } private int AssignRemainingPolicesToPatitionWithMaxDelta(){ if (MRL.MRLConstants.PARTITION_DEBUG_MODE) { if (unassignedpartitions.size() != 0) { System.out.println("Error , Some Partitions Are Not Covered"); return 0; } } int res = 0; Partition partition; PoliceForce policeforce;// Message msg; Collection used = new ArrayList(); for (Iterator it = remainingpolices.iterator(); it.hasNext(); ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -