From: Stefan F. <ste...@us...> - 2010-05-18 04:12:30
|
Update of /cvsroot/rails/18xx/rails/algorithms In directory sfp-cvsdas-4.v30.ch3.sourceforge.com:/tmp/cvs-serv19864/rails/algorithms Modified Files: RevenueAdapter.java NetworkEdge.java NetworkVertex.java RevenueBonusTemplate.java RevenueCalculator.java RevenueTrainRun.java Added Files: RevenueManager.java RevenueStaticModifier.java Log Message: Various updates to revenue calculation, especially RevenueStaticModifier and support for 18Kaas Index: RevenueCalculator.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/algorithms/RevenueCalculator.java,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** RevenueCalculator.java 14 May 2010 15:19:57 -0000 1.13 --- RevenueCalculator.java 18 May 2010 04:12:22 -0000 1.14 *************** *** 541,545 **** for (int j=0; j < vertexNbVisitSets[vertexId]; j++) { trainVisited[trainId][vertexVisitSets[vertexId][j]] = arrive; ! log.info("RC: visited = " + arrive + " for vertex " + vertexVisitSets[vertexId][j] + " due to block rule"); } --- 541,545 ---- for (int j=0; j < vertexNbVisitSets[vertexId]; j++) { trainVisited[trainId][vertexVisitSets[vertexId][j]] = arrive; ! log.debug("RC: visited = " + arrive + " for vertex " + vertexVisitSets[vertexId][j] + " due to block rule"); } --- NEW FILE: RevenueManager.java --- package rails.algorithms; import java.util.HashSet; import java.util.List; import java.util.Set; import org.apache.log4j.Logger; import rails.game.ConfigurableComponentI; import rails.game.ConfigurationException; import rails.game.GameManagerI; import rails.util.LocalText; import rails.util.Tag; /** * Coordinates and stores all elements related to revenue calulcation, * which are permanent. * The conversion of Rails elements is in the responsibility of the RevenueAdapter. * For each GameManager instance only one RevenueManager is created. * * @author freystef * */ public final class RevenueManager implements ConfigurableComponentI { protected static Logger log = Logger.getLogger(RevenueManager.class.getPackage().getName()); private Set<RevenueStaticModifier> staticModifiers; public RevenueManager() { staticModifiers = new HashSet<RevenueStaticModifier>(); } public void configureFromXML(Tag tag) throws ConfigurationException { // define static modifiers List<Tag> modifierTags = tag.getChildren("StaticModifier"); for (Tag modifierTag:modifierTags) { // get classname String className = modifierTag.getAttributeAsString("class"); if (className == null) { throw new ConfigurationException(LocalText.getText( "ComponentHasNoClass", "StaticModifier")); } // create modifier RevenueStaticModifier modifier; try { modifier = (RevenueStaticModifier) Class.forName(className).newInstance(); } catch (Exception e) { throw new ConfigurationException(LocalText.getText( "ClassCannotBeInstantiated", className), e); } // add them to the revenueManager staticModifiers.add(modifier); log.info("Added modifier " + className); } } public void finishConfiguration(GameManagerI parent) throws ConfigurationException { for (RevenueStaticModifier modifier:staticModifiers) { if (modifier instanceof ConfigurableComponentI) { ((ConfigurableComponentI)modifier).finishConfiguration(parent); } } } void callStaticModifiers(RevenueAdapter revenueAdapter) { for (RevenueStaticModifier modifier:staticModifiers) { modifier.modifyCalculator(revenueAdapter); } } } --- NEW FILE: RevenueStaticModifier.java --- package rails.algorithms; /** * Classes that change properties of the revenue calculation * before the actual calculation starts implement a the static modifier. * * They have to register themselves to the RevenueManager via the GameManager instance. * @author freystef * */ public interface RevenueStaticModifier{ public void modifyCalculator(RevenueAdapter revenueAdapter); } Index: NetworkVertex.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/algorithms/NetworkVertex.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** NetworkVertex.java 11 May 2010 21:47:21 -0000 1.9 --- NetworkVertex.java 18 May 2010 04:12:22 -0000 1.10 *************** *** 4,9 **** --- 4,12 ---- import java.util.Collection; import java.util.Comparator; + import java.util.HashSet; + import java.util.Set; import org.apache.log4j.Logger; + import org.jgrapht.graph.SimpleGraph; import rails.game.City; *************** *** 21,25 **** Logger.getLogger(NetworkVertex.class.getPackage().getName()); ! private static enum VertexType { STATION, SIDE, --- 24,28 ---- Logger.getLogger(NetworkVertex.class.getPackage().getName()); ! public static enum VertexType { STATION, SIDE, *************** *** 258,263 **** public String toString(){ StringBuffer message = new StringBuffer(); ! if (isStation()) ! message.append( hex.getName() + "." + station.getNumber()); else if (isSide()) message.append(hex.getName() + "." + hex.getOrientationName(side)); --- 261,268 ---- public String toString(){ StringBuffer message = new StringBuffer(); ! if (isVirtual()) ! message.append(virtualId); ! else if (isStation()) ! message.append(hex.getName() + "." + station.getNumber()); else if (isSide()) message.append(hex.getName() + "." + hex.getOrientationName(side)); *************** *** 293,296 **** --- 298,325 ---- } + + /** + * replaces one vertex by another for a network graph + * copies all edges + */ + public static boolean replaceVertex(SimpleGraph<NetworkVertex, NetworkEdge> graph, + NetworkVertex oldVertex, NetworkVertex newVertex) { + // add new vertex + graph.addVertex(newVertex); + // replace old edges + Set<NetworkEdge> oldEdges = graph.edgesOf(oldVertex); + for (NetworkEdge oldEdge:oldEdges) { + NetworkEdge newEdge = NetworkEdge.replaceVertex(oldEdge, oldVertex, newVertex); + if (newEdge.getSource() == newVertex) { + graph.addEdge(newVertex, newEdge.getTarget(), newEdge); + } else { + graph.addEdge(newEdge.getSource(), newVertex, newEdge); + } + } + // remove old vertex + return graph.removeVertex(oldVertex); + } + + public static Point2D getVertexPoint2D(HexMap map, NetworkVertex vertex) { GUIHex guiHex = map.getHexByName(vertex.getHex().getName()); Index: RevenueAdapter.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/algorithms/RevenueAdapter.java,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** RevenueAdapter.java 14 May 2010 15:19:57 -0000 1.13 --- RevenueAdapter.java 18 May 2010 04:12:22 -0000 1.14 *************** *** 83,90 **** } public Set<NetworkVertex> getVertices() { return graph.vertexSet(); } ! public Set<NetworkEdge> getEdges() { return graph.edgeSet(); --- 83,94 ---- } + public SimpleGraph<NetworkVertex,NetworkEdge> getGraph() { + return graph; + } + public Set<NetworkVertex> getVertices() { return graph.vertexSet(); } ! public Set<NetworkEdge> getEdges() { return graph.edgeSet(); *************** *** 163,167 **** // define graph, without HQ graph = graphBuilder.getRailRoadGraph(company, false); ! // define startVertexes startVertices.addAll(graphBuilder.getCompanyBaseTokenVertexes(company)); --- 167,174 ---- // define graph, without HQ graph = graphBuilder.getRailRoadGraph(company, false); ! ! // initialize vertices ! NetworkVertex.initAllRailsVertices(graph.vertexSet(), company, phase); ! // define startVertexes startVertices.addAll(graphBuilder.getCompanyBaseTokenVertexes(company)); *************** *** 179,182 **** --- 186,192 ---- } + // add all static modifiers + gameManager.getRevenueManager().callStaticModifiers(this); + } private void defineVertexVisitSets() { *************** *** 230,233 **** --- 240,244 ---- public void initRevenueCalculator(){ + // optimize graph (optimizeGraph clones the graph) rcGraph = NetworkGraphBuilder.optimizeGraph(graph, protectedVertices); *************** *** 235,239 **** // define the vertices and edges lists rcVertices = new ArrayList<NetworkVertex>(rcGraph.vertexSet()); - NetworkVertex.initAllRailsVertices(rcVertices, company, phase); // define ordering on vertexes by value Collections.sort(rcVertices, new NetworkVertex.ValueOrder()); --- 246,249 ---- Index: NetworkEdge.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/algorithms/NetworkEdge.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** NetworkEdge.java 11 May 2010 21:47:21 -0000 1.7 --- NetworkEdge.java 18 May 2010 04:12:22 -0000 1.8 *************** *** 163,166 **** --- 163,183 ---- } + /** + * for a given edge it replaces one of the vertices by a different one + * otherwise copies all edge attributes + * @return copied edge with replaced vertex, null if oldVertex is neither source, nor target + */ + public static NetworkEdge replaceVertex(NetworkEdge edge, NetworkVertex oldVertex, NetworkVertex newVertex) { + NetworkEdge newEdge; + if (edge.source == oldVertex) { + newEdge= new NetworkEdge(newVertex, edge.target, edge.greedy, edge.distance, edge.hiddenVertexes); + } else if (edge.target == oldVertex) { + newEdge= new NetworkEdge(edge.source, newVertex, edge.greedy, edge.distance, edge.hiddenVertexes); + } else { + newEdge = null; + } + return newEdge; + } + public static Shape getEdgeShape(HexMap map, NetworkEdge edge){ Point2D source = NetworkVertex.getVertexPoint2D(map, edge.getSource()); *************** *** 176,178 **** --- 193,196 ---- + } Index: RevenueTrainRun.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/algorithms/RevenueTrainRun.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RevenueTrainRun.java 14 May 2010 15:19:57 -0000 1.1 --- RevenueTrainRun.java 18 May 2010 04:12:22 -0000 1.2 *************** *** 19,23 **** public class RevenueTrainRun { ! private static final int PRETTY_PRINT_LENGTH = 80; protected static Logger log = --- 19,23 ---- public class RevenueTrainRun { ! private static final int PRETTY_PRINT_LENGTH = 100; protected static Logger log = *************** *** 49,52 **** --- 49,58 ---- value += revenueAdapter.getVertexValue(vertex, train, revenueAdapter.getPhase()); } + // check revenueBonuses (complex) + for (RevenueBonus bonus:revenueAdapter.getRevenueBonuses()) { + if (bonus.checkComplexBonus(vertices, train.getRailsTrainType(), revenueAdapter.getPhase())) { + value += bonus.getValue(); + } + } return value; } *************** *** 102,109 **** for (RevenueBonus bonus:revenueAdapter.getRevenueBonuses()) { if (bonus.checkComplexBonus(vertices, train.getRailsTrainType(), revenueAdapter.getPhase())) { ! runPrettyPrint.append(", " + bonus.getName() + "(" + bonus.getValue() + ")"); multiple = prettyPrintNewLine(runPrettyPrint, multiple, initLength); } - } --- 108,115 ---- for (RevenueBonus bonus:revenueAdapter.getRevenueBonuses()) { if (bonus.checkComplexBonus(vertices, train.getRailsTrainType(), revenueAdapter.getPhase())) { ! runPrettyPrint.append(" + "); ! runPrettyPrint.append(bonus.getName() + "(" + bonus.getValue() + ")"); multiple = prettyPrintNewLine(runPrettyPrint, multiple, initLength); } } Index: RevenueBonusTemplate.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/algorithms/RevenueBonusTemplate.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** RevenueBonusTemplate.java 14 May 2010 15:19:57 -0000 1.2 --- RevenueBonusTemplate.java 18 May 2010 04:12:22 -0000 1.3 *************** *** 6,9 **** --- 6,10 ---- import org.apache.log4j.Logger; + import rails.game.ConfigurableComponentI; import rails.game.ConfigurationException; import rails.game.GameManagerI; *************** *** 20,24 **** * @author freystef */ ! public class RevenueBonusTemplate { protected static Logger log = --- 21,25 ---- * @author freystef */ ! public final class RevenueBonusTemplate implements ConfigurableComponentI { protected static Logger log = *************** *** 26,33 **** // bonus value ! private final int value; // bonus name ! private final String name; // template condition attributes --- 27,34 ---- // bonus value ! private int value; // bonus name ! private String name; // template condition attributes *************** *** 36,48 **** private final List<String> identPhases; ! public RevenueBonusTemplate(Tag tag) throws ! ConfigurationException { ! ! value = tag.getAttributeAsInteger("value"); ! name = tag.getAttributeAsString("name"); ! identVertices = new ArrayList<Integer>(); identTrainTypes = new ArrayList<String>(); identPhases = new ArrayList<String>(); // check for vertices --- 37,49 ---- private final List<String> identPhases; ! public RevenueBonusTemplate() { identVertices = new ArrayList<Integer>(); identTrainTypes = new ArrayList<String>(); identPhases = new ArrayList<String>(); + } + + public void configureFromXML(Tag tag) throws ConfigurationException { + value = tag.getAttributeAsInteger("value"); + name = tag.getAttributeAsString("name"); // check for vertices *************** *** 78,84 **** } } ! log.info("Created " + this); } public RevenueBonus toRevenueBonus(MapHex hex, GameManagerI gm, NetworkGraphBuilder ngb) { log.info("Convert " + this); --- 79,94 ---- } } ! log.info("Configured " + this); ! } + /** + * is not used, use toRevenueBonus instead + */ + public void finishConfiguration(GameManagerI parent) + throws ConfigurationException { + throw new ConfigurationException("Use toRevenueBonus"); + } + public RevenueBonus toRevenueBonus(MapHex hex, GameManagerI gm, NetworkGraphBuilder ngb) { log.info("Convert " + this); *************** *** 136,138 **** --- 146,149 ---- return s.toString(); } + } |