📄 path.java
字号:
package MRL.Utilities.Partitioning;import java.util.*;import yab.agent.object.*;import yab.agent.*;import MRL.Police.*;import MRL.Utilities.Utils;public final class Path extends ArrayList implements Partitionable, MRL.Utilities.ConstantConditions , yab.agent.Constants{ public static final float curCycReportedBlockCoef = PoliceConstantsAndConditions.PATH_VAL_CURRENT_CYCLE_COEF; public static final float prevCycReportedBlockCoef = PoliceConstantsAndConditions.PATH_VAL_PREV_CYCLE_DIFF; private final float visHumansCoef = 0.5f; public final Report clearReport = new Report(); private final int m_hashCode; public int hashCode(){// if(m_hashCode==1){// Random rnd = new Random(world.self.id);// do{// m_hashCode = m_hashCode *2 + rnd.nextInt();// }while(world.get(m_hashCode) != null) ;// } return m_hashCode; } public static final Condition NOT_CLEARED_C = new Condition() { public boolean eval(Object obj) { return ( (Path) obj).shouldCheck(); } }; public final Condition IS_CONTAINING_C = new Condition() { public boolean eval(Object obj) { return IsContaining( (MotionlessObject) obj); } }; public final Condition IS_IN_PATH_C = new Condition() { // Doesnt Check The Time MovingObject mo; public boolean eval(Object obj) { mo = (MovingObject) obj; return (mo.position() != null && IsContaining(mo.motionlessPosition())); } }; private boolean hasBlocks = true; private float tresh = 0.0f; private float value = 0.0f; private final DisasterSpace world; private boolean m_firstTime = true; // These All Should Be Change , It's Foolish ! private Node m_firstNode; private Road m_firstRoad; private Road m_lastRoad; private Node m_lastNode; private float [] vi; public static final int DTOREF = 0, DTOFIRE = 1, ATCOUNT = 2, FBCOUNT = 3, PFCOUNT = 4, CIVCOUNT = 5, DTOSELF = 6, BLOCKREP = 7 , CONPATH = 8 , EDGEPATH=9; public static final int valCount = 10 ; private float[] vali = new float[valCount]; private static final float[] nViFact = new float[valCount]; private int reportedBlockades=0; private int reportedBlockadesIS = 0; ///TEST PURPOSE private static boolean firstTime = true; private static int firstID = -1; // public Path(DisasterSpace world,int hashCode) { this.world = world; initVals(); ai = world.ai;// hashCode(); m_hashCode = hashCode; } private Set pathToHead = new HashSet(); // set of path that connected to the first Node of path private Set pathToTail = new HashSet(); // set of path that connected to the Last Node of path private boolean IsCandidate = false; //private float [] alpha = { 0.85f, 0.49f, 0.3f, 2.46f, 1.46f, 0.72f, 0.0f, 1.61f, 1000.0f, 1000.0f , 1000.0f}; private boolean isEdgePath = false; public void setEdgePath(){ isEdgePath = true; } public boolean isEdgePath(){ return isEdgePath; } private final float [] ai; private void initVals() { vi = new float[valCount];// ai = world.ai;// setAlphas(); resetVals(); } public static void setAlphasFromFile(float [] ai){ float[] res = Utils.getAlphasFromFile(); if (res == null) setAlphas1(ai); else for (int i = 0; i < valCount; i++) ai[i] = res[i]; }// public static void setAlphas(float [] ai) {// ai[DTOREF] = 1f;// ai[DTOFIRE] = 1f;// ai[ATCOUNT] = 5f;// ai[PFCOUNT] = 5f;// ai[FBCOUNT] = 5f;// ai[CIVCOUNT] = 1f;// ai[DTOSELF] = 1f;// ai[BLOCKREP] = 1.5f;// ai[CONPATH] = 0.75f;// } public static void setAlphas(float [] ai) { ai[DTOREF] = 1.5f; ai[DTOFIRE] = 1.5f; ai[ATCOUNT] = 5f; ai[PFCOUNT] = 5f; ai[FBCOUNT] = 5f; ai[CIVCOUNT] = 1f; ai[DTOSELF] = 3f; ai[BLOCKREP] = 2f; ai[CONPATH] = 1f; ai[EDGEPATH] = 1f; } public static void setAlphas1(float [] ai) { for(int i = 0 ; i < valCount ; i++) ai[i] = 1f; } public void setTresh(float val) { tresh = val; } public void addPathToHead(Path path) { pathToHead.add(path); } public void addPathToTail(Path path) { pathToTail.add(path); } public Set getPathToHead() { return pathToHead; } public Set getPathToTail() { return pathToTail; } /// NEW public boolean checkOneWayPath(){ Road road; m_oneWayPath = false; for(Iterator it = this.iterator();it.hasNext();){ road = (Road) it.next(); if(road.linesToHead() == 0 || road.linesToTail() == 0) { m_oneWayPath = true;// System.out.println(this + " IS One Way Path"); Route tr = goToEndFromStart(getFirstNode()); int hi = tr.getIndexOf(road.head()) , ti = tr.getIndexOf(road.tail()); if(road.linesToHead() ==0){ // Should Go To Road's Tail if(hi > ti){ // Head Is Farer To Last Node m_firstNodeForOneWay = getFirstNode(); } else{ // Tail Is Farer To Last Node m_firstNodeForOneWay = getLastNode(); } } else{ // Should Go To Road's Head if(hi > ti){ // Head Is Farer To Last Node m_firstNodeForOneWay = getLastNode(); } else{ // Tail Is Farer To Last Node m_firstNodeForOneWay = getFirstNode(); } } break; } } return m_oneWayPath; } private Node m_firstNodeForOneWay=null; public Node getFirstNodeForOneWayPaths(){// if(m_firstNodeForOneWay==null){// if(getFirstRoad().head() == getFirstNode()){// if(getFirstRoad().linesToHead() == 0){// m_firstNodeForOneWay = getFirstNode();// }// else{// m_firstNodeForOneWay = getLastNode();// }// }// else{ // tail// if(getFirstRoad().linesToTail() == 0){// m_firstNodeForOneWay = getFirstNode();// }// else{// m_firstNodeForOneWay = getLastNode();// }// }// } return m_firstNodeForOneWay; } private Node m_lastNodeForOneWay=null; public Node getLastNodeForOneWayPaths(){ if(m_lastNodeForOneWay==null){ if(getFirstNodeForOneWayPaths()==getFirstNode()) m_lastNodeForOneWay = getLastNode(); else m_lastNodeForOneWay = getFirstNode(); } return m_lastNodeForOneWay; } private boolean m_oneWayPath = false; public boolean isOneWayPath(){ return m_oneWayPath; } /// NEW private final Set m_edges = new HashSet(); public Collection getEdges() { if(m_edges.isEmpty()){ m_edges.add(getFirstNode()); m_edges.add(getLastNode()); } return m_edges; } private Collection m_cPaths=null; public Collection getConnectedPaths(){ if(m_cPaths == null){ m_cPaths = new HashSet(); m_cPaths.addAll(pathToHead); m_cPaths.addAll(pathToTail); } return m_cPaths; } public void submitChanges() { Road road = null; Node hN, tN; Iterator it; Collection finRoads = new HashSet(); Collection finNodes = new HashSet(); for (it = this.iterator(); it.hasNext(); ) { road = (Road) it.next(); if (road.getConnectedRoads().size() == 2)continue; finRoads.add(road); } if (finRoads.isEmpty() || finRoads.size() > 2) { System.out.println("ERROR PATH IS BULLSHIT"); return; } for (it = finRoads.iterator(); it.hasNext(); ) { road = (Road) it.next(); hN = (Node) road.head(); tN = (Node) road.tail(); if (hN.getConnectedRoads().size() != 2) finNodes.add(hN); if (hN.getConnectedRoads().size() != 2) finNodes.add(tN); } if (finNodes.size() != 2) { System.out.println("ERROR , PATH NODE Is Incorrect"); return; } if (finRoads.size() == 1) { this.m_firstRoad = road; this.m_lastRoad = road; } else { it = finRoads.iterator(); m_firstRoad = (Road) it.next(); m_lastRoad = (Road) it.next(); } it = finNodes.iterator(); m_firstNode = (Node) it.next(); m_lastNode = (Node) it.next(); } private int m_id = Integer.MAX_VALUE; public int getID() { if (m_id == Integer.MAX_VALUE) { Road road; for (Iterator it = this.iterator(); it.hasNext(); ) { road = (Road) it.next(); if (road.id < m_id) m_id = road.id; } } return m_id; } public float getValue() { return value + tresh ; } public void setLastNode(Node node) { m_lastNode = node; } public Node getLastNode() { return m_lastNode; } public void setLastRoad(Road road) { m_lastRoad = road; } public Road getLastRoad() { return m_lastRoad; } public void setFirstRoad(Road road) { m_firstRoad = road; } public Road getFirstRoad() { return m_firstRoad; } public void setFirstNode(Node n) { m_firstNode = n; } public Node getFirstNode() { return m_firstNode; } /* public void setHasBlocks(boolean val) { hasBlocks = val; } */ public boolean shouldCheck() { return hasBlocks; } public boolean isCandidate() { return IsCandidate; } private void resetVals() {// vi[DTOREF] = Integer.MAX_VALUE;// vi[DTOFIRE] = Integer.MAX_VALUE;// vi[ATCOUNT] = 0;// vi[PFCOUNT] = 0;// vi[FBCOUNT] = 0;// vi[CIVCOUNT] = 0;// vi[DTOSELF] = Integer.MAX_VALUE;// vi[BLOCKREP] = 0;// vi[CONPATH] = 0;// vi[EDGEPATH] = 0; value = 0f; absValue =0f; for(int i = 0 ; i< valCount ; i++) vali[i] = 0; } public int x() { return position().x(); } public int y() { return position().y(); } private MotionlessObject middle = null; public MotionlessObject position() { if (middle == null) setMiddle(); return middle; } public MotionlessObject getMiddle() { if (middle == null) setMiddle(); return middle; } public int getLenght() { int length = 0; Road road; for (int i = 0; i < this.size(); i++) { road = (Road)this.get(i); length += road.length(); } return length; } private Collection m_containingObjs = new HashSet(); public Collection getContainingObjects() { // Includes Roads if (m_containingObjs.isEmpty()) { Road road; for(Iterator it = this.iterator();it.hasNext();){ road = (Road) it.next(); m_containingObjs.add(road); m_containingObjs.addAll(road.neighborhood()); m_containingObjs.addAll(road.getConnectedBuildings()); } } return m_containingObjs; } private List m_connectedBuildings = new ArrayList(); public List getConnectedBuildings() { if(m_connectedBuildings.isEmpty()){ m_connectedBuildings.addAll(BUILDING_C.extract(getContainingObjects())); } return m_connectedBuildings; } public boolean IsContaining(MotionlessObject mo) { // Includes building/Nodes In Head And Tail Of Path Also return getContainingObjects().contains(mo); /* Road road; if (mo instanceof Road) { /*for (Iterator it = this.iterator(); it.hasNext(); ) { road = (Road) it.next(); if (road == mo)return true; } return false; return this.contains(mo); } else { Node node = null; if (mo instanceof Building) { node = (Node) ( (Building) mo).entrance(); } else if (mo instanceof Node) { node = (Node) mo; } for (Iterator it = this.iterator(); it.hasNext(); ) { road = (Road) it.next(); if (road.head() == node || road.tail() == node)return true; } return false; } */ } public boolean isContainingRoute(Route route) { return getRouteFromHeadNodeToTailNode().isContaining(route); // Regardless Of Sequence ... } private void setMiddle() { // Can Be Improved Node first = getFirstNode(), last = getLastNode(); Road current; /* Road first = getFirstRoad(),last = getLastRoad(); int d1 = first.head().distance(last.head()) , d2 = first.head().distance(last.tail()) , d3 = first.tail().distance(last.head()) , d4 = first.tail().distance(last.tail()); int max = Util.max(Util.max(d1,d2),Util.max(d3,d4)); */ int bestmatch = Util.distance(first, last), distance; MotionlessObject res = this.getFirstRoad(); for (Iterator it = this.iterator(); it.hasNext(); ) { current = (Road) it.next(); distance = Util.max(Util.distance(first.x(), first.y(), current.x(), current.y()), Util.distance(last.x(), last.y(), current.x(), current.y())); if (distance < bestmatch) { res = current; bestmatch = distance; } } middle = res; } private int maxDistToRef() { int maxDist = 0, dist; Path path; Building bldg; for (Iterator it = world.paths.iterator(); it.hasNext(); ) { path = (Path) it.next(); for (Iterator it2 = world.refuges.iterator(); it2.hasNext(); ) { bldg = (Building) it2.next(); dist = Util.distance(bldg.x(), bldg.y(), path.x(), path.y()); if (dist > maxDist) maxDist = dist; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -