Back to Or Tools

magic_sequence_sat

examples/notebook/contrib/magic_sequence_sat.ipynb

2016-062.1 KB
Original Source
Copyright 2025 Google LLC.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, 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.

magic_sequence_sat

<table align="left"> <td> <a href="https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/magic_sequence_sat.ipynb">Run in Google Colab</a> </td> <td> <a href="https://github.com/google/or-tools/blob/main/examples/contrib/magic_sequence_sat.py">View source on GitHub</a> </td> </table>

First, you must install ortools package in this colab.

python
%pip install ortools

Solve the magic sequence problem with the CP-SAT solver.

python
from ortools.sat.python import cp_model


def main():
  """Magic sequence problem."""
  n = 100
  values = range(n)

  model = cp_model.CpModel()

  x = [model.NewIntVar(0, n, 'x%i' % i) for i in values]

  for k in values:
    tmp_array = []
    for i in values:
      tmp_var = model.NewBoolVar('')
      model.Add(x[i] == k).OnlyEnforceIf(tmp_var)
      model.Add(x[i] != k).OnlyEnforceIf(tmp_var.Not())
      tmp_array.append(tmp_var)
    model.Add(sum(tmp_array) == x[k])

  # Redundant constraint.
  model.Add(sum(x) == n)

  solver = cp_model.CpSolver()
  # No solution printer, this problem has only 1 solution.
  solver.parameters.log_search_progress = True
  solver.Solve(model)
  print(solver.ResponseStats())
  for k in values:
    print('x[%i] = %i ' % (k, solver.Value(x[k])), end='')
  print()


main()