Adding Segments



>> Friday, July 16, 2010

I made a small update to my previous solution to the problem-solving agent. The developer in me hated the duplicate "code" of when I defined the nodes. For example, when I defined that Arad was 75 units from Zerind, I also had to define that Zerind was 75 units from Arad. For this example that was not huge, but it could lead to errors.

Here is what it was:

nodes = [
createNode("Arad", ["Zerind":75, "Sibiu":140, "Timisoara":118]),
...
createNode("Zerind", ["Oradea":71, "Arad":75])
];

To make it a little less error-prone, I added the idea of a "segment." Now I just have to define each segment one time instead of twice. It looks like this:

segments = [
new Segment(start:"Arad", end:"Zerind", cost:75),
new Segment(start:"Arad", end:"Sibiu", cost:140),
new Segment(start:"Arad", end:"Timisoara", cost:118),
new Segment(start:"Bucharest", end:"Fagaras", cost:211),
new Segment(start:"Bucharest", end:"Giurgiu", cost:90),
new Segment(start:"Bucharest", end:"Pitesti", cost:101),
new Segment(start:"Bucharest", end:"Urziemi", cost:85),
new Segment(start:"Craiova", end:"Drobeta", cost:120),
new Segment(start:"Craiova", end:"Pitesti", cost:138),
new Segment(start:"Craiova", end:"Rimnieu Vilcea", cost:146),
new Segment(start:"Drobeta", end:"Mehadia", cost:75),
new Segment(start:"Eforie", end:"Hirsova", cost:86),
new Segment(start:"Fagaras", end:"Sibiu", cost:99),
new Segment(start:"Hirsova", end:"Urziemi", cost:98),
new Segment(start:"Iasi", end:"Neamt", cost:87),
new Segment(start:"Iasi", end:"Vaslui", cost:92),
new Segment(start:"Lugoj", end:"Mehadia", cost:70),
new Segment(start:"Lugoj", end:"Timisoara", cost:111),
new Segment(start:"Oradea", end:"Sibiu", cost:151),
new Segment(start:"Oradea", end:"Zerind", cost:71),
new Segment(start:"Pitesti", end:"Rimnieu Vilcea", cost:97),
new Segment(start:"Rimnieu Vilcea", end:"Sibiu", cost:80),
new Segment(start:"Urziemi", end:"Vaslui", cost:142),
];
Then in the Segment class I created a method to convert a list of Segments into a list of Nodes so I just needed to do this in my groovy script to have the same information I did before:
nodes = Segment.convertSegmentsToNodes(segments);

I also had to move the createActionList and createNode methods into the Segment class, which helped clean up my groovy script.

0 comments:

Post a Comment

  © Blogger template Webnolia by Ourblogtemplates.com 2009

Back to TOP