remove python dependencies on apputils, gflags
This commit is contained in:
@@ -12,16 +12,19 @@
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
from google.apputils import app
|
||||
import gflags
|
||||
import argparse
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
from ortools.linear_solver import pywraplp
|
||||
|
||||
FLAGS = gflags.FLAGS
|
||||
gflags.DEFINE_integer('load_min', 480, 'Minimum load in minutes')
|
||||
gflags.DEFINE_integer('load_max', 540, 'Maximum load in minutes')
|
||||
gflags.DEFINE_integer('commute_time', 30, 'Commute time in minutes')
|
||||
gflags.DEFINE_integer('num_workers', 98, 'Maximum number of workers.')
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--load_min', default = 480, type = int,
|
||||
help = 'Minimum load in minutes')
|
||||
parser.add_argument('--load_max', default = 540, type = int,
|
||||
help = 'Maximum load in minutes')
|
||||
parser.add_argument('--commute_time', default = 30, type = int,
|
||||
help = 'Commute time in minutes')
|
||||
parser.add_argument('--num_workers', default = 98, type = int,
|
||||
help = 'Maximum number of workers.')
|
||||
|
||||
|
||||
def FindCombinations(durations, load_min, load_max, commute_time):
|
||||
@@ -102,17 +105,17 @@ def Select(combinations, loads, max_number_of_workers):
|
||||
return -1, []
|
||||
|
||||
|
||||
def GetOptimalSchedule(demand):
|
||||
def GetOptimalSchedule(demand, args):
|
||||
"""Computes the optimal schedule for the appointment selection problem."""
|
||||
combinations = FindCombinations([a[2] for a in demand],
|
||||
FLAGS.load_min,
|
||||
FLAGS.load_max,
|
||||
FLAGS.commute_time)
|
||||
args.load_min,
|
||||
args.load_max,
|
||||
args.commute_time)
|
||||
print 'found %d possible combinations of appointements' % len(combinations)
|
||||
|
||||
cost, selection = Select(combinations,
|
||||
[a[0] for a in demand],
|
||||
FLAGS.num_workers)
|
||||
args.num_workers)
|
||||
output = [(selection[i], [(combinations[i][t], demand[t][1])
|
||||
for t in range(len(demand))
|
||||
if combinations[i][t] != 0])
|
||||
@@ -120,15 +123,15 @@ def GetOptimalSchedule(demand):
|
||||
return cost, output
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
def main(args):
|
||||
demand = [(40, 'A1', 90), (30, 'A2', 120), (25, 'A3', 180)]
|
||||
print 'appointments: '
|
||||
for a in demand:
|
||||
print ' %d * %s : %d min' % (a[0], a[1], a[2])
|
||||
print 'commute time = %d' % FLAGS.commute_time
|
||||
print 'accepted total duration = [%d..%d]' % (FLAGS.load_min, FLAGS.load_max)
|
||||
print '%d workers' % FLAGS.num_workers
|
||||
cost, selection = GetOptimalSchedule(demand)
|
||||
print 'commute time = %d' % args.commute_time
|
||||
print 'accepted total duration = [%d..%d]' % (args.load_min, args.load_max)
|
||||
print '%d workers' % args.num_workers
|
||||
cost, selection = GetOptimalSchedule(demand, args)
|
||||
print 'Optimal solution as a cost of %d' % cost
|
||||
for template in selection:
|
||||
print '%d schedules with ' % template[0]
|
||||
@@ -137,4 +140,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main(parser.parse_args())
|
||||
|
||||
@@ -12,14 +12,13 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from google.apputils import app
|
||||
import gflags
|
||||
import argparse
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
|
||||
FLAGS = gflags.FLAGS
|
||||
parser = argparse.ArgumentParser()
|
||||
|
||||
gflags.DEFINE_string('data', 'python/data/bacp/bacp12.txt',
|
||||
'path to data file')
|
||||
parser.add_argument('--data', default = 'data/bacp/bacp12.txt',
|
||||
help = 'path to data file')
|
||||
|
||||
#----------------helper for binpacking posting----------------
|
||||
|
||||
@@ -48,10 +47,10 @@ def ReadData(filename):
|
||||
return (credits, nb_periods, prereq)
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
def main(args):
|
||||
#------------------solver and variable declaration-------------
|
||||
|
||||
credits, nb_periods, prereq = ReadData(FLAGS.data)
|
||||
credits, nb_periods, prereq = ReadData(args.data)
|
||||
nb_courses = len(credits)
|
||||
|
||||
solver = pywrapcp.Solver('Balanced Academic Curriculum Problem')
|
||||
@@ -85,4 +84,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main(parser.parse_args())
|
||||
|
||||
@@ -58,7 +58,6 @@
|
||||
http://www.hakank.org/google_or_tools/
|
||||
"""
|
||||
|
||||
from google.apputils import app
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
|
||||
|
||||
@@ -200,4 +199,4 @@ def print_solution(A, E, alpha, n, word_len):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -21,20 +21,15 @@ between all marks are all different. The objective is to minimize the length
|
||||
of the rule.
|
||||
"""
|
||||
|
||||
|
||||
|
||||
from google.apputils import app
|
||||
import gflags
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
|
||||
FLAGS = gflags.FLAGS
|
||||
|
||||
# We disable the following warning because it is a false positive on constraints
|
||||
# like: solver.Add(x == 0)
|
||||
# pylint: disable=g-explicit-bool-comparison
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
def main():
|
||||
# Create the solver.
|
||||
solver = pywrapcp.Solver('golomb ruler')
|
||||
|
||||
@@ -78,4 +73,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
'''
|
||||
"""
|
||||
|
||||
from google.apputils import app
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
|
||||
|
||||
@@ -204,4 +203,4 @@ def PrintMatrix(game):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -14,8 +14,6 @@
|
||||
"""Integer programming examples that show how to use the APIs."""
|
||||
|
||||
|
||||
from google.apputils import app
|
||||
|
||||
from ortools.linear_solver import pywraplp
|
||||
|
||||
|
||||
@@ -122,4 +120,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -24,14 +24,10 @@ jobs. This is called the makespan.
|
||||
|
||||
|
||||
|
||||
from google.apputils import app
|
||||
import gflags
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
|
||||
FLAGS = gflags.FLAGS
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
def main():
|
||||
# Creates the solver.
|
||||
solver = pywrapcp.Solver('jobshop ft06')
|
||||
|
||||
@@ -106,4 +102,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -23,12 +23,8 @@ jobs. This is called the makespan.
|
||||
"""
|
||||
|
||||
|
||||
|
||||
from google.apputils import app
|
||||
import gflags
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
|
||||
FLAGS = gflags.FLAGS
|
||||
|
||||
class Dist:
|
||||
def __init__(self):
|
||||
@@ -38,7 +34,7 @@ class Dist:
|
||||
return abs(x - y)
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
def main():
|
||||
# Creates the solver.
|
||||
solver = pywrapcp.Solver('jobshop ft06')
|
||||
|
||||
@@ -119,4 +115,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -15,14 +15,10 @@
|
||||
|
||||
|
||||
|
||||
from google.apputils import app
|
||||
import gflags
|
||||
from ortools.algorithms import pywrapknapsack_solver
|
||||
|
||||
FLAGS = gflags.FLAGS
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
def main():
|
||||
# Create the solver.
|
||||
solver = pywrapknapsack_solver.KnapsackSolver(
|
||||
pywrapknapsack_solver.KnapsackSolver.
|
||||
@@ -49,4 +45,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
|
||||
|
||||
|
||||
from google.apputils import app
|
||||
from ortools.graph import pywrapgraph
|
||||
|
||||
|
||||
@@ -59,4 +58,4 @@ def main(unused_argv):
|
||||
RunAssignmentOn4x4Matrix()
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -14,8 +14,6 @@
|
||||
"""Linear programming examples that show how to use the APIs."""
|
||||
|
||||
|
||||
from google.apputils import app
|
||||
|
||||
from ortools.linear_solver import linear_solver_pb2
|
||||
from ortools.linear_solver import pywraplp
|
||||
|
||||
@@ -131,4 +129,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -21,14 +21,10 @@ distribute().
|
||||
|
||||
|
||||
|
||||
from google.apputils import app
|
||||
import gflags
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
|
||||
FLAGS = gflags.FLAGS
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
def main():
|
||||
# Create the solver.
|
||||
solver = pywrapcp.Solver('magic sequence')
|
||||
|
||||
@@ -48,4 +44,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
|
||||
|
||||
|
||||
from google.apputils import app
|
||||
from ortools.graph import pywrapgraph
|
||||
|
||||
|
||||
@@ -84,4 +83,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
from google.apputils import app
|
||||
import gflags
|
||||
import random
|
||||
|
||||
class OneVarLns(pywrapcp.BaseLns):
|
||||
@@ -113,11 +111,11 @@ def Solve(type):
|
||||
print 'Objective value = %d' % collector.ObjectiveValue(0)
|
||||
|
||||
|
||||
def main(_):
|
||||
def main():
|
||||
Solve(0)
|
||||
Solve(1)
|
||||
Solve(2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -18,8 +18,6 @@ This example file can also be used as a unit test.
|
||||
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
|
||||
from google.apputils import app
|
||||
|
||||
|
||||
class OneVarLns(pywrapcp.PyLns):
|
||||
"""One Var LNS."""
|
||||
@@ -139,4 +137,4 @@ def main(_):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -20,14 +20,10 @@ Each letter corresponds to one figure and all letters have different values.
|
||||
|
||||
|
||||
|
||||
from google.apputils import app
|
||||
import gflags
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
|
||||
FLAGS = gflags.FLAGS
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
def main():
|
||||
# Create the solver.
|
||||
solver = pywrapcp.Solver('SEND + MORE = MONEY')
|
||||
|
||||
@@ -63,4 +59,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -27,15 +27,11 @@ in the meeting and a maximum of non mandatory people are also in the meeting.
|
||||
|
||||
|
||||
|
||||
from google.apputils import app
|
||||
import gflags
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
|
||||
FLAGS = gflags.FLAGS
|
||||
|
||||
|
||||
# pylint: disable=too-many-statements
|
||||
def main(unused_argv):
|
||||
def main():
|
||||
# Create the solver.
|
||||
solver = pywrapcp.Solver('simple meeting scheduler')
|
||||
|
||||
@@ -212,4 +208,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -12,16 +12,14 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from google.apputils import app
|
||||
import gflags
|
||||
import argparse
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
|
||||
FLAGS = gflags.FLAGS
|
||||
|
||||
gflags.DEFINE_string('data', 'data/steel_mill/steel_mill_slab.txt',
|
||||
'path to data file')
|
||||
gflags.DEFINE_integer('time_limit', 20000, 'global time limit')
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--data', default = 'data/steel_mill/steel_mill_slab.txt',
|
||||
help = 'path to data file')
|
||||
parser.add_argument('--time_limit', default = 20000, type = int,
|
||||
help = 'global time limit')
|
||||
|
||||
#----------------helper for binpacking posting----------------
|
||||
|
||||
@@ -75,6 +73,7 @@ class SteelDecisionBuilder(pywrapcp.PyDecisionBuilder):
|
||||
'''
|
||||
|
||||
def __init__(self, x, nb_slabs, weights, losstab, loads):
|
||||
pywrapcp.PyDecisionBuilder.__init__(self)
|
||||
self.__x = x
|
||||
self.__nb_slabs = nb_slabs
|
||||
self.__weights = weights
|
||||
@@ -132,10 +131,10 @@ class SteelDecisionBuilder(pywrapcp.PyDecisionBuilder):
|
||||
return 'SteelMillDecisionBuilder(' + str(self.__x) + ')'
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
def main(args):
|
||||
#------------------solver and variable declaration-------------
|
||||
(nb_slabs, capacity, max_capacity, weights, colors, loss, color_orders) =\
|
||||
ReadData(FLAGS.data)
|
||||
ReadData(args.data)
|
||||
nb_colors = len(color_orders)
|
||||
solver = pywrapcp.Solver('Steel Mill Slab')
|
||||
x = [solver.IntVar(0, nb_slabs - 1, 'x' + str(i))
|
||||
@@ -163,7 +162,7 @@ def main(unused_argv):
|
||||
|
||||
db = SteelDecisionBuilder(x, nb_slabs, weights, loss, load_vars)
|
||||
search_log = solver.SearchLog(100000, objective_var)
|
||||
global_limit = solver.TimeLimit(FLAGS.time_limit)
|
||||
global_limit = solver.TimeLimit(args.time_limit)
|
||||
solver.NewSearch(db, [objective, search_log, global_limit])
|
||||
while solver.NextSolution():
|
||||
print 'Objective:', objective_var.Value(),\
|
||||
@@ -172,4 +171,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main(parser.parse_args())
|
||||
|
||||
@@ -12,24 +12,22 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import argparse
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
from google.apputils import app
|
||||
import gflags
|
||||
import random
|
||||
|
||||
FLAGS = gflags.FLAGS
|
||||
parser = argparse.ArgumentParser()
|
||||
|
||||
gflags.DEFINE_string('data',
|
||||
'data/steel_mill/steel_mill_slab.txt',
|
||||
'path to data file')
|
||||
gflags.DEFINE_integer('lns_fragment_size',
|
||||
10,
|
||||
'size of the random lns fragment')
|
||||
gflags.DEFINE_integer('lns_random_seed', 0, 'seed for the lns random generator')
|
||||
gflags.DEFINE_integer('lns_fail_limit',
|
||||
30,
|
||||
'fail limit when exploring fragments')
|
||||
gflags.DEFINE_integer('time_limit', 20000, 'global time limit')
|
||||
parser.add_argument('--data', default = 'data/steel_mill/steel_mill_slab.txt',
|
||||
help = 'path to data file')
|
||||
parser.add_argument('--time_limit', default = 20000, type = int,
|
||||
help = 'global time limit')
|
||||
parser.add_argument('--lns_fragment_size', default = 10, type = int,
|
||||
help = 'size of the random lns fragment')
|
||||
parser.add_argument('--lns_random_seed', default = 0, type = int,
|
||||
help = 'seed for the lns random generator')
|
||||
parser.add_argument('--lns_fail_limit', default = 30, type = int,
|
||||
help = 'fail limit when exploring fragments')
|
||||
|
||||
|
||||
# ---------- helper for binpacking posting ----------
|
||||
@@ -69,7 +67,6 @@ def ReadData(filename):
|
||||
|
||||
# ---------- dedicated search for this problem ----------
|
||||
|
||||
|
||||
class SteelDecisionBuilder(pywrapcp.PyDecisionBuilder):
|
||||
'''Dedicated Decision Builder for steel mill slab.
|
||||
|
||||
@@ -84,6 +81,7 @@ class SteelDecisionBuilder(pywrapcp.PyDecisionBuilder):
|
||||
'''
|
||||
|
||||
def __init__(self, x, nb_slabs, weights, loss_array, loads):
|
||||
pywrapcp.PyDecisionBuilder.__init__(self)
|
||||
self.__x = x
|
||||
self.__nb_slabs = nb_slabs
|
||||
self.__weights = weights
|
||||
@@ -144,11 +142,11 @@ class SteelDecisionBuilder(pywrapcp.PyDecisionBuilder):
|
||||
# ----------- LNS Operator ----------
|
||||
|
||||
|
||||
class SteelRandomLns(pywrapcp.PyLns):
|
||||
class SteelRandomLns(pywrapcp.BaseLns):
|
||||
"""Random LNS for Steel."""
|
||||
|
||||
def __init__(self, x, rand, lns_size):
|
||||
pywrapcp.PyLns.__init__(self, x)
|
||||
pywrapcp.BaseLns.__init__(self, x)
|
||||
self.__random = rand
|
||||
self.__lns_size = lns_size
|
||||
|
||||
@@ -156,19 +154,18 @@ class SteelRandomLns(pywrapcp.PyLns):
|
||||
pass
|
||||
|
||||
def NextFragment(self):
|
||||
fragment = []
|
||||
while len(fragment) < self.__lns_size:
|
||||
while self.FragmentSize() < self.__lns_size:
|
||||
pos = self.__random.randint(0, self.Size() - 1)
|
||||
fragment.append(pos)
|
||||
return fragment
|
||||
self.AppendToFragment(pos)
|
||||
return True
|
||||
|
||||
# ----------- Main Function -----------
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
def main(args):
|
||||
# ----- solver and variable declaration -----
|
||||
(nb_slabs, capacity, max_capacity, weights, colors, loss, color_orders) =\
|
||||
ReadData(FLAGS.data)
|
||||
ReadData(args.data)
|
||||
nb_colors = len(color_orders)
|
||||
solver = pywrapcp.Solver('Steel Mill Slab')
|
||||
x = [solver.IntVar(0, nb_slabs - 1, 'x' + str(i))
|
||||
@@ -210,22 +207,22 @@ def main(unused_argv):
|
||||
solver.CHOOSE_RANDOM,
|
||||
solver.ASSIGN_MIN_VALUE)
|
||||
# The most important aspect is to limit the time exploring each fragment.
|
||||
inner_limit = solver.FailuresLimit(FLAGS.lns_fail_limit)
|
||||
inner_limit = solver.FailuresLimit(args.lns_fail_limit)
|
||||
continuation_db = solver.SolveOnce(inner_db, [inner_limit])
|
||||
|
||||
# Now, we create the LNS objects.
|
||||
rand = random.Random()
|
||||
rand.seed(FLAGS.lns_random_seed)
|
||||
local_search_operator = SteelRandomLns(x, rand, FLAGS.lns_fragment_size)
|
||||
rand.seed(args.lns_random_seed)
|
||||
local_search_operator = SteelRandomLns(x, rand, args.lns_fragment_size)
|
||||
# This is in fact equivalent to the following predefined LNS operator:
|
||||
# local_search_operator = solver.RandomLNSOperator(x,
|
||||
# FLAGS.lns_fragment_size,
|
||||
# FLAGS.lns_random_seed)
|
||||
# args.lns_fragment_size,
|
||||
# args.lns_random_seed)
|
||||
local_search_parameters = solver.LocalSearchPhaseParameters(
|
||||
local_search_operator, continuation_db)
|
||||
local_search_db = solver.LocalSearchPhase(first_solution,
|
||||
local_search_parameters)
|
||||
global_limit = solver.TimeLimit(FLAGS.time_limit)
|
||||
global_limit = solver.TimeLimit(args.time_limit)
|
||||
|
||||
print 'using LNS to improve the initial solution'
|
||||
|
||||
@@ -238,4 +235,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main(parser.parse_args())
|
||||
|
||||
@@ -15,14 +15,9 @@
|
||||
|
||||
|
||||
|
||||
from google.apputils import app
|
||||
import gflags
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
|
||||
FLAGS = gflags.FLAGS
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
def main():
|
||||
# Create the solver.
|
||||
solver = pywrapcp.Solver('sudoku')
|
||||
|
||||
@@ -91,4 +86,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -26,22 +26,20 @@
|
||||
|
||||
|
||||
import random
|
||||
|
||||
from google.apputils import app
|
||||
import gflags
|
||||
import argparse
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
|
||||
FLAGS = gflags.FLAGS
|
||||
|
||||
gflags.DEFINE_integer('tsp_size', 10,
|
||||
'Size of Traveling Salesman Problem instance.')
|
||||
gflags.DEFINE_boolean('tsp_use_random_matrix', True,
|
||||
'Use random cost matrix.')
|
||||
gflags.DEFINE_integer('tsp_random_forbidden_connections', 0,
|
||||
'Number of random forbidden connections.')
|
||||
gflags.DEFINE_integer('tsp_random_seed', 0, 'Random seed.')
|
||||
gflags.DEFINE_boolean('light_propagation', False, 'Use light propagation')
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--tsp_size', default = 10, type = int,
|
||||
help='Size of Traveling Salesman Problem instance.')
|
||||
parser.add_argument('--tsp_use_random_matrix', default=True, type=bool,
|
||||
help='Use random cost matrix.')
|
||||
parser.add_argument('--tsp_random_forbidden_connections', default = 0,
|
||||
type = int, help='Number of random forbidden connections.')
|
||||
parser.add_argument('--tsp_random_seed', default = 0, type = int,
|
||||
help = 'Random seed.')
|
||||
parser.add_argument('--light_propagation', default = False,
|
||||
type = bool, help = 'Use light propagation')
|
||||
|
||||
# Cost/distance functions.
|
||||
|
||||
@@ -55,11 +53,11 @@ def Distance(i, j):
|
||||
class RandomMatrix(object):
|
||||
"""Random matrix."""
|
||||
|
||||
def __init__(self, size):
|
||||
def __init__(self, size, seed):
|
||||
"""Initialize random matrix."""
|
||||
|
||||
rand = random.Random()
|
||||
rand.seed(FLAGS.tsp_random_seed)
|
||||
rand.seed(seed)
|
||||
distance_max = 100
|
||||
self.matrix = {}
|
||||
for from_node in xrange(size):
|
||||
@@ -74,19 +72,19 @@ class RandomMatrix(object):
|
||||
return self.matrix[from_node][to_node]
|
||||
|
||||
|
||||
def main(_):
|
||||
def main(args):
|
||||
# Create routing model
|
||||
if FLAGS.tsp_size > 0:
|
||||
if args.tsp_size > 0:
|
||||
# Set a global parameter.
|
||||
param = pywrapcp.RoutingParameters()
|
||||
param.use_light_propagation = FLAGS.light_propagation
|
||||
param.use_light_propagation = args.light_propagation
|
||||
pywrapcp.RoutingModel.SetGlobalParameters(param)
|
||||
|
||||
# TSP of size FLAGS.tsp_size
|
||||
# TSP of size args.tsp_size
|
||||
# Second argument = 1 to build a single tour (it's a TSP).
|
||||
# Nodes are indexed from 0 to FLAGS_tsp_size - 1, by default the start of
|
||||
# Nodes are indexed from 0 to parser_tsp_size - 1, by default the start of
|
||||
# the route is node 0.
|
||||
routing = pywrapcp.RoutingModel(FLAGS.tsp_size, 1)
|
||||
routing = pywrapcp.RoutingModel(args.tsp_size, 1)
|
||||
|
||||
parameters = pywrapcp.RoutingSearchParameters()
|
||||
# Setting first solution heuristic (cheapest addition).
|
||||
@@ -99,19 +97,19 @@ def main(_):
|
||||
# Put a callback to the distance accessor here. The callback takes two
|
||||
# arguments (the from and to node inidices) and returns the distance between
|
||||
# these nodes.
|
||||
matrix = RandomMatrix(FLAGS.tsp_size)
|
||||
matrix = RandomMatrix(args.tsp_size, args.tsp_random_seed)
|
||||
matrix_callback = matrix.Distance
|
||||
if FLAGS.tsp_use_random_matrix:
|
||||
if args.tsp_use_random_matrix:
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(matrix_callback)
|
||||
else:
|
||||
routing.SetArcCostEvaluatorOfAllVehicles(Distance)
|
||||
# Forbid node connections (randomly).
|
||||
rand = random.Random()
|
||||
rand.seed(FLAGS.tsp_random_seed)
|
||||
rand.seed(args.tsp_random_seed)
|
||||
forbidden_connections = 0
|
||||
while forbidden_connections < FLAGS.tsp_random_forbidden_connections:
|
||||
from_node = rand.randrange(FLAGS.tsp_size - 1)
|
||||
to_node = rand.randrange(FLAGS.tsp_size - 1) + 1
|
||||
while forbidden_connections < args.tsp_random_forbidden_connections:
|
||||
from_node = rand.randrange(args.tsp_size - 1)
|
||||
to_node = rand.randrange(args.tsp_size - 1) + 1
|
||||
if routing.NextVar(from_node).Contains(to_node):
|
||||
print 'Forbidding connection ' + str(from_node) + ' -> ' + str(to_node)
|
||||
routing.NextVar(from_node).RemoveValue(to_node)
|
||||
@@ -138,4 +136,4 @@ def main(_):
|
||||
print 'Specify an instance greater than 0.'
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main(parser.parse_args())
|
||||
|
||||
@@ -35,15 +35,11 @@ Who owns a zebra and who drinks water?
|
||||
|
||||
|
||||
|
||||
from google.apputils import app
|
||||
import gflags
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
|
||||
FLAGS = gflags.FLAGS
|
||||
|
||||
|
||||
# pylint: disable=too-many-statements
|
||||
def main(unused_argv):
|
||||
def main():
|
||||
# Create the solver.
|
||||
solver = pywrapcp.Solver('zebra')
|
||||
|
||||
@@ -122,4 +118,4 @@ def main(unused_argv):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
"""Code for issue 46 in or-tools."""
|
||||
|
||||
from google.apputils import app
|
||||
from ortools.constraint_solver import pywrapcp
|
||||
|
||||
class AssignToStartMin(pywrapcp.PyDecisionBuilder):
|
||||
@@ -75,10 +74,10 @@ def Sequence():
|
||||
print [collector.EndValue(i, tasks[j]) for j in range(3)]
|
||||
|
||||
|
||||
def main(_):
|
||||
def main():
|
||||
NoSequence()
|
||||
Sequence()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
main()
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
GFLAGS_TAG = 2.1.2
|
||||
PROTOBUF_TAG = 3.0.0-beta-1
|
||||
GOOGLE_APPUTILS_TAG = 0.4.2
|
||||
SPARSEHASH_TAG = 2.0.3
|
||||
CBC_TAG = 2.9.7
|
||||
SWIG_TAG = 3.0.7
|
||||
@@ -265,12 +264,6 @@ dependencies/sources/protobuf-$(PROTOBUF_TAG)/configure: dependencies/sources/pr
|
||||
dependencies/sources/protobuf-$(PROTOBUF_TAG)/autogen.sh:
|
||||
git clone -b v$(PROTOBUF_TAG) https://github.com/google/protobuf.git dependencies/sources/protobuf-$(PROTOBUF_TAG)
|
||||
|
||||
# Intall Google Apputils Python.
|
||||
install_google_apputils: dependencies/sources/google-apputils-$(GOOGLE_APPUTILS_TAG)/README
|
||||
|
||||
dependencies/sources/google-apputils-$(GOOGLE_APPUTILS_TAG)/README:
|
||||
git clone https://github.com/google/google-apputils dependencies/sources/google-apputils-$(GOOGLE_APPUTILS_TAG)
|
||||
|
||||
# Install sparsehash.
|
||||
install_sparsehash: dependencies/install/include/google/dense_hash_map
|
||||
|
||||
@@ -487,8 +480,7 @@ clean_third_party:
|
||||
makefile_third_party: Makefile.local
|
||||
|
||||
# Install python modules
|
||||
install_python_modules: install_google_apputils install_protobuf
|
||||
cd dependencies/sources/google-apputils-$(GOOGLE_APPUTILS_TAG) && python$(PYTHON_VERSION) setup.py install --user
|
||||
install_python_modules: install_protobuf
|
||||
cd dependencies/sources/protobuf-$(PROTOBUF_TAG)/python && python$(PYTHON_VERSION) setup.py build
|
||||
cd dependencies/sources/protobuf-$(PROTOBUF_TAG)/python && python$(PYTHON_VERSION) setup.py install --user
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
# tags of dependencies to checkout.
|
||||
GFLAGS_TAG = 2.1.2
|
||||
PROTOBUF_TAG = 3.0.0-beta-1
|
||||
GOOGLE_APPUTILS_TAG = 0.4.2
|
||||
SPARSEHASH_TAG = 2.0.3
|
||||
CBC_TAG = 2.9.7
|
||||
ZLIB_TAG = 1.2.8
|
||||
@@ -192,7 +191,6 @@ download_third_party: \
|
||||
dependencies/sources/protobuf/autogen.sh \
|
||||
dependencies/sources/sparsehash/autogen.sh \
|
||||
dependencies/archives/swigwin-$(SWIG_TAG).zip \
|
||||
dependencies/sources/google-apputils/README \
|
||||
dependencies/sources/cbc-$(CBC_TAG)/configure
|
||||
|
||||
# Directories:
|
||||
@@ -287,7 +285,7 @@ dependencies\sources\protobuf-$(PROTOBUF_TAG)\cmake\build\include.tar: dependenc
|
||||
cd dependencies\sources\protobuf-$(PROTOBUF_TAG)\cmake\build && ..\..\..\..\..\tools\tar.exe cf include.tar include
|
||||
|
||||
dependencies\sources\protobuf-$(PROTOBUF_TAG)\cmake\build\protobuf.sln: dependencies\sources\protobuf-$(PROTOBUF_TAG)\cmake\CMakeLists.txt
|
||||
-md dependencies\sources\protobuf-$(PROTOBUF_TAG)\cmake\build
|
||||
-md dependencies\sources\protobuf-$(PROTOBUF_TAG)\cmake\build
|
||||
tools\sed -i -e '/\"\/MD\"/d' dependencies\sources\protobuf-$(PROTOBUF_TAG)\cmake\CMakeLists.txt
|
||||
ifeq ("$(PTRLENGTH)", "64")
|
||||
cd dependencies\sources\protobuf-$(PROTOBUF_TAG)\cmake\build && cmake -G "Visual Studio 12 2013 Win64" -DBUILD_TESTING=OFF ..
|
||||
@@ -301,15 +299,6 @@ dependencies\sources\protobuf-$(PROTOBUF_TAG)\cmake\CMakeLists.txt:
|
||||
tools\unzip -d dependencies\sources dependencies\archives\v$(PROTOBUF_TAG).zip
|
||||
|
||||
|
||||
# Intall Google Apputils Python.
|
||||
install_google_apputils: dependencies\sources\google-apputils-$(GOOGLE_APPUTILS_TAG)\README
|
||||
|
||||
dependencies\sources\google-apputils-$(GOOGLE_APPUTILS_TAG)\README:
|
||||
tools\wget -P dependencies\archives --no-check-certificate https://github.com/google/google-apputils/archive/master.zip
|
||||
tools\unzip -d dependencies\sources dependencies\archives\master.zip
|
||||
cd dependencies\sources && move google-apputils-master google-apputils-$(GOOGLE_APPUTILS_TAG)
|
||||
|
||||
|
||||
# Install sparsehash.
|
||||
install_sparsehash: dependencies\install\include\google\dense_hash_map
|
||||
|
||||
@@ -453,8 +442,7 @@ clean_third_party:
|
||||
makefile_third_party: Makefile.local
|
||||
|
||||
# Install python modules
|
||||
install_python_modules: install_google_apputils
|
||||
cd dependencies\sources\google-apputils-$(GOOGLE_APPUTILS_TAG) && $(WINDOWS_PYTHON_PATH)\\python.exe setup.py install --user
|
||||
install_python_modules: install_protobuf
|
||||
copy dependencies\install\bin\protoc.exe dependencies\sources\protobuf-$(PROTOBUF_TAG)\src
|
||||
cd dependencies\sources\protobuf-$(PROTOBUF_TAG)/python && $(WINDOWS_PYTHON_PATH)\\python.exe setup.py build
|
||||
cd dependencies\sources\protobuf-$(PROTOBUF_TAG)/python && $(WINDOWS_PYTHON_PATH)\\python.exe setup.py install --user
|
||||
|
||||
@@ -688,75 +688,6 @@ PROTECT_FROM_FAILURE(Solver::Fail(), arg1);
|
||||
|
||||
// ############ END DUPLICATED CODE BLOCK ############
|
||||
|
||||
// ============= Handling C++ flags ==============
|
||||
|
||||
// We "convert" the C++ flags to python gflags. We also need to actually
|
||||
// pass the value from the python flags to the C++ ones, upon the Solver
|
||||
// construction.
|
||||
// Phase 1: declare the C++ flags.
|
||||
%import "base/commandlineflags.h"
|
||||
%{
|
||||
DECLARE_bool(cp_trace_propagation);
|
||||
DECLARE_bool(cp_trace_search);
|
||||
DECLARE_bool(cp_print_model);
|
||||
DECLARE_bool(cp_model_stats);
|
||||
DECLARE_string(cp_export_file);
|
||||
DECLARE_bool(cp_no_solve);
|
||||
DECLARE_string(cp_profile_file);
|
||||
%}
|
||||
// Phase 2: define the python flags.
|
||||
%pythoncode {
|
||||
import gflags as flags
|
||||
FLAGS = flags.FLAGS
|
||||
flags.DEFINE_boolean('cp_trace_propagation', False,
|
||||
'trace all propagation events.')
|
||||
flags.DEFINE_boolean('cp_trace_search', False,
|
||||
'trace all search events.')
|
||||
flags.DEFINE_boolean('cp_print_model', False,
|
||||
'prints the model before solving it.')
|
||||
flags.DEFINE_boolean('cp_model_stats', False,
|
||||
'displays model statistics before solving it.')
|
||||
flags.DEFINE_string('cp_export_file', '',
|
||||
'exports model to file using CPModelProto.')
|
||||
flags.DEFINE_boolean('cp_no_solve', False,
|
||||
'force failures at the beginning of a search.')
|
||||
flags.DEFINE_string('cp_profile_file', '',
|
||||
'exports profiling overview to file.')
|
||||
} // %pythoncode
|
||||
// Phase 3: extend the Solver C++ object with a SetFlags() method that sets
|
||||
// all the important flags from its input parameters.
|
||||
%extend operations_research::Solver {
|
||||
static void SetFlags(bool trace_propagation,
|
||||
bool trace_search,
|
||||
bool print_model,
|
||||
bool model_stats,
|
||||
const std::string& export_file,
|
||||
bool no_solve,
|
||||
const std::string& profile_file) {
|
||||
FLAGS_cp_trace_propagation = trace_propagation;
|
||||
FLAGS_cp_trace_search = trace_search;
|
||||
FLAGS_cp_print_model = print_model;
|
||||
FLAGS_cp_model_stats = model_stats;
|
||||
FLAGS_cp_export_file = export_file;
|
||||
FLAGS_cp_no_solve = no_solve;
|
||||
FLAGS_cp_profile_file = profile_file;
|
||||
}
|
||||
}
|
||||
// Phase 4: at the beginning of the Solver's constructor code (in python), call
|
||||
// SetFlags() with the python flags as parameters.
|
||||
// Note: Indentation is critical here as the code is copied verbatim in the
|
||||
// python code.
|
||||
%feature("pythonprepend") operations_research::Solver::Solver %{
|
||||
Solver.SetFlags(FLAGS.cp_trace_propagation,
|
||||
FLAGS.cp_trace_search,
|
||||
FLAGS.cp_print_model,
|
||||
FLAGS.cp_model_stats,
|
||||
FLAGS.cp_export_file,
|
||||
FLAGS.cp_no_solve,
|
||||
FLAGS.cp_profile_file)
|
||||
%}
|
||||
|
||||
|
||||
// ============= Exposed C++ API : Solver class ==============
|
||||
|
||||
%ignoreall
|
||||
|
||||
@@ -20,27 +20,6 @@
|
||||
#include "constraint_solver/routing.h"
|
||||
%}
|
||||
|
||||
// We "convert" the C++ flags to python gflags. We also need to actually
|
||||
// pass the value from the python flags to the C++ ones, upon the
|
||||
// RoutingModel construction. This is a copy paste of the same code on the
|
||||
// Solver class in ./constraint_solver.swig.
|
||||
|
||||
// As with the solver ctor, at the beginning of the routing model's
|
||||
// constructor code (in python), call SetFlags() with the python flags
|
||||
// as parameters.
|
||||
// Note: Indentation is critical here as the code is copied verbatim in the
|
||||
// python code.
|
||||
%feature("pythonprepend") operations_research::RoutingModel::RoutingModel %{
|
||||
Solver.SetFlags(FLAGS.cp_trace_propagation,
|
||||
FLAGS.cp_trace_search,
|
||||
FLAGS.cp_print_model,
|
||||
FLAGS.cp_model_stats,
|
||||
FLAGS.cp_export_file,
|
||||
FLAGS.cp_no_solve,
|
||||
FLAGS.cp_profile_file)
|
||||
%}
|
||||
|
||||
|
||||
// See ./constraint_solver_helpers.swig.
|
||||
//PY_CONVERT_HELPER_PTR(SearchMonitor);
|
||||
//PY_CONVERT_HELPER_PTR(IntervalVar);
|
||||
|
||||
@@ -25,8 +25,7 @@ setup(
|
||||
'ortools.linear_solver',],
|
||||
ext_modules = [dummy_module],
|
||||
install_requires = [
|
||||
'google-apputils >= 0.4',
|
||||
'protobuf >= 2.5.0'],
|
||||
'protobuf >= 2.8.0'],
|
||||
dependency_links = ['http://google-apputils-python.googlecode.com/files/'],
|
||||
package_data = {
|
||||
'ortools.constraint_solver' : ['_pywrapcp.dll'],
|
||||
|
||||
Reference in New Issue
Block a user