📄 policeofficeagent.java
字号:
policeforce = (PoliceForce) it.next(); partition = world.partitioner.getPartition( (Partitionable) policeforce. motionlessPosition()); //msg = world.messagepool.SetPartition(policeforce,partition); messageManager.reportSetPartition(policeforce,partition); //if (MRL.MRLConstants.PARTITION_DEBUG_MODE) { // System.out.println("Object Partitioned In POA : " + msg); //} policePartitionsMap.put(policeforce, partition); used.add(policeforce); } remainingpolices.removeAll(used); used = null; return res; } */ /** * * todo Aghazadeh : Must Check Not To Assign More Than 3 Polices To A Partition */ /* private int AssignRemainingPolicesToCurrentPartition() { 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(); ) { policeforce = (PoliceForce) it.next(); partition = world.partitioner.getPartition( (Partitionable) policeforce. motionlessPosition()); if(fullCnd.eval(partition)) { List notFull = fullCnd.not().extract(world.partitioner.getPartitions()); partition = (Partition) policeforce.distancePrp.min(notFull); } //msg = world.messagepool.SetPartition(policeforce,partition); messageManager.reportSetPartition(policeforce,partition); //if (MRL.MRLConstants.PARTITION_DEBUG_MODE) { // System.out.println("Object Partitioned In POA : " + msg); //} res++; policePartitionsMap.put(policeforce, partition); used.add(policeforce); } remainingpolices.removeAll(used); used = null; return res; } */ private int getNewPartIDUsingPolicePopulation(int senderINDX){ int best=Integer.MAX_VALUE; PoliceForce pf = (PoliceForce) world.agents.get(senderINDX); Partition part,res=null;// int cnt = 0 , partSize = world.partitioner.getPartitions().size() - searchedPartitions.size(); int cnt; for(Iterator it = world.partitioner.getPartitions().iterator();it.hasNext();){ part = (Partition) it.next(); cnt=getPoliceInPartCount(part); if(cnt<best && ! hasMaxPolice_C.eval(part)){ res = part; best = cnt; } } if (res != null) { self().policePartitionsMap.remove(pf); self().policePartitionsMap.put(pf, res); return res.getID(); } return -1; } private int getNewPartIDUsingUnSearchedParts(int senderINDX){ PoliceForce pf = (PoliceForce) world.agents.get(senderINDX); Partition part = (Partition) self().policePartitionsMap.get(pf); self().searchedPartitions.add(part); List parts = new ArrayList(world.partitioner.getPartitions()); parts.removeAll(self().searchedPartitions);// int cnt = 0 , partSize = world.partitioner.getPartitions().size() - searchedPartitions.size(); Collections.shuffle(parts); do{ if(parts.isEmpty()) return -1; part = (Partition) parts.remove(0);// part = getNearestUnusedPartition(searchedPartitions, pf); }while (hasMaxPolice_C.eval(part)); if (part != null) { self().policePartitionsMap.remove(pf); self().policePartitionsMap.put(pf, part); return part.getID(); } return -1; } protected int getPoliceInPartCount(Partition part){ int res = 0; for(Iterator it = self().policePartitionsMap.keySet().iterator();it.hasNext();){ if((Partition)self().policePartitionsMap.get(it.next()) == part) res ++; } return res; } public final Comparator scoreDeltaComp = new Comparator() { double val1,val2; public int compare(Object obj1, Object obj2) { val1 = ((Partition)obj1).getScoreDelta(time()); val2 = ((Partition)obj2).getScoreDelta(time()); return (int) ((val1 - val2) * 1000000); } }; /** * * todo Aghazadeh : Complete LockedBBAgent's Effect */ private int getNewPartIDUsingUnclearedParts(int senderINDX){ PoliceForce pf = (PoliceForce) world.agents.get(senderINDX); Partition part = (Partition) self().policePartitionsMap.get(pf); self().clearedPartitions.add(part); List unclearedParts = new ArrayList(world.partitioner.getPartitions()); unclearedParts.removeAll(self().clearedPartitions); Collections.sort(unclearedParts,scoreDeltaComp); List res = unclearedParts; /* Collection lockedRoads = world.lockedBBAgents.values(); if(! lockedRoads.isEmpty()){ List hasLBBAParts=new ArrayList(); for(Iterator it = unclearedParts.iterator();it.hasNext();){ part = (Partition) it.next(); if(! part.ML_OBJS_IN_PART_CND.extract(lockedRoads).isEmpty()) hasLBBAParts.add(part); } if(! hasLBBAParts.isEmpty()){ Collections.sort(hasLBBAParts,scoreDeltaComp); res = hasLBBAParts; } } */ do { // part = getNearestUnusedPartition(clearedPartitions,pf); if(res.size() == 0 ) return -1; part = (Partition) res.remove(0); } while (hasMaxPolice_C.eval(part)); if(part != null ) { self().policePartitionsMap.remove(pf); self().policePartitionsMap.put(pf,part); return part.getID(); } return -1; } private Partition getNearestUnusedPartition(Collection used,PoliceForce pf){ int bestmatch=Integer.MAX_VALUE,dist; Partition res = null; Partition part; Collection parts = new HashSet(world.partitioner.getPartitions()); parts.removeAll(used); parts.removeAll(hasMinPolice_C.extract(parts)); MotionlessObject mp = pf.motionlessPosition(); for(Iterator it = parts.iterator();it.hasNext();){ part = (Partition)it.next(); dist = part.getDistanceToCenter(mp); if(dist<bestmatch){ bestmatch=dist; res = part; } } parts = null; return res; } public void act() throws ActionCommandException { } private void ShowPolicePartitions() { String res = ""; PoliceForce pf; System.out.println("--- Showing Police Partition Info : "); for (Iterator it = self().policePartitionsMap.keySet().iterator(); it.hasNext(); ) { pf = (PoliceForce) it.next(); res += pf.id + " At " + self().policePartitionsMap.get(pf) + "\n"; } System.out.println(res); System.out.println("--- End Of Police Partition Info"); } private void ShowPoliceTasks(){ PoliceForce pf; System.out.println("--- Showing Police Tasks Info :"); for(Iterator it = self().policeTaskMap.keySet().iterator();it.hasNext();){ pf = (PoliceForce) it.next(); // System.out.println(pf + " , Current Task : " + ((Integer)self().policeTaskMap.get(pf)).intValue() + " , Task Assigned In : " + ((Integer)self().policeTaskTimeMap.get(pf)).intValue()); } // System.out.println("--- End Of Police Tasks Info :"); } private void setTaskToPoliceForce(PoliceForce pf , int task){ self().policeTaskMap.put(pf,task); self().policeTaskTimeMap.put(pf,time()); messageManager.reportTask(pf,task); } private void AssignTasks(){ AssignToClearRefuge(); AssignToClearNearFire(); } private void AssignToClearRefuge(){ PoliceForce pf; Collection av = self().availablePolices; if(av.isEmpty()) return; int lastTask,lastTime; for(int i = 0 ; i < INITIAL_FREE_POL_TO_CLEAR_REFS ; i++){ pf = (PoliceForce) getMinDistFromFirst(av,world.refuges); if(pf!= null){// lastTask = getLastTaskAssignedToPolice(pf);// lastTime = getLastTimeTaskAssignedToPolice(pf);// if(lastTask != -1){ if(doesPoliceHasTask(pf)){ continue; } if(MRL.MRLConstants.POLICE_TASK_DEBUG_MODE) System.out.println(self() + " , Assigned REFUGE Cleaning To : " + pf ); setTaskToPoliceForce(pf,TASK_MOVE_NEAR_REFUGE_PATH); av.remove(pf); } else break; } } private void AssignToClearNearFire(){ PoliceForce pf; Collection av = self().availablePolices; if(av.isEmpty()) return; int lastTask,lastTime; for(int i = 0 ; i < INITIAL_FREE_POL_TO_CLEAR_NEAR_FIRE ; i++){ if(world.burningBuildings.isEmpty()){ pf = (PoliceForce) av.iterator().next(); System.out.println(self() + " Empty Firey Buildings , Assigning Randomly !"); } else pf = (PoliceForce) getMinDistFromFirst(av,world.burningBuildings); if(pf!= null){// lastTask = getLastTaskAssignedToPolice(pf);// lastTime = getLastTimeTaskAssignedToPolice(pf);// if(lastTask != -1){ if(doesPoliceHasTask(pf)){ continue; } if(MRL.MRLConstants.POLICE_TASK_DEBUG_MODE) System.out.println(self() + " , Assigned Near Fire Cleaning To : " + pf ); setTaskToPoliceForce(pf,TASK_MOVE_NEAR_FIRE_PATH); av.remove(pf); } else break; } } public RealObject getMinDistFromFirst(Collection first , Collection second){ int dist , best = Integer.MAX_VALUE; RealObject res=null,rof,ros; for(Iterator it = first.iterator();it.hasNext();){ rof = (RealObject) it.next(); for(Iterator it2 = second.iterator();it2.hasNext();){ ros = (RealObject) it2.next(); dist = rof.distance(ros); if(dist < best){ best = dist; res = rof; } } } return res; } protected void correctAvailablePolices(){ if(self().remainingpolices == null ) return; PoliceForce pf; self().availablePolices.clear(); for(Iterator it = self().remainingpolices.iterator();it.hasNext();){ pf = (PoliceForce) it.next(); if(pf.buriedness() > 0) continue; if(pf.hp() <= 0 ) { it.remove(); continue; } self().availablePolices.add(pf); } } protected int getCurrentTaskTimeAssignedToPolice(PoliceForce pf){ if(self().policeTaskTimeMap.containsKey(pf)){ return ((Integer)self().policeTaskTimeMap.get(pf)).intValue(); } else return -1; } protected int getCurrentTaskAssignedToPolice(PoliceForce pf){ if(self().policeTaskMap.containsKey(pf)){ return ((Integer)self().policeTaskMap.get(pf)).intValue(); } else return -1; } protected int getLastTimeTaskAssignedToPolice(PoliceForce pf){ int indx = self().policeAssignedTask_Pols.lastIndexOf(pf); if(indx == -1 ) return -1; else return ((Integer)self().policeAssignedTask_Times.get(indx)).intValue(); } protected int getLastTaskAssignedToPolice(PoliceForce pf){ int indx = self().policeAssignedTask_Pols.lastIndexOf(pf); if(indx == -1 ) return -1; else return ((Integer)self().policeAssignedTask_Tasks.get(indx)).intValue(); } protected boolean doesPoliceHasTask(PoliceForce pf){ return self().policeTaskMap.keySet().contains(pf); }} // ---------------------------------------------------------------- OMID
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -