PySCIPOpt  4.3.0
Python Interface for the SCIP Optimization Suite
ssp.py
Go to the documentation of this file.
1 
3 """
4 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
5 """
6 
7 from pyscipopt import Model, quicksum, multidict
8 
9 def ssp(V,E):
10  """ssp -- model for the stable set problem
11  Parameters:
12  - V: set/list of nodes in the graph
13  - E: set/list of edges in the graph
14  Returns a model, ready to be solved.
15  """
16  model = Model("ssp")
17 
18  x = {}
19  for i in V:
20  x[i] = model.addVar(vtype="B", name="x(%s)"%i)
21 
22  for (i,j) in E:
23  model.addCons(x[i] + x[j] <= 1, "Edge(%s,%s)"%(i,j))
24 
25  model.setObjective(quicksum(x[i] for i in V), "maximize")
26 
27  model.data = x
28  return model
29 
30 
31 import random
32 def make_data(n,prob):
33  """make_data: prepare data for a random graph
34  Parameters:
35  - n: number of vertices
36  - prob: probability of existence of an edge, for each pair of vertices
37  Returns a tuple with a list of vertices and a list edges.
38  """
39  V = range(1,n+1)
40  E = [(i,j) for i in V for j in V if i < j and random.random() < prob]
41  return V,E
42 
43 
44 if __name__ == "__main__":
45  random.seed(1)
46  V,E = make_data(100,.5)
47 
48  model = ssp(V,E)
49  model.optimize()
50  print("Optimal value:", model.getObjVal())
51 
52  x = model.data
53  print("Maximum stable set:")
54  print([i for i in V if model.getVal(x[i]) > 0.5])
pyscipopt.expr.quicksum
def quicksum(termlist)
Definition: expr.pxi:357
ssp.ssp
def ssp(V, E)
Definition: ssp.py:9
ssp
Definition: ssp.py:1
ssp.make_data
def make_data(n, prob)
Definition: ssp.py:32