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

📄 groundcontrolleragent.java.svn-base

📁 這是一個JAVA語言寫的多代理人程式用來模擬飛機起飛或是降落的程式
💻 SVN-BASE
字号:
package agents;import interfaces.GroundControllerInterface;import interfaces.LocalControllerInterface;import interfaces.PilotInterface;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.List;import java.util.Map;import world.Gate;import world.Plane;import world.Runway;import world.Strip;import world.Taxiway;import world.Tower;import base.Agent;import base.World;import base.WorldObject;import base.WorldObjectState;import base.WorldVector;import dijkstra.DijkstraAlgorithm;import dijkstra.Edge;import dijkstra.Node;public class GroundControllerAgent extends Agent implements GroundControllerInterface { 		public List<Strip> strips;	public Tower tower;	public String name;		public GroundControllerAgent(String new_name, Tower new_tower) {		super();		strips = new ArrayList<Strip>();		name = new_name;		tower = new_tower;		tower.ctrlAssumeGroundControllerRole(this);	}		public void msgConfirmPushBack(String callNumber, Runway takeoffRunway,			Gate gate) {		print("Received msgConfirmPushBack");		Strip temp_strip;		if ((temp_strip = get_strip(callNumber)) != null)			temp_strip.flight_state = Strip.FlightState.GC_CONFIRMED_PUSH_BACK;				if (takeoffRunway != null)				temp_strip.runway = takeoffRunway;				stateChanged();	}	public void msgConfirmTaxiLandingPath(String callNumber,			List<Taxiway> taxiInstructions) {		print("Received msgConfirmTaxiLandingPath");		Strip temp_strip;		if ((temp_strip = get_strip(callNumber)) != null)			temp_strip.flight_state = Strip.FlightState.GC_CONFIRMED_TAXI_LANDING_PATH;				stateChanged();			}	public void msgConfirmTaxiTakeoffPath(String callNumber,			List<Taxiway> path, LocalControllerInterface lc) {		print("Received msgConfirmTaxiTakeoffPath");		Strip temp_strip;		if ((temp_strip = get_strip(callNumber)) != null)			temp_strip.flight_state = Strip.FlightState.GC_CONFIRMED_TAXI_TAKEOFF_PATH;				stateChanged();			}	public void msgLeavingRunway(String callNumber, Runway lastPath, Gate gate) {		print("Received msgLeavingRunway");		print(gate.toString());		Strip temp_strip;		print(callNumber);		for(Strip strip : strips) {			print(strip.callNum);		}		if ((temp_strip = get_strip(callNumber)) != null)			temp_strip.flight_state = Strip.FlightState.GC_LEAVING_RUNWAY;				if (gate != null)			temp_strip.gate = gate;		if (lastPath != lastPath)			temp_strip.runway = lastPath;		stateChanged();			}	public void msgNewArrivalStrip(Strip newArrival) {		print("Received msgNewArrivalStrip");		strips.add(newArrival);			stateChanged();	}	public void msgNewDepartureStrip(Strip newDeparture) {		print("Received msgNewDepartureStrip");		strips.add(newDeparture);		stateChanged();			}	public void msgReadyForTaxi(String callNumber, Taxiway currentPosition) {		print("Received msgReadyForTaxi");		Strip temp_strip;		if ((temp_strip = get_strip(callNumber)) != null)			temp_strip.flight_state = Strip.FlightState.GC_READY_FOR_TAXI;				if (currentPosition != null)			temp_strip.current_taxiway = currentPosition;				stateChanged();			}	public void msgRequestPushBack(String callNumber, Gate gate) {		print("Received msgRequestPushBack");		Strip temp_strip;		if ((temp_strip = get_strip(callNumber)) != null)			temp_strip.flight_state = Strip.FlightState.GC_REQUEST_PUSH_BACK;				if (gate != null)			temp_strip.gate = gate;				stateChanged();			}		public boolean pickAndExecuteAnAction() {				Strip temp_strip;		if ((temp_strip = get_strip(Strip.FlightState.GC_REQUEST_PUSH_BACK)) != null) {			act_give_push_back(temp_strip);			return true;		}		else if ((temp_strip = get_strip(Strip.FlightState.GC_READY_FOR_TAXI)) != null) {			act_give_taxi_takeoff_path(temp_strip);			return true;		}		else if ((temp_strip = get_strip(Strip.FlightState.GC_LEAVING_RUNWAY)) != null) {			act_give_taxi_landing_path(temp_strip);			return true;		}		else if ((temp_strip = get_strip(Strip.FlightState.GC_CONFIRMED_TAXI_LANDING_PATH)) != null) {			temp_strip.flight_state = Strip.FlightState.NO_ACTION;			return true;		}		return false;	}		public void act_give_taxi_landing_path(Strip temp_strip) {		temp_strip.flight_state = Strip.FlightState.NO_ACTION;		Plane temp_plane = World.get_plane(temp_strip.callNum);				List<Taxiway> taxiways = do_get_taxi_landing_path(temp_strip);		if (!(taxiways.get(0) instanceof Runway))			taxiways.add(0, temp_strip.runway);				System.out.println("taxi landing path: " + taxiways);				PilotInterface temp_pilot = temp_plane.msgPilot();		print("Sending pilot msgTaxiLandingPath");				temp_pilot.msgTaxiLandingPath(taxiways);	}			public void act_give_push_back(Strip temp_strip) {		temp_strip.flight_state = Strip.FlightState.NO_ACTION;		Plane temp_plane = World.get_plane(temp_strip.callNum);		PilotInterface temp_pilot = temp_plane.msgPilot();		print("Sending pilot msgGivePushBack");		temp_pilot.msgGivePushBack(new Runway(), new Gate(new Taxiway()));	}	public void act_give_taxi_takeoff_path(Strip temp_strip) {		temp_strip.flight_state = Strip.FlightState.NO_ACTION;		// TODO actually get good runways and gate				LocalControllerAgent lc;		// lc = World.get_local_controller(tower);		lc = (LocalControllerAgent)tower.localController;		List<Taxiway> taxiways = do_get_taxi_takeoff_path(temp_strip);		if (!(taxiways.get(taxiways.size() - 1) instanceof Runway))			taxiways.add(0, temp_strip.runway);		taxiways.add(temp_strip.runway);		boolean runwayisoccupied = true;				if((strips.size() == 2) && (strips.get(0).callNum == strips.get(1).callNum)) {			runwayisoccupied = false;		} else {		    while(runwayisoccupied) {		    	System.out.println("Strip size: " + strips.size());		 		for(Strip strip : strips) {					// System.out.println("Strip 1: " + strip.callNum + " Strip 2: " + tempstrip.callNum);					//System.out.println("Plane pos " + strip.callNum + " " + World.get_plane(strip.callNum).objectState.position.y + 						//	"Runway pos: " + tempstrip.runway.objectState.position.y);					print(strip.callNum + temp_strip.callNum);					if((strip.callNum != temp_strip.callNum)) {						// These pilots have the same runway						System.out.println("Plane pos: " + World.get_plane(strip.callNum).objectState.position.x + 								"Runway pos: " + temp_strip.runway.objectState.position.y);						if((World.get_plane(strip.callNum).objectState.position.y >= temp_strip.runway.objectState.position.y-20) && 								World.get_plane(strip.callNum).objectState.position.x < 365) {							// print("Sending pilot msgNotClearedToLand");							//World.get_plane(temp_strip.callNum).msgPilot().msgWaitForTaxiway();							runwayisoccupied = true;							break;						} else if(World.get_plane(strip.callNum).objectState.position.y == temp_strip.runway.objectState.position.y) {							// System.out.println("Other plane x: " + World.get_plane(strip.callNum).objectState.position.x);							// print("Sending pilot msgNotClearedToLand");							// World.get_plane(temp_strip.callNum).msgPilot().msgWaitForTaxiway();							runwayisoccupied = true;							if(World.get_plane(strip.callNum).objectState.position.x == 365) {								runwayisoccupied = false;							}							break;						} else {							runwayisoccupied = false;						}					}				}		    }		}		if(!runwayisoccupied) {			World.get_plane(temp_strip.callNum).msgPilot().msgTaxiTakeoffPath(taxiways, lc);			print("Sending local controller msgNewDepartureStrip");			lc.msgNewDepartureStrip(temp_strip);		}	}				public List<Taxiway> do_get_taxi_landing_path(Strip strip) {		Map<String, Node<Taxiway, WorldVector>> nodes = get_nodes();				set_gate_taxiway(strip.gate);				WorldVector plane_pos = World.get_plane(strip.callNum).get_world_object_state().get_position();		Node<Taxiway, WorldVector> root = 			nodes.get(key_string(get_closest_connection(plane_pos, strip.runway)));				Node<Taxiway, WorldVector> target = 			nodes.get(key_string(get_closest_connection(strip.gate.get_world_object_state().get_position(), strip.gate.getTaxiway())));				return new_dijkstra_path(root, target, nodes.values());	}		public List<Taxiway> do_get_taxi_takeoff_path(Strip strip) {		Map<String, Node<Taxiway, WorldVector>> nodes = get_nodes();				set_gate_taxiway(strip.gate);				WorldVector plane_pos = World.get_plane(strip.callNum).get_world_object_state().get_position();		Node<Taxiway, WorldVector> target = 			nodes.get(key_string(get_closest_connection(plane_pos, strip.runway)));				Node<Taxiway, WorldVector> root = 			nodes.get(key_string(get_closest_connection(strip.gate.get_world_object_state().get_position(), strip.gate.getTaxiway())));				return new_dijkstra_path(root, target, nodes.values());	}		public List<Taxiway> new_dijkstra_path(Node<Taxiway, WorldVector> root,			Node<Taxiway, WorldVector> target, Collection<Node<Taxiway, WorldVector>> nodes) {				if ((root == null)||(target == null)||(nodes == null))			return null;				DijkstraAlgorithm<Taxiway, WorldVector> dijkstra = 			new DijkstraAlgorithm<Taxiway, WorldVector>(root, target, nodes);		dijkstra.calculate_distance();		return dijkstra.get_edge_path();			}		public WorldVector get_closest_connection(WorldVector wv, WorldObject object) {		if ((wv == null)||(object == null))			return null;				WorldVector closest = null;;		Double closest_dist = Double.POSITIVE_INFINITY;		Double dist;				WorldObjectState wos = object.get_world_object_state();		WorldVector real_conn;				for (WorldVector connection : wos.getConnections()) {			real_conn = wos.position.plus(connection);			dist = get_distance(real_conn, wv);			if (dist < closest_dist) {				closest = real_conn;				closest_dist = dist;			}		}				return closest;	}		public String key_string(WorldVector wv) {		if (wv == null) 			return null;				return "" + wv.x + ":" + wv.y;	}		public Map<String, Node<Taxiway, WorldVector>> get_nodes() {		Map<String, Node<Taxiway, WorldVector>> nodes = new HashMap<String, Node<Taxiway, WorldVector>>();				WorldObjectState wos;		WorldVector real_conn;		WorldVector real_conn2;					// First make list of nodes		for (WorldObject object : World.world_objects) {			if ((object instanceof Runway)||(object instanceof Taxiway)) {				wos = object.get_world_object_state();				for (WorldVector connection : wos.getConnections()) {					real_conn = wos.get_position().plus(connection);					if (nodes.get(key_string(real_conn)) == null) {						nodes.put(key_string(real_conn), new Node<Taxiway, WorldVector>(real_conn));					}				}			}		}				//System.out.println(nodes.keySet());				// Now attach nodes to other nodes with edges		Node<Taxiway, WorldVector> node1;		Node<Taxiway, WorldVector> node2;		for (WorldObject object : World.world_objects) {			if (object instanceof Taxiway) {			//System.out.println(object.getName() + " has " + object.get_world_object_state().getConnections().size() + " connections");			//if ((object instanceof Runway)||(object instanceof Taxiway)) {				wos = object.get_world_object_state();								for (WorldVector connection1 : wos.getConnections()) {					real_conn = wos.get_position().plus(connection1);					node1 = nodes.get(key_string(real_conn));					for (WorldVector connection2 : object.get_world_object_state().getConnections()) {						real_conn2 = wos.get_position().plus(connection2);						if (real_conn.equals(real_conn2))							continue;						//System.out.println(real_conn + " " + real_conn2);						node2 = nodes.get(key_string(real_conn2)); // get node associated with connection point						node1.neighbors.put(node2, 								new Edge<Taxiway>((Taxiway)object, 										get_distance(real_conn, real_conn2))); // add it as a neighbor to node1					}				}			}		}				return nodes;	}			/*	public boolean contains_node(List<Node<WorldObject, WorldVector>> nodes, WorldVector connection) {		for (Node<WorldObject, WorldVector> node : nodes) {			if ((node.position.x == connection.x)&&					(node.position.y == connection.y)) {				return true;			}		}		return false;	}			public Node<WorldObject, WorldVector> get_node(List<Node<WorldObject, WorldVector>> nodes, WorldVector connection) {		for (Node<WorldObject, WorldVector> node : nodes) {			if ((node.position.x == connection.x)&&					(node.position.y == connection.y)) {				return node;			}		}		return null;	}*/		public void set_gate_taxiway(Gate gate) {		WorldObjectState wos;		WorldVector real_conn;		WorldVector gate_conn = gate.get_world_object_state().get_position().plus(gate.get_world_object_state().getConnections().get(0));		for (WorldObject object : World.world_objects) {			if (!(object instanceof Taxiway))				continue;			wos = object.get_world_object_state();			for (WorldVector connection : wos.getConnections()) {				real_conn = wos.get_position().plus(connection);				if (real_conn.equals(gate_conn)) {					gate.setTaxiway((Taxiway)object);				}			}		}	}		public Double get_distance(WorldVector wv1, WorldVector wv2) {		return Math.sqrt(Math.pow(wv1.x - wv2.x, 2) + Math.pow(wv1.y - wv2.y, 2));	}		public Strip get_strip(String cn) {		for (Strip strip : strips)			if (strip.callNum.equals(cn))				return strip;		return null;	}		public Strip get_strip(Strip.FlightState state) {		for (Strip strip : strips)			if (strip.flight_state == state)				return strip;		return null;	}}// need delete strip at appropriate time

⌨️ 快捷键说明

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