or-tools 0.1.3 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ module ORTools
2
+ class BoolVar
3
+ include ComparisonOperators
4
+
5
+ def *(other)
6
+ SatLinearExpr.new([[self, other]])
7
+ end
8
+ end
9
+ end
@@ -12,11 +12,20 @@ module ORTools
12
12
  end
13
13
 
14
14
  def value(var)
15
- _solution_integer_value(@response, var)
15
+ if var.is_a?(BoolVar)
16
+ _solution_boolean_value(@response, var)
17
+ else
18
+ _solution_integer_value(@response, var)
19
+ end
20
+ end
21
+
22
+ def solve_with_solution_callback(model, observer)
23
+ @response = _solve_with_observer(model, observer, false)
24
+ @response.status
16
25
  end
17
26
 
18
27
  def search_for_all_solutions(model, observer)
19
- @response = _solve_with_observer(model, observer)
28
+ @response = _solve_with_observer(model, observer, true)
20
29
  @response.status
21
30
  end
22
31
  end
@@ -12,5 +12,9 @@ module ORTools
12
12
  raise "Unsupported type"
13
13
  end
14
14
  end
15
+
16
+ def objective_value
17
+ @response.objective_value
18
+ end
15
19
  end
16
20
  end
@@ -8,6 +8,14 @@ module ORTools
8
8
  end
9
9
  end
10
10
 
11
+ def >=(other)
12
+ if other.is_a?(LinearExpr)
13
+ _gte_linear_expr(other)
14
+ else
15
+ _gte_double(other)
16
+ end
17
+ end
18
+
11
19
  def <=(other)
12
20
  if other.is_a?(LinearExpr)
13
21
  _lte_linear_expr(other)
@@ -0,0 +1,115 @@
1
+ module ORTools
2
+ class Seating
3
+ attr_reader :assignments, :people, :total_weight
4
+
5
+ def initialize(connections:, tables:, min_connections: 1)
6
+ @people = connections.flat_map { |c| c[:people] }.uniq
7
+
8
+ @connections_for = {}
9
+ @people.each do |person|
10
+ @connections_for[person] = {}
11
+ end
12
+ connections.each do |c|
13
+ c[:people].each_with_index do |person, i|
14
+ others = c[:people].dup
15
+ others.delete_at(i)
16
+ others.each do |other|
17
+ @connections_for[person][other] ||= 0
18
+ @connections_for[person][other] += c[:weight]
19
+ end
20
+ end
21
+ end
22
+
23
+ model = ORTools::CpModel.new
24
+ all_tables = tables.size.times.to_a
25
+
26
+ # decision variables
27
+ seats = {}
28
+ all_tables.each do |t|
29
+ people.each do |g|
30
+ seats[[t, g]] = model.new_bool_var("guest %s seats on table %i" % [g, t])
31
+ end
32
+ end
33
+
34
+ pairs = people.combination(2)
35
+
36
+ colocated = {}
37
+ pairs.each do |g1, g2|
38
+ colocated[[g1, g2]] = model.new_bool_var("guest %s seats with guest %s" % [g1, g2])
39
+ end
40
+
41
+ same_table = {}
42
+ pairs.each do |g1, g2|
43
+ all_tables.each do |t|
44
+ same_table[[g1, g2, t]] = model.new_bool_var("guest %s seats with guest %s on table %i" % [g1, g2, t])
45
+ end
46
+ end
47
+
48
+ # objective
49
+ objective = []
50
+ pairs.each do |g1, g2|
51
+ weight = @connections_for[g1][g2]
52
+ objective << colocated[[g1, g2]] * weight if weight
53
+ end
54
+ model.maximize(model.sum(objective))
55
+
56
+ # everybody seats at one table
57
+ people.each do |g|
58
+ model.add(model.sum(all_tables.map { |t| seats[[t, g]] }) == 1)
59
+ end
60
+
61
+ # tables have a max capacity
62
+ all_tables.each do |t|
63
+ model.add(model.sum(@people.map { |g| seats[[t, g]] }) <= tables[t])
64
+ end
65
+
66
+ # link colocated with seats
67
+ pairs.each do |g1, g2|
68
+ all_tables.each do |t|
69
+ # link same_table and seats
70
+ model.add_bool_or([seats[[t, g1]].not, seats[[t, g2]].not, same_table[[g1, g2, t]]])
71
+ model.add_implication(same_table[[g1, g2, t]], seats[[t, g1]])
72
+ model.add_implication(same_table[[g1, g2, t]], seats[[t, g2]])
73
+ end
74
+
75
+ # link colocated and same_table
76
+ model.add(model.sum(all_tables.map { |t| same_table[[g1, g2, t]] }) == colocated[[g1, g2]])
77
+ end
78
+
79
+ # min known neighbors rule
80
+ same_table_by_person = Hash.new { |hash, key| hash[key] = [] }
81
+ same_table.each do |(g1, g2, t), v|
82
+ next unless @connections_for[g1][g2]
83
+ same_table_by_person[g1] << v
84
+ same_table_by_person[g2] << v
85
+ end
86
+ same_table_by_person.each do |_, vars|
87
+ model.add(model.sum(vars) >= min_connections)
88
+ end
89
+
90
+ # solve
91
+ solver = ORTools::CpSolver.new
92
+ status = solver.solve(model)
93
+ raise Error, "No solution found" unless [:feasible, :optimal].include?(status)
94
+
95
+ # read solution
96
+ @assignments = {}
97
+ seats.each do |k, v|
98
+ if solver.value(v)
99
+ @assignments[k[1]] = k[0]
100
+ end
101
+ end
102
+ @total_weight = solver.objective_value
103
+ end
104
+
105
+ def assigned_tables
106
+ assignments.group_by { |_, v| v }.map { |k, v| [k, v.map(&:first)] }.sort_by(&:first).map(&:last)
107
+ end
108
+
109
+ def connections_for(person, same_table: false)
110
+ result = @connections_for[person]
111
+ result = result.select { |k, _| @assignments[k] == @assignments[person] } if same_table
112
+ result
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,132 @@
1
+ module ORTools
2
+ class Sudoku
3
+ attr_reader :solution
4
+
5
+ def initialize(initial_grid, x: false, magic_square: false, anti_knight: false, anti_king: false, non_consecutive: false)
6
+ raise ArgumentError, "Grid must be 9x9" unless initial_grid.size == 9 && initial_grid.all? { |r| r.size == 9 }
7
+ raise ArgumentError, "Grid must contain values between 0 and 9" unless initial_grid.flatten(1).all? { |v| (0..9).include?(v) }
8
+
9
+ model = ORTools::CpModel.new
10
+
11
+ cell_size = 3
12
+ line_size = cell_size**2
13
+ line = (0...line_size).to_a
14
+ cell = (0...cell_size).to_a
15
+
16
+ grid = {}
17
+ line.each do |i|
18
+ line.each do |j|
19
+ grid[[i, j]] = model.new_int_var(1, line_size, "grid %i %i" % [i, j])
20
+ end
21
+ end
22
+
23
+ line.each do |i|
24
+ model.add_all_different(line.map { |j| grid[[i, j]] })
25
+ end
26
+
27
+ line.each do |j|
28
+ model.add_all_different(line.map { |i| grid[[i, j]] })
29
+ end
30
+
31
+ cell.each do |i|
32
+ cell.each do |j|
33
+ one_cell = []
34
+ cell.each do |di|
35
+ cell.each do |dj|
36
+ one_cell << grid[[i * cell_size + di, j * cell_size + dj]]
37
+ end
38
+ end
39
+ model.add_all_different(one_cell)
40
+ end
41
+ end
42
+
43
+ line.each do |i|
44
+ line.each do |j|
45
+ if initial_grid[i][j] != 0
46
+ model.add(grid[[i, j]] == initial_grid[i][j])
47
+ end
48
+ end
49
+ end
50
+
51
+ if x
52
+ model.add_all_different(9.times.map { |i| grid[[i, i]] })
53
+ model.add_all_different(9.times.map { |i| grid[[i, 8 - i]] })
54
+ end
55
+
56
+ if magic_square
57
+ magic_sums = []
58
+ 3.times do |i|
59
+ magic_sums << model.sum(3.times.map { |j| grid[[3 + i, 3 + j]] })
60
+ magic_sums << model.sum(3.times.map { |j| grid[[3 + j, 3 + i]] })
61
+ end
62
+
63
+ magic_sums << model.sum(3.times.map { |i| grid[[3 + i, 3 + i]] })
64
+ magic_sums << model.sum(3.times.map { |i| grid[[3 + i, 5 - i]] })
65
+
66
+ first_sum = magic_sums.shift
67
+ magic_sums.each do |magic_sum|
68
+ model.add(magic_sum == first_sum)
69
+ end
70
+ end
71
+
72
+ if anti_knight
73
+ # add anti-knights rule
74
+ # for each square, add squares that cannot be feasible
75
+ moves = [[1, 2], [2, 1], [2, -1], [1, -2], [-1, -2], [-2, -1], [-2, 1], [-1, 2]]
76
+ 9.times do |i|
77
+ 9.times do |j|
78
+ moves.each do |mi, mj|
79
+ square = grid[[i + mi, j + mj]]
80
+ if square
81
+ model.add(grid[[i, j]] != square)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ if anti_king
89
+ # add anti-king rule
90
+ # for each square, add squares that cannot be feasible
91
+ moves = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
92
+ 9.times do |i|
93
+ 9.times do |j|
94
+ moves.each do |mi, mj|
95
+ square = grid[[i + mi, j + mj]]
96
+ if square
97
+ model.add(grid[[i, j]] != square)
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ if non_consecutive
105
+ # add non-consecutive rule
106
+ # for each square, add squares that cannot be feasible
107
+ moves = [[1, 0], [0, 1], [-1, 0], [0, -1]]
108
+ 9.times do |i|
109
+ 9.times do |j|
110
+ moves.each do |mi, mj|
111
+ square = grid[[i + mi, j + mj]]
112
+ if square
113
+ model.add(grid[[i, j]] + 1 != square)
114
+ model.add(grid[[i, j]] - 1 != square)
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ solver = ORTools::CpSolver.new
122
+ status = solver.solve(model)
123
+ raise Error, "No solution found" unless [:feasible, :optimal].include?(status)
124
+
125
+ solution = []
126
+ line.each do |i|
127
+ solution << line.map { |j| solver.value(grid[[i, j]]) }
128
+ end
129
+ @solution = solution
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,60 @@
1
+ module ORTools
2
+ class TSP
3
+ attr_reader :route, :route_indexes, :distances, :total_distance
4
+
5
+ DISTANCE_SCALE = 1000
6
+ DEGREES_TO_RADIANS = Math::PI / 180
7
+
8
+ def initialize(locations)
9
+ raise ArgumentError, "Locations must have latitude and longitude" unless locations.all? { |l| l[:latitude] && l[:longitude] }
10
+ raise ArgumentError, "Latitude must be between -90 and 90" unless locations.all? { |l| l[:latitude] >= -90 && l[:latitude] <= 90 }
11
+ raise ArgumentError, "Longitude must be between -180 and 180" unless locations.all? { |l| l[:longitude] >= -180 && l[:longitude] <= 180 }
12
+ raise ArgumentError, "Must be at least two locations" unless locations.size >= 2
13
+
14
+ distance_matrix =
15
+ locations.map do |from|
16
+ locations.map do |to|
17
+ # must be integers
18
+ (distance(from, to) * DISTANCE_SCALE).to_i
19
+ end
20
+ end
21
+
22
+ manager = ORTools::RoutingIndexManager.new(locations.size, 1, 0)
23
+ routing = ORTools::RoutingModel.new(manager)
24
+
25
+ distance_callback = lambda do |from_index, to_index|
26
+ from_node = manager.index_to_node(from_index)
27
+ to_node = manager.index_to_node(to_index)
28
+ distance_matrix[from_node][to_node]
29
+ end
30
+
31
+ transit_callback_index = routing.register_transit_callback(distance_callback)
32
+ routing.set_arc_cost_evaluator_of_all_vehicles(transit_callback_index)
33
+ assignment = routing.solve(first_solution_strategy: :path_cheapest_arc)
34
+
35
+ @route_indexes = []
36
+ @distances = []
37
+
38
+ index = routing.start(0)
39
+ while !routing.end?(index)
40
+ @route_indexes << manager.index_to_node(index)
41
+ previous_index = index
42
+ index = assignment.value(routing.next_var(index))
43
+ @distances << routing.arc_cost_for_vehicle(previous_index, index, 0) / DISTANCE_SCALE.to_f
44
+ end
45
+ @route_indexes << manager.index_to_node(index)
46
+ @route = locations.values_at(*@route_indexes)
47
+ @total_distance = @distances.sum
48
+ end
49
+
50
+ private
51
+
52
+ def distance(from, to)
53
+ from_lat = from[:latitude] * DEGREES_TO_RADIANS
54
+ from_lng = from[:longitude] * DEGREES_TO_RADIANS
55
+ to_lat = to[:latitude] * DEGREES_TO_RADIANS
56
+ to_lng = to[:longitude] * DEGREES_TO_RADIANS
57
+ 2 * 6371 * Math.asin(Math.sqrt(Math.sin((to_lat - from_lat) / 2.0)**2 + Math.cos(from_lat) * Math.cos(to_lat) * Math.sin((from_lng - to_lng) / 2.0)**2))
58
+ end
59
+ end
60
+ end
@@ -1,3 +1,3 @@
1
1
  module ORTools
2
- VERSION = "0.1.3"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: or-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-24 00:00:00.000000000 Z
11
+ date: 2020-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rice
@@ -93,20 +93,25 @@ files:
93
93
  - README.md
94
94
  - ext/or-tools/ext.cpp
95
95
  - ext/or-tools/extconf.rb
96
+ - ext/or-tools/vendor.rb
96
97
  - lib/or-tools.rb
98
+ - lib/or_tools/basic_scheduler.rb
99
+ - lib/or_tools/bool_var.rb
97
100
  - lib/or_tools/comparison.rb
98
101
  - lib/or_tools/comparison_operators.rb
99
102
  - lib/or_tools/cp_model.rb
100
103
  - lib/or_tools/cp_solver.rb
101
104
  - lib/or_tools/cp_solver_solution_callback.rb
102
- - lib/or_tools/ext.bundle
103
105
  - lib/or_tools/int_var.rb
104
106
  - lib/or_tools/knapsack_solver.rb
105
107
  - lib/or_tools/linear_expr.rb
106
108
  - lib/or_tools/routing_model.rb
107
109
  - lib/or_tools/sat_int_var.rb
108
110
  - lib/or_tools/sat_linear_expr.rb
111
+ - lib/or_tools/seating.rb
109
112
  - lib/or_tools/solver.rb
113
+ - lib/or_tools/sudoku.rb
114
+ - lib/or_tools/tsp.rb
110
115
  - lib/or_tools/version.rb
111
116
  homepage: https://github.com/ankane/or-tools
112
117
  licenses:
Binary file