From: Stefan F. <ste...@us...> - 2010-05-19 20:14:22
|
Update of /cvsroot/rails/18xx/rails/algorithms In directory sfp-cvsdas-4.v30.ch3.sourceforge.com:/tmp/cvs-serv12072/rails/algorithms Modified Files: RevenueAdapter.java RevenueTrainRun.java NetworkVertex.java Log Message: Added Off-Board bonus for 18EU and some minor fixes Index: NetworkVertex.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/algorithms/NetworkVertex.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** NetworkVertex.java 18 May 2010 21:36:12 -0000 1.11 --- NetworkVertex.java 19 May 2010 20:14:13 -0000 1.12 *************** *** 2,5 **** --- 2,6 ---- import java.awt.geom.Point2D; + import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; *************** *** 109,112 **** --- 110,124 ---- } + public String getIdentifier(){ + if (virtual) + return virtualId; + else if (isStation()) + return hex.getName() + "." + -station.getNumber(); + else if (isSide()) + return hex.getName() + "." + side; + else + return null; + } + public boolean isVirtual() { return virtual; *************** *** 125,128 **** --- 137,144 ---- } + public VertexType getType() { + return type; + } + public boolean isMajor(){ *************** *** 203,207 **** return side; } ! /** --- 219,223 ---- return side; } ! /** *************** *** 226,230 **** sink = false; } else if (station.getType().equals(Station.OFF_MAP_AREA) || ! station.getType().equals(Station.CITY) && !city.hasTokenSlotsLeft() && !city.hasTokenOf(company)) { sink = true; } --- 242,246 ---- sink = false; } else if (station.getType().equals(Station.OFF_MAP_AREA) || ! station.getType().equals(Station.CITY) && !city.hasTokenSlotsLeft() && city.getSlots() != 0 && !city.hasTokenOf(company)) { sink = true; } *************** *** 243,247 **** // define value ! if (station.getType().equals(Station.OFF_MAP_AREA)) { value = hex.getCurrentOffBoardValue(phase); } else { --- 259,263 ---- // define value ! if (station.getType().equals(Station.OFF_MAP_AREA) || station.getValue() == -1) { value = hex.getCurrentOffBoardValue(phase); } else { *************** *** 250,261 **** } - public String getIdentifier(){ - if (isStation()) - return hex.getName() + "." + -station.getNumber(); - else if (isSide()) - return hex.getName() + "." + side; - else - return "HQ"; - } @Override --- 266,269 ---- *************** *** 300,303 **** --- 308,345 ---- /** + * creates a new virtual vertex with identical properties and links + */ + public static NetworkVertex duplicateVertex(SimpleGraph<NetworkVertex, NetworkEdge> graph, + NetworkVertex vertex, String newIdentifier, boolean addOldVertexAsHidden) { + // create new vertex + NetworkVertex newVertex = NetworkVertex.getVirtualVertex(vertex.type, newIdentifier); + // copy values + newVertex.major = vertex.major; + newVertex.minor = vertex.minor; + newVertex.value = vertex.value; + newVertex.sink = vertex.sink; + newVertex.cityName = vertex.cityName; + graph.addVertex(newVertex); + // copy edges + Set<NetworkEdge> edges = graph.edgesOf(vertex); + for (NetworkEdge edge:edges) { + List<NetworkVertex> hiddenVertices; + if (edge.getSource() == vertex) { + hiddenVertices = edge.getHiddenVertexes(); + if (addOldVertexAsHidden) hiddenVertices.add(vertex); + NetworkEdge newEdge = new NetworkEdge(newVertex, edge.getTarget(), edge.isGreedy(), edge.getDistance(), hiddenVertices); + graph.addEdge(newVertex, edge.getTarget(), newEdge); + } else { + hiddenVertices = new ArrayList<NetworkVertex>(); + if (addOldVertexAsHidden) hiddenVertices.add(vertex); + hiddenVertices.addAll(edge.getHiddenVertexes()); + NetworkEdge newEdge = new NetworkEdge(edge.getSource(), newVertex, edge.isGreedy(), edge.getDistance(), edge.getHiddenVertexes()); + graph.addEdge(newEdge.getSource(), newVertex, newEdge); + } + } + return newVertex; + } + + /** * replaces one vertex by another for a network graph * copies all edges *************** *** 324,329 **** * Returns all vertices in a specified collection of hexes */ ! public static Set<NetworkVertex> getVerticesByHex(Collection<NetworkVertex> vertices, Collection<MapHex> hexes) { ! log.info("hexes = " + hexes); Set<NetworkVertex> hexVertices = new HashSet<NetworkVertex>(); for (NetworkVertex vertex:vertices) { --- 366,370 ---- * Returns all vertices in a specified collection of hexes */ ! public static Set<NetworkVertex> getVerticesByHexes(Collection<NetworkVertex> vertices, Collection<MapHex> hexes) { Set<NetworkVertex> hexVertices = new HashSet<NetworkVertex>(); for (NetworkVertex vertex:vertices) { *************** *** 335,340 **** --- 376,391 ---- } + public static NetworkVertex getVertexByIdentifier(Collection<NetworkVertex> vertices, String identifier) { + for (NetworkVertex vertex:vertices) { + if (vertex.getIdentifier().equals(identifier)) { + return vertex; + } + } + return null; + } public static Point2D getVertexPoint2D(HexMap map, NetworkVertex vertex) { + if (vertex.isVirtual()) return null; + GUIHex guiHex = map.getHexByName(vertex.getHex().getName()); if (vertex.isMajor()) { Index: RevenueTrainRun.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/algorithms/RevenueTrainRun.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** RevenueTrainRun.java 18 May 2010 04:12:22 -0000 1.2 --- RevenueTrainRun.java 19 May 2010 20:14:13 -0000 1.3 *************** *** 58,61 **** --- 58,69 ---- } + private String prettyPrintHexName(NetworkVertex vertex) { + if (vertex.isVirtual()) { + return vertex.getIdentifier(); + } else { + return vertex.getHex().getName(); + } + } + private int prettyPrintNewLine(StringBuffer runPrettyPrint, int multiple, int initLength) { if (runPrettyPrint.length() / PRETTY_PRINT_LENGTH != multiple) { *************** *** 79,88 **** currentHex = vertex.getHex(); startVertex = vertex; ! runPrettyPrint.append(vertex.getHex().getName() + "("); } else if (startVertex == vertex) { currentHex = vertex.getHex(); runPrettyPrint.append(") / "); multiple = prettyPrintNewLine(runPrettyPrint, multiple, initLength); ! runPrettyPrint.append(vertex.getHex().getName() + "(0"); continue; } else if (vertex.getHex() != currentHex) { --- 87,96 ---- currentHex = vertex.getHex(); startVertex = vertex; ! runPrettyPrint.append(prettyPrintHexName(vertex) + "("); } else if (startVertex == vertex) { currentHex = vertex.getHex(); runPrettyPrint.append(") / "); multiple = prettyPrintNewLine(runPrettyPrint, multiple, initLength); ! runPrettyPrint.append(prettyPrintHexName(vertex) + "(0"); continue; } else if (vertex.getHex() != currentHex) { *************** *** 90,94 **** runPrettyPrint.append("), "); multiple = prettyPrintNewLine(runPrettyPrint, multiple, initLength); ! runPrettyPrint.append(vertex.getHex().getName() + "("); } else { runPrettyPrint.append(","); --- 98,102 ---- runPrettyPrint.append("), "); multiple = prettyPrintNewLine(runPrettyPrint, multiple, initLength); ! runPrettyPrint.append(prettyPrintHexName(vertex) + "("); } else { runPrettyPrint.append(","); Index: RevenueAdapter.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/algorithms/RevenueAdapter.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** RevenueAdapter.java 18 May 2010 21:36:12 -0000 1.15 --- RevenueAdapter.java 19 May 2010 20:14:13 -0000 1.16 *************** *** 34,39 **** // define VertexVisitSet public class VertexVisit { ! Set<NetworkVertex> set; ! VertexVisit() {set = new HashSet<NetworkVertex>();} public String toString() { return "VertexVisit Set:" + set; --- 34,39 ---- // define VertexVisitSet public class VertexVisit { ! public Set<NetworkVertex> set; ! public VertexVisit() {set = new HashSet<NetworkVertex>();} public String toString() { return "VertexVisit Set:" + set; |