forked from ERGO-Code/HiGHS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor highspy for enhanced usability
Refactor highspy for enhanced usability This commit significantly improves the `Highs` class within `highs.py`, focusing on enhancing usability, efficiency, and robustness. Key changes include: - Added comprehensive docstrings. - Improved methods for adding, deleting, and retrieving multiple variables and constraints, for a more flexible and efficient API. - Standardized some API conventions. Note, this is a breaking change for the constraint value/dual methods. - Updated tests and examples.
- Loading branch information
1 parent
b73d4e8
commit 3b5c3da
Showing
7 changed files
with
824 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,40 @@ | ||
import highspy | ||
import time | ||
import networkx as nx | ||
from random import randint | ||
|
||
h = highspy.Highs() | ||
|
||
x1 = h.addVariable(lb = -h.inf) | ||
x2 = h.addVariable(lb = -h.inf) | ||
(x1, x2) = h.addVariables(2, lb = -h.inf) | ||
|
||
h.addConstr(x2 - x1 >= 2) | ||
h.addConstr(x1 + x2 >= 0) | ||
h.addConstrs(x2 - x1 >= 2, | ||
x1 + x2 >= 0) | ||
|
||
h.minimize(x2) | ||
|
||
|
||
|
||
h = highspy.Highs() | ||
|
||
|
||
G = nx.circular_ladder_graph(5).to_directed() | ||
nx.set_edge_attributes(G, {e: {'weight': randint(1, 9)} for e in G.edges}) | ||
|
||
d = h.addBinaries(G.edges, obj=nx.get_edge_attributes(G, 'weight')) | ||
|
||
h.addConstrs(sum(d[e] for e in G.in_edges(i)) - sum(d[e] for e in G.out_edges(i)) == 0 for i in G.nodes) | ||
|
||
h = highspy.Highs() | ||
|
||
ts = time.time() | ||
perf1 = [h.addBinary() for _ in range(1000000)] | ||
t1 = time.time() - ts | ||
print(t1) | ||
|
||
h = highspy.Highs() | ||
|
||
ts = time.time() | ||
perf2 = h.addVariables(1000000) | ||
t2 = time.time() - ts | ||
print(t2) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Example of a shortest path network flow in a graph | ||
# Shows integration of highspy with networkx | ||
|
||
import highspy | ||
import networkx as nx | ||
|
||
orig, dest = ('A', 'D') | ||
|
||
# create directed graph with edge weights (distances) | ||
G = nx.DiGraph() | ||
G.add_weighted_edges_from([('A', 'B', 2.0), ('B', 'C', 3.0), ('A', 'C', 1.5), ('B', 'D', 2.5), ('C', 'D', 1.0)]) | ||
|
||
h = highspy.Highs() | ||
h.silent() | ||
|
||
x = h.addBinaries(G.edges, obj=nx.get_edge_attributes(G, 'weight')) | ||
|
||
# add flow conservation constraints | ||
# { 1 if n = orig | ||
# sum(out) - sum(in) = { -1 if n = dest | ||
# { 0 otherwise | ||
rhs = lambda n: 1 if n == orig else -1 if n == dest else 0 | ||
flow = lambda E: sum((x[e] for e in E)) | ||
|
||
h.addConstrs(flow(G.out_edges(n)) - flow(G.in_edges(n)) == rhs(n) for n in G.nodes) | ||
h.minimize() | ||
|
||
# Print the solution | ||
print('Shortest path from', orig, 'to', dest, 'is: ', end = '') | ||
sol = h.vals(x) | ||
|
||
n = orig | ||
while n != dest: | ||
print(n, end=' ') | ||
n = next(e[1] for e in G.out_edges(n) if sol[e] > 0.5) | ||
|
||
print(dest) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# This is an example of the N-Queens problem, which is a classic combinatorial problem. | ||
# The problem is to place N queens on an N x N chessboard so that no two queens attack each other. | ||
# | ||
# We show how to model the problem as a MIP and solve it using highspy. | ||
# Using numpy can simplify the construction of the constraints (i.e., diagonal). | ||
|
||
import highspy | ||
import numpy as np | ||
|
||
N = 8 | ||
h = highspy.Highs() | ||
h.silent() | ||
|
||
x = np.reshape(h.addBinaries(N*N), (N, N)) | ||
|
||
h.addConstrs(sum(x[i,:]) == 1 for i in range(N)) # each row has exactly one queen | ||
h.addConstrs(sum(x[:,j]) == 1 for j in range(N)) # each col has exactly one queen | ||
|
||
y = np.fliplr(x) | ||
h.addConstrs(x.diagonal(k).sum() <= 1 for k in range(-N + 1, N)) # each diagonal has at most one queen | ||
h.addConstrs(y.diagonal(k).sum() <= 1 for k in range(-N + 1, N)) # each 'reverse' diagonal has at most one queen | ||
|
||
h.solve() | ||
sol = np.array(h.vals(x)) | ||
|
||
print('Queens:') | ||
|
||
for i in range(N): | ||
print(''.join('Q' if sol[i, j] > 0.5 else '*' for j in range(N))) |
Oops, something went wrong.