forked from mkaspr/ilp-solver
/
ilp_solver.py
85 lines (69 loc) · 1.89 KB
/
ilp_solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
import sys
import time
from mpi_ilp import *
from mpi4py import MPI
from serial import Serial
from master import Master
from worker import Worker
from solver import Solver
from solver import SolverError
from problem_generator import ProblemGenerator
from time import clock
# runs serial or parallel solver
def startMaster(processCount):
try:
# get and show solution
problem = getProblem()
solution = getSolution(problem, processCount)
showSolution(solution)
# no solution found
except SolverError:
showError()
# reads in LP problem
def getProblem():
# validate arguments
if len(sys.argv) != 4:
print "ERROR: invalid arguments"
print "usage: rows cols seed"
exit(0)
# read arguments
rows = int(sys.argv[1])
cols = int(sys.argv[2])
seed = int(sys.argv[3])
# return random generator problem
generator = ProblemGenerator()
problem = generator.generate(rows, cols, seed)
return problem
# gets solution from serial or parallel solver
def getSolution(problem, processCount):
# single-process program
if processCount == 1:
solver = Serial(problem)
# multi-process program
else:
workerCount = processCount - 1
solver = Master(problem, workerCount)
# get solution using correct solver
return solver.solve()
# displays valid solution
def showSolution(solution):
print solution
# displays non-solution error
def showError():
print "No solution found"
# determine how MPI process should run
if __name__ == "__main__":
start = clock()
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
# master process
if rank == MASTER_RANK:
startMaster(size)
end = clock()
print "RUNTIME: " + str(end - start)
# worker process
else:
worker = Worker()
worker.start()