An optimization package for solving classic problems like TSP using various algorithms
Project description
optimx
optimx is a Python package designed to solve classic optimization problems. Its purpose is to provide efficient, flexible, and extendable solutions for a variety of optimization challenges.
Currently, optimx offers partial support for solving the Traveling Salesman Problem (TSP) through multiple algorithmic approaches, including brute force, dynamic programming, nearest neighbor, and branch-and-bound. This package aims to serve as a robust foundation for tackling optimization problems, with plans for additional algorithms and broader optimization support in future releases.
Installation
You can install optimx via pip:
pip install optimx
Tutorial
Here is a quick example to get started with optimx and see how it can be used to solve the Traveling Salesman Problem (TSP) with different algorithms.
import optimx as ox
# Define a distance matrix representing the distances between nodes
distance_matrix = [
[0, 10, 15, 20],
[10, 0, 35, 25],
[15, 35, 0, 30],
[20, 25, 30, 0]
]
# Set algorithm options and parameters
algorithm = "branch_and_bound" # Options: "nearest_neighbor", "branch_and_bound", "genetic_algorithm"
node_names = None # Optionally, specify node names, e.g., ["A", "B", "C", "D"]
start_node = None # Optionally, specify a start node, e.g., "A"
cycle = False # Set to True if the route should return to the start node
node_coordinates = None # Optionally, specify coordinates, e.g., [(0, 0), (0, 1), (1, 0), (1, 1)]
# Solve the TSP using the specified algorithm and options
best_route = ox.solve_tsp(
distance_matrix=distance_matrix,
algorithm=algorithm,
node_names=node_names,
start_node=start_node,
cycle=cycle
)
print("Best route:", best_route)
Best route: [0, 1, 3, 2]
Total distance: 65
total_distance = ox.calculate_tsp_distance_by_route(distance_matrix=distance_matrix, route=best_route, node_names=node_names)
print("Total distance:", total_distance)
Total distance: 65
ox.plot_tsp_route(best_route, node_names, node_coordinates, start_node, cycle)
The solve_knapsack function can be used to solve the Knapsack problem.
best_combination, max_val = ox.solve_knapsack(weights, values, capacity, "dynamic_programming")
print(f"Best combination: {best_combination}, max value: {max_val}")
The solve_ant_colony function can be used to solve the Any Colony Optimization problem.
from optimx.utils import generate_square_distances
distances = generate_square_distances(10)
n_ants = 10
n_best = 2
n_iteration = 100
decay = 0.6
shortest_paths = ox.solve_ant_colony(distances, n_ants, n_best, n_iteration, decay)
print("Shortest paths:", shortest_paths)
License
OptimX is licensed under the MIT License. See LICENSE for more details.
Contributing
We welcome contributions to OptimX! Please see CONTRIBUTING.md for guidelines.
Contact
For any questions or feedback, please contact the author:
- Okan Yenigün - okanyenigun@gmail.com
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file optimx-0.0.3.tar.gz.
File metadata
- Download URL: optimx-0.0.3.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
412aed847fa51013f4d7acee060dadc37337a8be67493edff98bf0787b1cf904
|
|
| MD5 |
168d71840b25b3ef2ec99a982e5f0777
|
|
| BLAKE2b-256 |
3adc375473fdfa060db4d6cb647776beb912f7b259f3dab7d4ae26bd06e65163
|
File details
Details for the file optimx-0.0.3-py3-none-any.whl.
File metadata
- Download URL: optimx-0.0.3-py3-none-any.whl
- Upload date:
- Size: 12.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5bb5629faa1c43cca37ab270f0225a9a0a45a88b4e236078e65f85f87a7be095
|
|
| MD5 |
ba4cd2f2e5d831f940438b69777f0359
|
|
| BLAKE2b-256 |
49aca3007cb613c830bb8ebf2206c71e9c8f3de5687b0f002c30ce418373dd02
|