-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'latest' of https://github.com/ERGO-Code/HiGHS into pres…
…olveBugTransform
- Loading branch information
Showing
14 changed files
with
850 additions
and
326 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
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
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 |
---|---|---|
@@ -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))) |
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
Oops, something went wrong.