Download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*******************************************************************************
 * OscaR is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *  
 * OscaR is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License  for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public License along with OscaR.
 ******************************************************************************/
package oscar.examples.cp.hakank
 
import oscar.cp.modeling._
 
import oscar.cp.core._
import scala.io.Source._
import scala.math._
 
/*
 
  Set partition problem in Oscar.
 
  Problem formulation from
  """
  This is a partition problem.
  Given the set S = {1, 2, ..., n},
  it consists in finding two sets A and B such that:
 
   A U B = S,
   |A| = |B|,
   sum(A) = sum(B),
   sum_squares(A) = sum_squares(B)
  """
 
  Note: This model uses a binary matrix to represent the sets.
   
 
  @author Hakan Kjellerstrand hakank@gmail.com
  
*/
 
object SetPartition {
 
  def main(args: Array[String]) {
 
    val cp = CPSolver()
 
    //
    // data
    //
    val n        = if (args.length > 0) args(0).toInt else 16;
    val num_sets = if (args.length > 1) args(1).toInt else 2;
 
    val NRANGE = 0 until n
    val SRANGE = 0 until num_sets
 
    println("n: " + n + " num_sets: " + num_sets)
 
    //
    // variables
    //
    // The matrix
    val a = Array.fill(num_sets,n)(CPIntVar(0 to 1)(cp))
 
 
    //
    // constraints
    //
    var numSols = 0
 
    cp.solve subjectTo {
     
      for(i <- SRANGE;
          j <- SRANGE if i!=j) {
          cp.add(
                 sum(for{k <- NRANGE} yield a(i)(k)*a(j)(k)) == 0
                 )
      }
 
      // ensure that all integers is in
      // (exactly) one partition
      cp.add(
             sum(
                 for{i <- SRANGE
                     j <- NRANGE} yield a(i)(j)
                 ) == n
             )
 
       
 
      for(i <- SRANGE; j <- SRANGE if i < j) {
        // same cardinality
        cp.add(
               sum(for{k <- NRANGE} yield a(i)(k) )
               ==
               sum(for{k <- NRANGE} yield a(j)(k) )
               )
 
        // same sum
        cp.add(
               sum(for{k <- NRANGE} yield a(i)(k)*k )
               ==
               sum(for{k <- NRANGE} yield a(j)(k)*k)
               )
 
 
        // same sum squared
        cp.add(
               sum(for{k <- NRANGE} yield a(i)(k)*k*a(i)(k)*k )
               ==
               sum(for{k <- NRANGE} yield a(j)(k)*k*a(j)(k)*k)
               )
      }
 
      // symmetry breaking for num_sets == 2
      if (num_sets == 2) {
        cp.add(a(0)(0) == 1)
      }
 
 
    } search {
        
      binaryStatic(a.flatten.toSeq)
    } onSolution {
      println("\nSolution:")
      var sums = 0
      var sums_squared = 0
      for(i <- SRANGE) {
        for(j <- NRANGE if a(i)(j).value == 1) {
          print((j+1) + " ")
          if (i == 0) {
            val v = (j+1)*a(i)(j).value
            sums += v
            sums_squared += v*v
          }
        }
        println()
      }
 
      println("Sums: " + sums  + " Sums squared: " + sums_squared)
 
      numSols += 1
 
    }
    println(cp.start())
 
 
  }
 
}