PySCIPOpt  5.1.1
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
8 
9 
10 def ssp(V, E):
11  """ssp -- model for the stable set problem
12  Parameters:
13  - V: set/list of nodes in the graph
14  - E: set/list of edges in the graph
15  Returns a model, ready to be solved.
16  """
17  model = Model("ssp")
18 
19  x = {}
20  for i in V:
21  x[i] = model.addVar(vtype="B", name="x(%s)" % i)
22 
23  for (i, j) in E:
24  model.addCons(x[i] + x[j] <= 1, "Edge(%s,%s)" % (i, j))
25 
26  model.setObjective(quicksum(x[i] for i in V), "maximize")
27 
28  model.data = x
29  return model
30 
31 
32 import random
33 
34 
35 def make_data(n, prob):
36  """make_data: prepare data for a random graph
37  Parameters:
38  - n: number of vertices
39  - prob: probability of existence of an edge, for each pair of vertices
40  Returns a tuple with a list of vertices and a list edges.
41  """
42  V = range(1, n + 1)
43  E = [(i, j) for i in V for j in V if i < j and random.random() < prob]
44  return V, E
45 
46 
47 if __name__ == "__main__":
48  random.seed(1)
49  V, E = make_data(100, .5)
50 
51  model = ssp(V, E)
52  model.optimize()
53  print("Optimal value:", model.getObjVal())
54 
55  x = model.data
56  print("Maximum stable set:")
57  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:10
ssp
Definition: ssp.py:1
ssp.make_data
def make_data(n, prob)
Definition: ssp.py:35