python f-string
This commit is contained in:
@@ -36,13 +36,13 @@ def BinpackingProblemSat():
|
||||
for i in all_items:
|
||||
num_copies = items[i][1]
|
||||
for b in all_bins:
|
||||
x[(i, b)] = model.NewIntVar(0, num_copies, 'x_%i_%i' % (i, b))
|
||||
x[(i, b)] = model.NewIntVar(0, num_copies, f'x[{i},{b}]')
|
||||
|
||||
# Load variables.
|
||||
load = [model.NewIntVar(0, bin_capacity, 'load_%i' % b) for b in all_bins]
|
||||
load = [model.NewIntVar(0, bin_capacity, f'load[{b}]') for b in all_bins]
|
||||
|
||||
# Slack variables.
|
||||
slacks = [model.NewBoolVar('slack_%i' % b) for b in all_bins]
|
||||
slacks = [model.NewBoolVar(f'slack[{b}]') for b in all_bins]
|
||||
|
||||
# Links load and x.
|
||||
for b in all_bins:
|
||||
@@ -66,13 +66,13 @@ def BinpackingProblemSat():
|
||||
# Solves and prints out the solution.
|
||||
solver = cp_model.CpSolver()
|
||||
status = solver.Solve(model)
|
||||
print('Solve status: %s' % solver.StatusName(status))
|
||||
print(f'Solve status: {solver.StatusName(status)}')
|
||||
if status == cp_model.OPTIMAL:
|
||||
print('Optimal objective value: %i' % solver.ObjectiveValue())
|
||||
print(f'Optimal objective value: {solver.ObjectiveValue()}')
|
||||
print('Statistics')
|
||||
print(' - conflicts : %i' % solver.NumConflicts())
|
||||
print(' - branches : %i' % solver.NumBranches())
|
||||
print(' - wall time : %f s' % solver.WallTime())
|
||||
print(f' - conflicts : {solver.NumConflicts()}')
|
||||
print(f' - branches : {solver.NumBranches()}')
|
||||
print(f' - wall time : {solver.WallTime()}s')
|
||||
|
||||
|
||||
BinpackingProblemSat()
|
||||
|
||||
@@ -27,7 +27,7 @@ class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
|
||||
def on_solution_callback(self):
|
||||
self.__solution_count += 1
|
||||
for v in self.__variables:
|
||||
print('%s=%i' % (v, self.Value(v)), end=' ')
|
||||
print(f'{v}={self.Value(v)}', end=' ')
|
||||
print()
|
||||
|
||||
def solution_count(self):
|
||||
|
||||
@@ -37,7 +37,7 @@ class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
|
||||
def on_solution_callback(self):
|
||||
self.__solution_count += 1
|
||||
for v in self.__variables:
|
||||
print('%s=%i' % (v, self.Value(v)), end=' ')
|
||||
print(f'{v}={self.Value(v)}', end=' ')
|
||||
print()
|
||||
|
||||
def solution_count(self):
|
||||
|
||||
@@ -27,7 +27,7 @@ class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
|
||||
def on_solution_callback(self):
|
||||
self.__solution_count += 1
|
||||
for v in self.__variables:
|
||||
print('%s=%i' % (v, self.Value(v)), end=' ')
|
||||
print(f'{v}={self.Value(v)}', end=' ')
|
||||
print()
|
||||
|
||||
def solution_count(self):
|
||||
|
||||
@@ -54,9 +54,8 @@ def main():
|
||||
|
||||
for job_id, job in enumerate(jobs_data):
|
||||
for task_id, task in enumerate(job):
|
||||
machine = task[0]
|
||||
duration = task[1]
|
||||
suffix = '_%i_%i' % (job_id, task_id)
|
||||
machine, duration = task
|
||||
suffix = f'_{job_id}_{task_id}'
|
||||
start_var = model.NewIntVar(0, horizon, 'start' + suffix)
|
||||
end_var = model.NewIntVar(0, horizon, 'end' + suffix)
|
||||
interval_var = model.NewIntervalVar(start_var, duration, end_var,
|
||||
@@ -119,16 +118,15 @@ def main():
|
||||
sol_line = ' '
|
||||
|
||||
for assigned_task in assigned_jobs[machine]:
|
||||
name = 'job_%i_task_%i' % (assigned_task.job,
|
||||
assigned_task.index)
|
||||
name = f'job_{assigned_task.job}_task_{assigned_task.index}'
|
||||
# Add spaces to output to align columns.
|
||||
sol_line_tasks += '%-15s' % name
|
||||
sol_line_tasks += f'{name:15}'
|
||||
|
||||
start = assigned_task.start
|
||||
duration = assigned_task.duration
|
||||
sol_tmp = '[%i,%i]' % (start, start + duration)
|
||||
sol_tmp = f'[{start},{start + duration}]'
|
||||
# Add spaces to output to align columns.
|
||||
sol_line += '%-15s' % sol_tmp
|
||||
sol_line += f'{sol_tmp:15}'
|
||||
|
||||
sol_line += '\n'
|
||||
sol_line_tasks += '\n'
|
||||
@@ -145,9 +143,9 @@ def main():
|
||||
# Statistics.
|
||||
# [START statistics]
|
||||
print('\nStatistics')
|
||||
print(' - conflicts: %i' % solver.NumConflicts())
|
||||
print(' - branches : %i' % solver.NumBranches())
|
||||
print(' - wall time: %f s' % solver.WallTime())
|
||||
print(f' - conflicts: {solver.NumConflicts()}')
|
||||
print(f' - branches : {solver.NumBranches()}')
|
||||
print(f' - wall time: {solver.WallTime()}s')
|
||||
# [END statistics]
|
||||
|
||||
|
||||
|
||||
@@ -58,12 +58,12 @@ def NoOverlapSampleSat():
|
||||
|
||||
if status == cp_model.OPTIMAL:
|
||||
# Print out makespan and the start times for all tasks.
|
||||
print('Optimal Schedule Length: %i' % solver.ObjectiveValue())
|
||||
print('Task 0 starts at %i' % solver.Value(start_0))
|
||||
print('Task 1 starts at %i' % solver.Value(start_1))
|
||||
print('Task 2 starts at %i' % solver.Value(start_2))
|
||||
print(f'Optimal Schedule Length: {solver.ObjectiveValue()}')
|
||||
print(f'Task 0 starts at {solver.Value(start_0)}')
|
||||
print(f'Task 1 starts at {solver.Value(start_1)}')
|
||||
print(f'Task 2 starts at {solver.Value(start_2)}')
|
||||
else:
|
||||
print('Solver exited with nonoptimal status: %i' % status)
|
||||
print(f'Solver exited with nonoptimal status: {status}')
|
||||
|
||||
|
||||
NoOverlapSampleSat()
|
||||
|
||||
@@ -40,9 +40,9 @@ def non_linear_sat():
|
||||
status = solver.Solve(model)
|
||||
|
||||
if status == cp_model.OPTIMAL or status == cp_model.FEASIBLE:
|
||||
print('x = %i' % solver.Value(x))
|
||||
print('y = %i' % solver.Value(y))
|
||||
print('s = %i' % solver.Value(area))
|
||||
print(f'x = {solver.Value(x)}')
|
||||
print(f'y = {solver.Value(y)}')
|
||||
print(f's = {solver.Value(area)}')
|
||||
else:
|
||||
print('No solution found.')
|
||||
|
||||
|
||||
@@ -36,8 +36,8 @@ class NQueenSolutionPrinter(cp_model.CpSolverSolutionCallback):
|
||||
|
||||
def on_solution_callback(self):
|
||||
current_time = time.time()
|
||||
print('Solution %i, time = %f s' %
|
||||
(self.__solution_count, current_time - self.__start_time))
|
||||
print(f'Solution {self.__solution_count}, '
|
||||
f'time = {current_time - self.__start_time} s')
|
||||
self.__solution_count += 1
|
||||
|
||||
all_queens = range(len(self.__queens))
|
||||
@@ -65,7 +65,7 @@ def main(board_size):
|
||||
# There are `board_size` number of variables, one for a queen in each column
|
||||
# of the board. The value of each variable is the row that the queen is in.
|
||||
queens = [
|
||||
model.NewIntVar(0, board_size - 1, 'x%i' % i) for i in range(board_size)
|
||||
model.NewIntVar(0, board_size - 1, f'x_{i}') for i in range(board_size)
|
||||
]
|
||||
# [END variables]
|
||||
|
||||
|
||||
@@ -42,8 +42,7 @@ def main():
|
||||
for n in all_nurses:
|
||||
for d in all_days:
|
||||
for s in all_shifts:
|
||||
shifts[(n, d,
|
||||
s)] = model.NewBoolVar('shift_n%id%is%i' % (n, d, s))
|
||||
shifts[(n, d, s)] = model.NewBoolVar(f'shift_n{n}_d{d}_s{s}')
|
||||
# [END variables]
|
||||
|
||||
# Each shift is assigned to exactly one nurse in the schedule period.
|
||||
@@ -103,19 +102,19 @@ def main():
|
||||
|
||||
def on_solution_callback(self):
|
||||
self._solution_count += 1
|
||||
print('Solution %i' % self._solution_count)
|
||||
print(f'Solution {self._solution_count}')
|
||||
for d in range(self._num_days):
|
||||
print('Day %i' % d)
|
||||
print(f'Day {d}')
|
||||
for n in range(self._num_nurses):
|
||||
is_working = False
|
||||
for s in range(self._num_shifts):
|
||||
if self.Value(self._shifts[(n, d, s)]):
|
||||
is_working = True
|
||||
print(' Nurse %i works shift %i' % (n, s))
|
||||
print(f' Nurse {n} works shift {s}')
|
||||
if not is_working:
|
||||
print(' Nurse {} does not work'.format(n))
|
||||
print(f' Nurse {n} does not work')
|
||||
if self._solution_count >= self._solution_limit:
|
||||
print('Stop search after %i solutions' % self._solution_limit)
|
||||
print(f'Stop search after {self._solution_limit} solutions')
|
||||
self.StopSearch()
|
||||
|
||||
def solution_count(self):
|
||||
@@ -135,10 +134,10 @@ def main():
|
||||
# Statistics.
|
||||
# [START statistics]
|
||||
print('\nStatistics')
|
||||
print(' - conflicts : %i' % solver.NumConflicts())
|
||||
print(' - branches : %i' % solver.NumBranches())
|
||||
print(' - wall time : %f s' % solver.WallTime())
|
||||
print(' - solutions found: %i' % solution_printer.solution_count())
|
||||
print(f' - conflicts : {solver.NumConflicts()}')
|
||||
print(f' - branches : {solver.NumBranches()}')
|
||||
print(f' - wall time : {solver.WallTime()} s')
|
||||
print(f' - solutions found: {solution_printer.solution_count()}')
|
||||
# [END statistics]
|
||||
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
|
||||
def on_solution_callback(self):
|
||||
self.__solution_count += 1
|
||||
for v in self.__variables:
|
||||
print('%s=%i' % (v, self.Value(v)), end=' ')
|
||||
print(f'{v}={self.Value(v)}', end=' ')
|
||||
print()
|
||||
|
||||
def solution_count(self):
|
||||
|
||||
@@ -33,8 +33,7 @@ def RabbitsAndPheasantsSat():
|
||||
status = solver.Solve(model)
|
||||
|
||||
if status == cp_model.OPTIMAL:
|
||||
print('%i rabbits and %i pheasants' %
|
||||
(solver.Value(r), solver.Value(p)))
|
||||
print(f'{solver.Value(r)} rabbits and {solver.Value(p)} pheasants')
|
||||
|
||||
|
||||
RabbitsAndPheasantsSat()
|
||||
|
||||
@@ -40,7 +40,7 @@ def RankTasks(model, starts, presences, ranks):
|
||||
if i == j:
|
||||
precedences[(i, j)] = presences[i]
|
||||
else:
|
||||
prec = model.NewBoolVar('%i before %i' % (i, j))
|
||||
prec = model.NewBoolVar(f'{i} before {j}')
|
||||
precedences[(i, j)] = prec
|
||||
model.Add(starts[i] < starts[j]).OnlyEnforceIf(prec)
|
||||
|
||||
@@ -92,25 +92,25 @@ def RankingSampleSat():
|
||||
|
||||
# Creates intervals, half of them are optional.
|
||||
for t in all_tasks:
|
||||
start = model.NewIntVar(0, horizon, 'start_%i' % t)
|
||||
start = model.NewIntVar(0, horizon, f'start[{t}]')
|
||||
duration = t + 1
|
||||
end = model.NewIntVar(0, horizon, 'end_%i' % t)
|
||||
end = model.NewIntVar(0, horizon, f'end[{t}]')
|
||||
if t < num_tasks // 2:
|
||||
interval = model.NewIntervalVar(start, duration, end,
|
||||
'interval_%i' % t)
|
||||
f'interval[{t}]')
|
||||
presence = True
|
||||
else:
|
||||
presence = model.NewBoolVar('presence_%i' % t)
|
||||
presence = model.NewBoolVar(f'presence[{t}]')
|
||||
interval = model.NewOptionalIntervalVar(start, duration, end,
|
||||
presence,
|
||||
'o_interval_%i' % t)
|
||||
f'o_interval[{t}]')
|
||||
starts.append(start)
|
||||
ends.append(end)
|
||||
intervals.append(interval)
|
||||
presences.append(presence)
|
||||
|
||||
# Ranks = -1 if and only if the tasks is not performed.
|
||||
ranks.append(model.NewIntVar(-1, num_tasks - 1, 'rank_%i' % t))
|
||||
ranks.append(model.NewIntVar(-1, num_tasks - 1, f'rank[{t}]'))
|
||||
|
||||
# Adds NoOverlap constraint.
|
||||
model.AddNoOverlap(intervals)
|
||||
@@ -137,17 +137,17 @@ def RankingSampleSat():
|
||||
|
||||
if status == cp_model.OPTIMAL:
|
||||
# Prints out the makespan and the start times and ranks of all tasks.
|
||||
print('Optimal cost: %i' % solver.ObjectiveValue())
|
||||
print('Makespan: %i' % solver.Value(makespan))
|
||||
print(f'Optimal cost: {solver.ObjectiveValue()}')
|
||||
print(f'Makespan: {solver.Value(makespan)}')
|
||||
for t in all_tasks:
|
||||
if solver.Value(presences[t]):
|
||||
print('Task %i starts at %i with rank %i' %
|
||||
(t, solver.Value(starts[t]), solver.Value(ranks[t])))
|
||||
print(f'Task {t} starts at {solver.Value(starts[t])} '
|
||||
f'with rank {solver.Value(ranks[t])}')
|
||||
else:
|
||||
print('Task %i in not performed and ranked at %i' %
|
||||
(t, solver.Value(ranks[t])))
|
||||
print(f'Task {t} in not performed '
|
||||
f'and ranked at {solver.Value(ranks[t])}')
|
||||
else:
|
||||
print('Solver exited with nonoptimal status: %i' % status)
|
||||
print(f'Solver exited with nonoptimal status: {status}')
|
||||
|
||||
|
||||
RankingSampleSat()
|
||||
|
||||
@@ -55,8 +55,7 @@ def main():
|
||||
for n in all_nurses:
|
||||
for d in all_days:
|
||||
for s in all_shifts:
|
||||
shifts[(n, d,
|
||||
s)] = model.NewBoolVar('shift_n%id%is%i' % (n, d, s))
|
||||
shifts[(n, d, s)] = model.NewBoolVar(f'shift_n{n}_d{d}_s{s}')
|
||||
# [END variables]
|
||||
|
||||
# Each shift is assigned to exactly one nurse in .
|
||||
@@ -128,9 +127,9 @@ def main():
|
||||
# Statistics.
|
||||
# [START statistics]
|
||||
print('\nStatistics')
|
||||
print(' - conflicts: %i' % solver.NumConflicts())
|
||||
print(' - branches : %i' % solver.NumBranches())
|
||||
print(' - wall time: %f s' % solver.WallTime())
|
||||
print(f' - conflicts: {solver.NumConflicts()}')
|
||||
print(f' - branches : {solver.NumBranches()}')
|
||||
print(f' - wall time: {solver.WallTime()}s')
|
||||
# [END statistics]
|
||||
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
|
||||
def on_solution_callback(self):
|
||||
self.__solution_count += 1
|
||||
for v in self.__variables:
|
||||
print('%s=%i' % (v, self.Value(v)), end=' ')
|
||||
print(f'{v}={self.Value(v)}', end=' ')
|
||||
print()
|
||||
|
||||
def solution_count(self):
|
||||
|
||||
@@ -29,7 +29,7 @@ class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
|
||||
def on_solution_callback(self):
|
||||
self.__solution_count += 1
|
||||
for v in self.__variables:
|
||||
print('%s=%i' % (v, self.Value(v)), end=' ')
|
||||
print(f'{v}={self.Value(v)}', end=' ')
|
||||
print()
|
||||
|
||||
def solution_count(self):
|
||||
@@ -67,8 +67,8 @@ def SearchForAllSolutionsSampleSat():
|
||||
status = solver.Solve(model, solution_printer)
|
||||
# [END solve]
|
||||
|
||||
print('Status = %s' % solver.StatusName(status))
|
||||
print('Number of solutions found: %i' % solution_printer.solution_count())
|
||||
print(f'Status = {solver.StatusName(status)}')
|
||||
print(f'Number of solutions found: {solution_printer.solution_count()}')
|
||||
|
||||
|
||||
SearchForAllSolutionsSampleSat()
|
||||
|
||||
@@ -47,9 +47,9 @@ def SimpleSatProgram():
|
||||
|
||||
# [START print_solution]
|
||||
if status == cp_model.OPTIMAL or status == cp_model.FEASIBLE:
|
||||
print('x = %i' % solver.Value(x))
|
||||
print('y = %i' % solver.Value(y))
|
||||
print('z = %i' % solver.Value(z))
|
||||
print(f'x = {solver.Value(x)}')
|
||||
print(f'y = {solver.Value(y)}')
|
||||
print(f'z = {solver.Value(z)}')
|
||||
else:
|
||||
print('No solution found.')
|
||||
# [END print_solution]
|
||||
|
||||
@@ -52,8 +52,8 @@ def SolutionHintingSampleSat():
|
||||
status = solver.Solve(model, solution_printer)
|
||||
# [END solve]
|
||||
|
||||
print('Status = %s' % solver.StatusName(status))
|
||||
print('Number of solutions found: %i' % solution_printer.solution_count())
|
||||
print(f'Status = {solver.StatusName(status)}')
|
||||
print(f'Number of solutions found: {solution_printer.solution_count()}')
|
||||
|
||||
|
||||
SolutionHintingSampleSat()
|
||||
|
||||
@@ -28,10 +28,10 @@ class VarArrayAndObjectiveSolutionPrinter(cp_model.CpSolverSolutionCallback):
|
||||
self.__solution_count = 0
|
||||
|
||||
def on_solution_callback(self):
|
||||
print('Solution %i' % self.__solution_count)
|
||||
print(' objective value = %i' % self.ObjectiveValue())
|
||||
print(f'Solution {self.__solution_count}')
|
||||
print(f' objective value = {self.ObjectiveValue()}')
|
||||
for v in self.__variables:
|
||||
print(' %s = %i' % (v, self.Value(v)), end=' ')
|
||||
print(f' {v}={self.Value(v)}', end=' ')
|
||||
print()
|
||||
self.__solution_count += 1
|
||||
|
||||
@@ -71,8 +71,8 @@ def SolveAndPrintIntermediateSolutionsSampleSat():
|
||||
status = solver.Solve(model, solution_printer)
|
||||
# [END solve]
|
||||
|
||||
print('Status = %s' % solver.StatusName(status))
|
||||
print('Number of solutions found: %i' % solution_printer.solution_count())
|
||||
print(f'Status = {solver.StatusName(status)}')
|
||||
print(f'Number of solutions found: {solution_printer.solution_count()}')
|
||||
|
||||
|
||||
SolveAndPrintIntermediateSolutionsSampleSat()
|
||||
|
||||
@@ -39,9 +39,9 @@ def SolveWithTimeLimitSampleSat():
|
||||
status = solver.Solve(model)
|
||||
|
||||
if status == cp_model.OPTIMAL:
|
||||
print('x = %i' % solver.Value(x))
|
||||
print('y = %i' % solver.Value(y))
|
||||
print('z = %i' % solver.Value(z))
|
||||
print(f'x = {solver.Value(x)}')
|
||||
print(f'y = {solver.Value(y)}')
|
||||
print(f'z = {solver.Value(z)}')
|
||||
|
||||
|
||||
SolveWithTimeLimitSampleSat()
|
||||
|
||||
@@ -27,7 +27,7 @@ class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
|
||||
def on_solution_callback(self):
|
||||
self.__solution_count += 1
|
||||
for v in self.__variables:
|
||||
print('%s=%i' % (v, self.Value(v)), end=' ')
|
||||
print(f'{v}={self.Value(v)}', end=' ')
|
||||
print()
|
||||
|
||||
def solution_count(self):
|
||||
|
||||
@@ -30,10 +30,10 @@ class VarArraySolutionPrinterWithLimit(cp_model.CpSolverSolutionCallback):
|
||||
def on_solution_callback(self):
|
||||
self.__solution_count += 1
|
||||
for v in self.__variables:
|
||||
print('%s=%i' % (v, self.Value(v)), end=' ')
|
||||
print(f'{v}={self.Value(v)}', end=' ')
|
||||
print()
|
||||
if self.__solution_count >= self.__solution_limit:
|
||||
print('Stop search after %i solutions' % self.__solution_limit)
|
||||
print(f'Stop search after {self.__solution_limit} solutions')
|
||||
self.StopSearch()
|
||||
|
||||
def solution_count(self):
|
||||
@@ -57,8 +57,8 @@ def StopAfterNSolutionsSampleSat():
|
||||
solver.parameters.enumerate_all_solutions = True
|
||||
# Solve.
|
||||
status = solver.Solve(model, solution_printer)
|
||||
print('Status = %s' % solver.StatusName(status))
|
||||
print('Number of solutions found: %i' % solution_printer.solution_count())
|
||||
print(f'Status = {solver.StatusName(status)}')
|
||||
print(f'Number of solutions found: {solution_printer.solution_count()}')
|
||||
assert solution_printer.solution_count() == 5
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user