Skip to main content

Constraint Programming Solver SCOP

Project description

SCOP (SOlver for Constraint Programming)

How to Install to Jupyter Notebook (Lab) and/or Google Colaboratory

pip install scop

How to use

See https://mikiokubo.github.io/scoptrial/ and https://www.logopt.com/scop2/

Here is an example.

"""
Example 1 (Assignment Problem):
Three jobs (0,1,2) must be assigned to three workers (A,B,C)
so that each job is assigned to exactly one worker.
The cost matrix is represented by the list of lists
Cost=[[15, 20, 30],
      [7, 15, 12],
      [25,10,13]],
where rows of the matrix are workers, and columns are jobs.
Find the minimum cost assignment of workers to jobs.
"""
from scop import Alldiff, Linear, Model

workers = ["A", "B", "C"]
Jobs = [0, 1, 2]
Cost = {
    ("A", 0): 15,
    ("A", 1): 20,
    ("A", 2): 30,
    ("B", 0): 7,
    ("B", 1): 15,
    ("B", 2): 12,
    ("C", 0): 25,
    ("C", 1): 10,
    ("C", 2): 13,
}

m = Model()
x = {}
for i in workers:
    x[i] = m.addVariable(name=i, domain=Jobs)

x_list = []
for i in x:
    x_list.append(x[i])

con1 = Alldiff("AD", x_list, weight="inf")

con2 = Linear("linear_constraint", weight=1, rhs=0, direction="<=")
for i in workers:
    for j in Jobs:
        con2.addTerms(Cost[i, j], x[i], j)

m.addConstraint(con1)
m.addConstraint(con2)

print(m)

m.Params.TimeLimit = 1
sol, violated = m.optimize()

if m.Status == 0:
    print("solution")
    for x in sol:
        print(x, sol[x])
    print("violated constraint(s)")
    for v in violated:
        print(v, violated[v])
Model: 
number of variables = 3  
number of constraints= 2  
variable A:['0', '1', '2'] = None 
variable B:['0', '1', '2'] = None 
variable C:['0', '1', '2'] = None 
AD: weight= inf type=alldiff  A B C ;  :LHS =0  
linear_constraint: weight= 1 type=linear 15(A,0) 20(A,1) 30(A,2) 7(B,0) 15(B,1) 12(B,2) 25(C,0) 10(C,1) 13(C,2) <=0 :LHS =0 

 ================ Now solving the problem ================ 

solution
A 0
B 2
C 1
violated constraint(s)
linear_constraint 37

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scop-0.1.0-py3-none-any.whl (324.8 kB view details)

Uploaded Python 3

File details

Details for the file scop-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: scop-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 324.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.5

File hashes

Hashes for scop-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6554f62dbc5071f72f83de5674593520ff468f596d50ab6919ba48f269a63c54
MD5 2ea75085d6da41eb8630e2dc015324f1
BLAKE2b-256 567dbaee351e3580ac14bc4edae26615f7bd03ee11217c0a3362c7ed1b1c06ef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page