examples/python: cleanup
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# [START program]
|
||||
"""Appointment selection.
|
||||
|
||||
@@ -29,11 +30,12 @@ from ortools.linear_solver import pywraplp
|
||||
from ortools.sat.python import cp_model
|
||||
# [END import]
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
flags.DEFINE_integer('load_min', 480, 'Minimum load in minutes.')
|
||||
flags.DEFINE_integer('load_max', 540, 'Maximum load in minutes.')
|
||||
flags.DEFINE_integer('commute_time', 30, 'Commute time in minutes.')
|
||||
flags.DEFINE_integer('num_workers', 98, 'Maximum number of workers.')
|
||||
_LOAD_MIN = flags.DEFINE_integer('load_min', 480, 'Minimum load in minutes.')
|
||||
_LOAD_MAX = flags.DEFINE_integer('load_max', 540, 'Maximum load in minutes.')
|
||||
_COMMUTE_TIME = flags.DEFINE_integer('commute_time', 30,
|
||||
'Commute time in minutes.')
|
||||
_NUM_WORKERS = flags.DEFINE_integer('num_workers', 98,
|
||||
'Maximum number of workers.')
|
||||
|
||||
|
||||
class AllSolutionCollector(cp_model.CpSolverSolutionCallback):
|
||||
@@ -118,7 +120,7 @@ def AggregateItemCollectionsOptimally(item_collections, max_num_collections,
|
||||
"""
|
||||
solver = pywraplp.Solver.CreateSolver('SCIP')
|
||||
if not solver:
|
||||
return []
|
||||
return []
|
||||
n = len(ideal_item_ratios)
|
||||
num_distinct_collections = len(item_collections)
|
||||
max_num_items_per_collection = 0
|
||||
@@ -191,13 +193,13 @@ def GetOptimalSchedule(demand):
|
||||
The same output type as EnumerateAllKnapsacksWithRepetition.
|
||||
"""
|
||||
combinations = EnumerateAllKnapsacksWithRepetition(
|
||||
[a[2] + FLAGS.commute_time for a in demand], FLAGS.load_min,
|
||||
FLAGS.load_max)
|
||||
[a[2] + _COMMUTE_TIME.value for a in demand], _LOAD_MIN.value,
|
||||
_LOAD_MAX.value)
|
||||
print(('Found %d possible day schedules ' % len(combinations) +
|
||||
'(i.e. combination of appointments filling up one worker\'s day)'))
|
||||
|
||||
selection = AggregateItemCollectionsOptimally(
|
||||
combinations, FLAGS.num_workers, [a[0] / 100.0 for a in demand])
|
||||
combinations, _NUM_WORKERS.value, [a[0] / 100.0 for a in demand])
|
||||
output = []
|
||||
for i in range(len(selection)):
|
||||
if selection[i] != 0:
|
||||
@@ -208,16 +210,16 @@ def GetOptimalSchedule(demand):
|
||||
return output
|
||||
|
||||
|
||||
def solve_appointments(_):
|
||||
def main(_):
|
||||
demand = [(45.0, 'Type1', 90), (30.0, 'Type2', 120), (25.0, 'Type3', 180)]
|
||||
print('*** input problem ***')
|
||||
print('Appointments: ')
|
||||
for a in demand:
|
||||
print(' %.2f%% of %s : %d min' % (a[0], a[1], a[2]))
|
||||
print('Commute time = %d' % FLAGS.commute_time)
|
||||
print('Commute time = %d' % _COMMUTE_TIME.value)
|
||||
print('Acceptable duration of a work day = [%d..%d]' %
|
||||
(FLAGS.load_min, FLAGS.load_max))
|
||||
print('%d workers' % FLAGS.num_workers)
|
||||
(_LOAD_MIN.value, _LOAD_MAX.value))
|
||||
print('%d workers' % _NUM_WORKERS.value)
|
||||
selection = GetOptimalSchedule(demand)
|
||||
print()
|
||||
installed = 0
|
||||
@@ -239,15 +241,17 @@ def solve_appointments(_):
|
||||
print()
|
||||
print('%d installations planned' % installed)
|
||||
for a in demand:
|
||||
name = a[1]
|
||||
per_type = installed_per_type[name]
|
||||
if installed != 0:
|
||||
print(f' {per_type} ({per_type * 100.0 / installed}%) installations of type {name} planned')
|
||||
else:
|
||||
print(f' {per_type} installations of type {name} planned')
|
||||
name = a[1]
|
||||
per_type = installed_per_type[name]
|
||||
if installed != 0:
|
||||
print(
|
||||
f' {per_type} ({per_type * 100.0 / installed}%) installations of type {name} planned'
|
||||
)
|
||||
else:
|
||||
print(f' {per_type} installations of type {name} planned')
|
||||
# [END print_solution]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(solve_appointments)
|
||||
app.run(main)
|
||||
# [END program]
|
||||
|
||||
@@ -31,13 +31,13 @@ from absl import flags
|
||||
from google.protobuf import text_format
|
||||
from ortools.sat.python import cp_model
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
|
||||
flags.DEFINE_string('output_proto', '',
|
||||
'Output file to write the cp_model proto to.')
|
||||
flags.DEFINE_string('params', 'num_search_workers:16,log_search_progress:true',
|
||||
'Sat solver parameters.')
|
||||
flags.DEFINE_integer('instance', 1, 'Instance to select (1, 2, 3).', 1, 3)
|
||||
_OUTPUT_PROTO = flags.DEFINE_string(
|
||||
'output_proto', '', 'Output file to write the cp_model proto to.')
|
||||
_PARAMS = flags.DEFINE_string('params',
|
||||
'num_search_workers:8,log_search_progress:true',
|
||||
'Sat solver parameters.')
|
||||
_INSTANCE = flags.DEFINE_integer('instance', 1, 'Instance to select (1, 2, 3).',
|
||||
1, 3)
|
||||
|
||||
SAMPLE_SHIFTS_SMALL = [
|
||||
#
|
||||
@@ -1688,11 +1688,11 @@ def bus_driver_scheduling(minimize_drivers, max_num_drivers):
|
||||
The objective value of the model.
|
||||
"""
|
||||
shifts = None
|
||||
if FLAGS.instance == 1:
|
||||
if _INSTANCE.value == 1:
|
||||
shifts = SAMPLE_SHIFTS_SMALL
|
||||
elif FLAGS.instance == 2:
|
||||
elif _INSTANCE.value == 2:
|
||||
shifts = SAMPLE_SHIFTS_MEDIUM
|
||||
elif FLAGS.instance == 3:
|
||||
elif _INSTANCE.value == 3:
|
||||
shifts = SAMPLE_SHIFTS_LARGE
|
||||
|
||||
num_shifts = len(shifts)
|
||||
@@ -1933,15 +1933,15 @@ def bus_driver_scheduling(minimize_drivers, max_num_drivers):
|
||||
model.Minimize(
|
||||
cp_model.LinearExpr.WeightedSum(delay_literals, delay_weights))
|
||||
|
||||
if not minimize_drivers and FLAGS.output_proto:
|
||||
print('Writing proto to %s' % FLAGS.output_proto)
|
||||
with open(FLAGS.output_proto, 'w') as text_file:
|
||||
if not minimize_drivers and _OUTPUT_PROTO.value:
|
||||
print('Writing proto to %s' % _OUTPUT_PROTO.value)
|
||||
with open(_OUTPUT_PROTO.value, 'w') as text_file:
|
||||
text_file.write(str(model))
|
||||
|
||||
# Solve model.
|
||||
solver = cp_model.CpSolver()
|
||||
if FLAGS.params:
|
||||
text_format.Parse(FLAGS.params, solver.parameters)
|
||||
if _PARAMS.value:
|
||||
text_format.Parse(_PARAMS.value, solver.parameters)
|
||||
|
||||
status = solver.Solve(model)
|
||||
|
||||
@@ -1978,7 +1978,7 @@ def bus_driver_scheduling(minimize_drivers, max_num_drivers):
|
||||
return int(solver.ObjectiveValue())
|
||||
|
||||
|
||||
def solve_bus_driver_scheduling():
|
||||
def main(_):
|
||||
"""Optimize the bus driver allocation in two passes."""
|
||||
print('----------- first pass: minimize the number of drivers')
|
||||
num_drivers = bus_driver_scheduling(True, -1)
|
||||
@@ -1989,9 +1989,5 @@ def solve_bus_driver_scheduling():
|
||||
bus_driver_scheduling(False, num_drivers)
|
||||
|
||||
|
||||
def main(_=None):
|
||||
solve_bus_driver_scheduling()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(main)
|
||||
|
||||
@@ -28,7 +28,7 @@ from ortools.sat.python import visualization
|
||||
from ortools.sat.python import cp_model
|
||||
|
||||
|
||||
def main(_=None):
|
||||
def main(_):
|
||||
"""Solves the gate scheduling problem."""
|
||||
model = cp_model.CpModel()
|
||||
|
||||
|
||||
@@ -189,7 +189,7 @@ def solve_hidato(puzzle, index):
|
||||
print(' - wall time : %f s' % solver.WallTime())
|
||||
|
||||
|
||||
def main(_=None):
|
||||
def main(_):
|
||||
for pb in range(1, 7):
|
||||
solve_hidato(build_puzzle(pb), pb)
|
||||
|
||||
|
||||
@@ -28,14 +28,13 @@ from google.protobuf import text_format
|
||||
|
||||
from ortools.sat.python import cp_model
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
|
||||
flags.DEFINE_string('output_proto', '',
|
||||
'Output file to write the cp_model proto to.')
|
||||
flags.DEFINE_string('params', 'num_search_workers:16,log_search_progress:true',
|
||||
'Sat solver parameters.')
|
||||
flags.DEFINE_string('model', 'rotation',
|
||||
'\'duplicate\' or \'rotation\' or \'optional\'')
|
||||
_OUTPUT_PROTO = flags.DEFINE_string(
|
||||
'output_proto', '', 'Output file to write the cp_model proto to.')
|
||||
_PARAMS = flags.DEFINE_string('params',
|
||||
'num_search_workers:16,log_search_progress:true',
|
||||
'Sat solver parameters.')
|
||||
_MODEL = flags.DEFINE_string('model', 'rotation',
|
||||
'\'duplicate\' or \'rotation\' or \'optional\'')
|
||||
|
||||
|
||||
def build_data():
|
||||
@@ -130,15 +129,15 @@ def solve_with_duplicate_items(data, max_height, max_width):
|
||||
model.Maximize(cp_model.LinearExpr.WeightedSum(is_used, item_values))
|
||||
|
||||
# Output proto to file.
|
||||
if FLAGS.output_proto:
|
||||
print('Writing proto to %s' % FLAGS.output_proto)
|
||||
with open(FLAGS.output_proto, 'w') as text_file:
|
||||
if _OUTPUT_PROTO.value:
|
||||
print('Writing proto to %s' % _OUTPUT_PROTO.value)
|
||||
with open(_OUTPUT_PROTO.value, 'w') as text_file:
|
||||
text_file.write(str(model))
|
||||
|
||||
# Solve model.
|
||||
solver = cp_model.CpSolver()
|
||||
if FLAGS.params:
|
||||
text_format.Parse(FLAGS.params, solver.parameters)
|
||||
if _PARAMS.value:
|
||||
text_format.Parse(_PARAMS.value, solver.parameters)
|
||||
|
||||
status = solver.Solve(model)
|
||||
|
||||
@@ -220,15 +219,15 @@ def solve_with_duplicate_optional_items(data, max_height, max_width):
|
||||
model.Maximize(cp_model.LinearExpr.WeightedSum(is_used, item_values))
|
||||
|
||||
# Output proto to file.
|
||||
if FLAGS.output_proto:
|
||||
print('Writing proto to %s' % FLAGS.output_proto)
|
||||
with open(FLAGS.output_proto, 'w') as text_file:
|
||||
if _OUTPUT_PROTO.value:
|
||||
print('Writing proto to %s' % _OUTPUT_PROTO.value)
|
||||
with open(_OUTPUT_PROTO.value, 'w') as text_file:
|
||||
text_file.write(str(model))
|
||||
|
||||
# Solve model.
|
||||
solver = cp_model.CpSolver()
|
||||
if FLAGS.params:
|
||||
text_format.Parse(FLAGS.params, solver.parameters)
|
||||
if _PARAMS.value:
|
||||
text_format.Parse(_PARAMS.value, solver.parameters)
|
||||
|
||||
status = solver.Solve(model)
|
||||
|
||||
@@ -333,15 +332,15 @@ def solve_with_rotations(data, max_height, max_width):
|
||||
model.Maximize(cp_model.LinearExpr.WeightedSum(is_used, item_values))
|
||||
|
||||
# Output proto to file.
|
||||
if FLAGS.output_proto:
|
||||
print('Writing proto to %s' % FLAGS.output_proto)
|
||||
with open(FLAGS.output_proto, 'w') as text_file:
|
||||
if _OUTPUT_PROTO.value:
|
||||
print('Writing proto to %s' % _OUTPUT_PROTO.value)
|
||||
with open(_OUTPUT_PROTO.value, 'w') as text_file:
|
||||
text_file.write(str(model))
|
||||
|
||||
# Solve model.
|
||||
solver = cp_model.CpSolver()
|
||||
if FLAGS.params:
|
||||
text_format.Parse(FLAGS.params, solver.parameters)
|
||||
if _PARAMS.value:
|
||||
text_format.Parse(_PARAMS.value, solver.parameters)
|
||||
|
||||
status = solver.Solve(model)
|
||||
|
||||
@@ -360,20 +359,16 @@ def solve_with_rotations(data, max_height, max_width):
|
||||
print(data)
|
||||
|
||||
|
||||
def solve_knapsack(model):
|
||||
def main(_):
|
||||
"""Solve the problem with all models."""
|
||||
data, max_height, max_width = build_data()
|
||||
if model == 'duplicate':
|
||||
if _MODEL.value == 'duplicate':
|
||||
solve_with_duplicate_items(data, max_height, max_width)
|
||||
elif model == 'optional':
|
||||
elif _MODEL.value == 'optional':
|
||||
solve_with_duplicate_optional_items(data, max_height, max_width)
|
||||
else:
|
||||
solve_with_rotations(data, max_height, max_width)
|
||||
|
||||
|
||||
def main(_=None):
|
||||
solve_knapsack(FLAGS.model)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(main)
|
||||
|
||||
@@ -35,15 +35,12 @@ from absl import flags
|
||||
from google.protobuf import text_format
|
||||
from ortools.sat.python import cp_model
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
|
||||
flags.DEFINE_string('input', '', 'Input file to parse and solve.')
|
||||
flags.DEFINE_string('params', '', 'Sat solver parameters.')
|
||||
flags.DEFINE_string('output_proto', '',
|
||||
'Output file to write the cp_model proto to.')
|
||||
flags.DEFINE_string('model', 'scheduling',
|
||||
'Model used: boolean, scheduling, greedy')
|
||||
|
||||
_INPUT = flags.DEFINE_string('input', '', 'Input file to parse and solve.')
|
||||
_PARAMS = flags.DEFINE_string('params', '', 'Sat solver parameters.')
|
||||
_OUTPUT_PROTO = flags.DEFINE_string(
|
||||
'output_proto', '', 'Output file to write the cp_model proto to.')
|
||||
_MODEL = flags.DEFINE_string('model', 'boolean',
|
||||
'Model used: boolean, scheduling, greedy')
|
||||
# pytype: disable=wrong-arg-types
|
||||
|
||||
|
||||
|
||||
@@ -19,12 +19,10 @@ from absl import flags
|
||||
from ortools.sat.python import cp_model
|
||||
from google.protobuf import text_format
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
|
||||
flags.DEFINE_string('output_proto', '',
|
||||
'Output file to write the cp_model proto to.')
|
||||
flags.DEFINE_string('params', 'max_time_in_seconds:10.0',
|
||||
'Sat solver parameters.')
|
||||
_OUTPUT_PROTO = flags.DEFINE_string(
|
||||
'output_proto', '', 'Output file to write the cp_model proto to.')
|
||||
_PARAMS = flags.DEFINE_string('params', 'max_time_in_seconds:10.0',
|
||||
'Sat solver parameters.')
|
||||
|
||||
|
||||
def negated_bounded_span(works, start, length):
|
||||
@@ -421,7 +419,7 @@ def solve_shift_scheduling(params, output_proto):
|
||||
|
||||
|
||||
def main(_):
|
||||
solve_shift_scheduling(FLAGS.params, FLAGS.output_proto)
|
||||
solve_shift_scheduling(_PARAMS.value, _OUTPUT_PROTO.value)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -24,12 +24,10 @@ from absl import flags
|
||||
from ortools.linear_solver import pywraplp
|
||||
from ortools.sat.python import cp_model
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
|
||||
flags.DEFINE_integer('problem', 2, 'Problem id to solve.')
|
||||
flags.DEFINE_bool('break_symmetries', True,
|
||||
'Break symmetries between equivalent orders.')
|
||||
flags.DEFINE_string(
|
||||
_PROBLEM = flags.DEFINE_integer('problem', 2, 'Problem id to solve.')
|
||||
_BREAK_SYMMETRIES = flags.DEFINE_boolean(
|
||||
'break_symmetries', True, 'Break symmetries between equivalent orders.')
|
||||
_SOLVER = flags.DEFINE_string(
|
||||
'solver', 'mip_column', 'Method used to solve: sat, sat_table, sat_column, '
|
||||
'mip_column.')
|
||||
|
||||
@@ -737,15 +735,16 @@ def steel_mill_slab_with_mip_column_generation(problem):
|
||||
print('No solution')
|
||||
|
||||
|
||||
def main(_=None):
|
||||
if FLAGS.solver == 'sat':
|
||||
steel_mill_slab(FLAGS.problem, FLAGS.break_symmetries)
|
||||
elif FLAGS.solver == 'sat_table':
|
||||
steel_mill_slab_with_valid_slabs(FLAGS.problem, FLAGS.break_symmetries)
|
||||
elif FLAGS.solver == 'sat_column':
|
||||
steel_mill_slab_with_column_generation(FLAGS.problem)
|
||||
def main(_):
|
||||
if _SOLVER.value == 'sat':
|
||||
steel_mill_slab(_PROBLEM.value, _BREAK_SYMMETRIES.value)
|
||||
elif _SOLVER.value == 'sat_table':
|
||||
steel_mill_slab_with_valid_slabs(_PROBLEM.value,
|
||||
_BREAK_SYMMETRIES.value)
|
||||
elif _SOLVER.value == 'sat_column':
|
||||
steel_mill_slab_with_column_generation(_PROBLEM.value)
|
||||
else: # 'mip_column'
|
||||
steel_mill_slab_with_mip_column_generation(FLAGS.problem)
|
||||
steel_mill_slab_with_mip_column_generation(_PROBLEM.value)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user