Skip to main content

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)

The shortest path problem in a graph can be solved using Dijkstra's algorithm.

distance_matrix = generate_square_distances(10)

names = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]

path, dist = ox.solve_shortest_path(distance_matrix, start_node="A", goal_node="D", node_names=names)

print(f"Shortest path: {path}")
print(f"Distance: {dist}")

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:


Release files for optimx 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for optimx 0.0.6
File Size Uploaded
optimx-0.0.6.tar.gz 15.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for optimx 0.0.6
File Interpreter ABI Platform
optimx-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 35.5 kB

Release files / optimx-0.0.6.tar.gz

Download URL optimx-0.0.6.tar.gz
Size 15.1 kB
Tags Source
SHA-256 checksum
How to use checksums
bcb32894028aba4d4cff6d1bb1d4f87ac75105837b664c9cba4fae25152efafd
BLAKE2b-256 checksum
How to use checksums
dfcbb0995ffd36c8c225d4bb1acfec05dbba4ca2b3e48e6d1f7faf40eda1b350
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.5

Release files / optimx-0.0.6-py3-none-any.whl

Download URL optimx-0.0.6-py3-none-any.whl
Size 20.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3617f3cfbdb18da29d936f9d8564d786f2179771a6ef5a6576d7abf467867868
BLAKE2b-256 checksum
How to use checksums
0b6f64e6ecc56dd7384806b3fa3489bee28f99633da75aef43e4464faf238f0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.5

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page