GithubHelp home page GithubHelp logo

hasnainroopawalla / ant-colony-optimization Goto Github PK

View Code? Open in Web Editor NEW
14.0 1.0 2.0 44 KB

A Python package to find the shortest path in a graph using Ant Colony Optimization

Home Page: https://medium.com/@hasnain.roopawalla/ant-colony-optimization-1bbc346c2da5

License: MIT License

Python 100.00%
ant-colony-optimization python shortest-path-algorithm graph antnet optimization routing routing-algorithm

ant-colony-optimization's Introduction

Ant Colony Optimization

Develop Deploy PyPi version Downloads

A Python package to find the shortest path in a graph using Ant Colony Optimization (ACO).

โžก๏ธ Check out my Medium article for a detailed walkthrough ๐Ÿš€

The Ant colony Optimization algorithm is a probabilistic technique for solving computational problems which can be reduced to finding good paths through graphs (source).

This implementation of the ACO algorithm uses the NetworkX graph environment.

๐Ÿ Getting Started

To install the package directly from PyPi:

$ pip install aco_routing

๐ŸŽˆ Usage

Check out: example.py

Import all the dependencies:

from aco_routing import ACO
import networkx as nx

Create a NetworkX.Graph object with nodes and edges:

G = nx.DiGraph()

G.add_edge("A", "B", cost=2)
G.add_edge("B", "C", cost=2)
G.add_edge("A", "H", cost=2)
G.add_edge("H", "G", cost=2)
G.add_edge("C", "F", cost=1)
G.add_edge("F", "G", cost=1)
G.add_edge("G", "F", cost=1)
G.add_edge("F", "C", cost=1)
G.add_edge("C", "D", cost=10)
G.add_edge("E", "D", cost=2)
G.add_edge("G", "E", cost=2)

Use ACO to find the shortest path and cost between the source and destination:

aco = ACO(G, ant_max_steps=100, num_iterations=100, ant_random_spawn=True)

aco_path, aco_cost = aco.find_shortest_path(
    source="A",
    destination="D",
    num_ants=100,
)

Output:

ACO path: A -> H -> G -> E -> D
ACO path cost: 8.0

๐Ÿ“ฆ Contents

Ant

aco_routing.Ant

  • An Ant that traverses the graph.

ACO

aco_routing.ACO

  • The traditional Ant Colony Optimization algorithm that spawns ants at various nodes in the graph and finds the shortest path between the specified source and destination (pseudo code).

Contributing

  • Post any issues and suggestions on the GitHub issues page.
  • To contribute, fork the project and then create a pull request back to master.

ant-colony-optimization's People

Contributors

hasnainroopawalla avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

ant-colony-optimization's Issues

line 136, in get_node_edges return self.graph[id].edges KeyError: ' '

It might be a problem with my data set but I don't know why Dijkstra works fine but the ant colony it has a problem
it gives me this error above
It does not really specify anything that is wrong except the keyError : ""

Any way you can help would be appreciate it thank you for tour time

Unable to find optimal solution for Undirected Graph

Thanks for solving the previous issue.

I'm trying to replicate the paper "Application of Ant Colony Algorithm in Finding
Shortest Paths in Mobile Games".

I notice that this library is specifically meant to deal with directed graphs, so perhaps I am trying to use the wrong tool.

from aco_routing import ACO
import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
# North of map
G.add_edge("HQ", "A", cost=1)
G.add_edge("A", "B", cost=1)
G.add_edge("B", "?North", cost=1)
G.add_edge("?North", "G", cost=1)
G.add_edge("G", "OGAS", cost=1)
# South of map
G.add_edge("HQ", "C", cost=1)
G.add_edge("C", "D", cost=1)
G.add_edge("D", "Helipad", cost=1)
G.add_edge("Helipad", "OGAS", cost=1)
G.add_edge("C", "E", cost=1)
G.add_edge("E", "?South", cost=1)
G.add_edge("?South", "Helipad", cost=1)

source = "OGAS"
destination = "HQ"

aco = ACO(G, ant_max_steps=100, num_iterations=100, ant_random_spawn=True)
# aco.evaporation_rate = 0
# aco.beta = 0

dijkstra_path = nx.dijkstra_path(G, source, destination)
dijkstra_cost = nx.path_weight(G, dijkstra_path, "cost")

aco_path, aco_cost = aco.find_shortest_path(
    source=source,
    destination=destination,
    num_ants=100,
)

print(f"ACO - path: {aco_path}, cost: {aco_cost}")
print(f"Dijkstra - path: {dijkstra_path}, cost: {dijkstra_cost}")
# nx.draw(G, with_labels=True, font_weight='bold')
# plt.show()

In this code, aco always returns the unoptimal solution. Is it due to the fact that the code is meant for directed graphs?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.