Dear Sir,
Following is the code to find shortest path using ACO.
I have tried a lot But I don't know why i am getting value of Co2 to 00 for all the edges. Please help to resolve this issue.
import random
graph = sumolib.net.readNet('Dijkstra1.net.xml')
class ACOAlgorithm():
'''
Ant colony optimization algorithms to find shortest path
'''
def __init__(self):
def set_graph(self, Objgraph):
#Initialize pheromone values to the length of edge
for edge in graph.getEdges():
Co2_Value = get_pollution_level_Of_Edge(edge.getID())
def process(self,start_node,end_node):
while True:
self._start_travel(start_node)
self._find_edge(start_node,end_node)
if self._finish_travel(end_node):
break
def _start_travel(self,start_node):
def _find_edge(self,start_node,end_node):
def _finish_travel(self,end_node):
return self.remain_path < self.size
def _have_ant_completed(self,end_node):
def get_pollution_level_Of_Edge(EdgeId):
#Get Edge Carbon Emission
Value_co2 = traci.edge.getCO2Emission(EdgeId)
print 'Co2 Value of '+ str(EdgeId) + ' Is : '+ str(Value_co2)
return Value_co2
def generate_routefile():
with open("dijkstra_000.rou.xml", "w") as routes:
print >> routes, """<routes>
<vType id="vehicle1" accel="0.8" decel="4.5" sigma="0.5" length="5" minGap="2.5" maxSpeed="16.67" guiShape="passenger"/>
<route id="1" edges="1 3 5 9" /><flow id="myflow" begin="0" end="3600" number="1000" from="1" to="18"/> </routes>"""
def main():
start_node = 9
end_node = 2
traci.init(PORT)
Objgraph_mat = Graph_mat()
aco = ACOAlgorithm()
for edge in graph.getEdges():
FromNode = str(int(str(edge.getFromNode().getID())) - 1)
ToNode = str(int(str(edge.getToNode().getID())) - 1)
Objgraph_mat.add_vertex(Vertex(FromNode))
Objgraph_mat.add_vertex(Vertex(ToNode))
for edge in graph.getEdges():
Objgraph_mat.add_edge(int(str(edge.getFromNode().getID())) - 1,int(str(edge.getToNode().getID()))-1,edge.getLength())
#Objgraph_mat.print_graph()
aco.set_graph(Objgraph_mat)
aco.process(start_node,end_node)
#create the new route for vehicle
traci.route.add("0", edges)
#assign the new route for vehicle with id vehicle1
traci.vehicle.add("vehicle0","0")
for i in range(3000): # or whatever nulmber of steps you want to simulate
traci.simulationStep()
traci.close()
sys.stdout.flush()
def get_options():
optParser = optparse.OptionParser()
optParser.add_option("--nogui", action="" default=False, help="run the commandline version of sumo")
options, args = optParser.parse_args()
return options
def get_EdgeId_From_vertex(prenode,nextnode):
# this is the main entry point of this script
if __name__ == "__main__":
options = get_options()
# this script has been called from the command line. It will start sumo as a
# server, then connect and run
if options.nogui:
sumoBinary = checkBinary('sumo')
else:
sumoBinary = checkBinary('sumo-gui')
generate_routefile()
# this is the normal way of using traci. sumo is started as a
# subprocess and then the python script connects and runs
sumoProcess = subprocess.Popen([sumoBinary, "-c", "dijkstra.sumo.cfg", "--tripinfo-output", "tripinfo.xml", "--remote-port",str(PORT),"--device.emissions.probability","1","--emission-output","emmission_aco.xml"], stdout=sys.stdout, stderr=sys.stderr)
main()
sumoProcess.wait()
--
_______________________________________________