Skip to main content

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}")

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:

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

optimx-0.0.2.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

optimx-0.0.2-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file optimx-0.0.2.tar.gz.

File metadata

  • Download URL: optimx-0.0.2.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for optimx-0.0.2.tar.gz
Algorithm Hash digest
SHA256 87ffb231f1a54cb7398a598343284da6166849d2f5f151187b20783abb63699e
MD5 96b8ecf42f107f54a3b8c830543618aa
BLAKE2b-256 a6a5c4a48acd562e3081c0f8d392dd80bf062332e8d4f8f9f5cc2e910db56f2e

See more details on using hashes here.

File details

Details for the file optimx-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: optimx-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for optimx-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 974826bca0bd86e83bac20d46a9eb0740116e90620838c782928ef488257b979
MD5 0a84f52b16a64da9548cf42b0554361b
BLAKE2b-256 bc64cddb6030378d6bc1bad59e249fab0cb2bd5cd9cb231baef3d82f4f093165

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page