opl 2.4.1 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/lib/opl.rb +7 -2
- data/lib/sudoku.rb +71 -0
- metadata +20 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 577e5853623d778f8c4aa44f28e787180aededce471e953107af513a132793d8
|
4
|
+
data.tar.gz: 58364f2f6b34053fa45a8a6bdb827b6fcd777cfb14d4752a6be82c1f214d6b7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d529766f941755f63be3a3e44a1cb8986c16368119493076bdb61aaf1f91c63e8bc73d6ab66b5bca512aebaf1ce72c99a1cf94f37a041000fa99cbc0bfbc43a4
|
7
|
+
data.tar.gz: 98d388bdd84b40c9476031fffd036255eb06e9c120b84ef916f9bf796714b68087bf7fa7dcc379fc7c5e6d25e270283f08219ec8edf828c68591f46612135963
|
data/lib/opl.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "rglpk"
|
2
|
-
require_relative "string.rb"
|
3
2
|
require_relative "array.rb"
|
3
|
+
require_relative "string.rb"
|
4
|
+
require_relative "sudoku.rb"
|
4
5
|
|
5
6
|
# Notes for future functionality
|
6
7
|
#
|
@@ -1115,7 +1116,11 @@ def optimize(optimization, objective, lp)
|
|
1115
1116
|
end
|
1116
1117
|
lp.solution = answer
|
1117
1118
|
lp.rglpk_object = p
|
1118
|
-
|
1119
|
+
begin
|
1120
|
+
lp.matrix_solution = lp.solution_as_matrix
|
1121
|
+
rescue
|
1122
|
+
return lp
|
1123
|
+
end
|
1119
1124
|
if lp.stop_processing
|
1120
1125
|
lp.solution = lp.error_message
|
1121
1126
|
lp.matrix_solution = lp.error_message
|
data/lib/sudoku.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
class OPL
|
2
|
+
class Sudoku
|
3
|
+
attr_accessor :input_matrix
|
4
|
+
attr_accessor :lp
|
5
|
+
attr_accessor :solution
|
6
|
+
|
7
|
+
def initialize(input_matrix)
|
8
|
+
@input_matrix = input_matrix
|
9
|
+
""
|
10
|
+
end
|
11
|
+
|
12
|
+
def solve
|
13
|
+
size = input_matrix.count
|
14
|
+
rubysize = size-1
|
15
|
+
|
16
|
+
constant_constraints = []
|
17
|
+
input_matrix.each_index do |i|
|
18
|
+
row = input_matrix[i]
|
19
|
+
|
20
|
+
row.each_index do |j|
|
21
|
+
element = input_matrix[i][j]
|
22
|
+
|
23
|
+
if element != 0
|
24
|
+
constant_constraints << "x[#{i}][#{j}][#{element-1}] = 1"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
@lp = minimize("y", subject_to([
|
30
|
+
"y = 2",# y is a dummy variable so I don't have to worry about the objective function
|
31
|
+
"forall(i in (0..#{rubysize}), j in (0..#{rubysize}), sum(k in (0..#{rubysize}), x[i][j][k]) = 1)",# an element contains only one number
|
32
|
+
"forall(i in (0..#{rubysize}), k in (0..#{rubysize}), sum(j in (0..#{rubysize}), x[i][j][k]) = 1)",# every row contains every number
|
33
|
+
"forall(j in (0..#{rubysize}), k in (0..#{rubysize}), sum(i in (0..#{rubysize}), x[i][j][k]) = 1)",# every column contains every number
|
34
|
+
"forall(u in [0,3,6], v in [0,3,6], k in (0..#{rubysize}), sum(i in ((0+u)..(#{(size/3)-1}+u)), j in ((0+v)..(#{(size/3)-1}+v)), x[i][j][k]) = 1)",# every 3x3 grid contains every number
|
35
|
+
constant_constraints# some elements already have their values set
|
36
|
+
].flatten,["BOOLEAN: x"]))
|
37
|
+
""
|
38
|
+
end
|
39
|
+
|
40
|
+
def format_solution
|
41
|
+
@lp.matrix_solution["x"]
|
42
|
+
mat = @lp.matrix_solution["x"]
|
43
|
+
sol = Array.new(mat[0][0].size) { Array.new(mat[0][0].size, 0) }
|
44
|
+
mat.each_index do |i|
|
45
|
+
mat[i].each_index do |j|
|
46
|
+
mat[i][j].each_index do |k|
|
47
|
+
if mat[i][j][k].to_f == 1.0
|
48
|
+
sol[i][j] = k+1
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
@solution = sol
|
54
|
+
""
|
55
|
+
end
|
56
|
+
|
57
|
+
def print_problem
|
58
|
+
@input_matrix.each do |row|
|
59
|
+
puts row.join(" ")
|
60
|
+
end
|
61
|
+
""
|
62
|
+
end
|
63
|
+
|
64
|
+
def print_solution
|
65
|
+
@solution.each do |row|
|
66
|
+
puts row.join(" ")
|
67
|
+
end
|
68
|
+
""
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Godlove
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
11
|
+
date: 2020-06-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rglpk
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.4.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.4.0
|
13
27
|
description: This gem gives you a beautifully simple way to formulate your linear
|
14
28
|
or mixed integer program. The syntax is inspired by OPL Studio, which remains my
|
15
29
|
favorite linear programming software, but the license is quite expensive.
|
@@ -21,9 +35,10 @@ files:
|
|
21
35
|
- lib/array.rb
|
22
36
|
- lib/opl.rb
|
23
37
|
- lib/string.rb
|
38
|
+
- lib/sudoku.rb
|
24
39
|
homepage: http://github.com/brg8/opl
|
25
40
|
licenses:
|
26
|
-
-
|
41
|
+
- MIT
|
27
42
|
metadata: {}
|
28
43
|
post_install_message:
|
29
44
|
rdoc_options: []
|
@@ -40,8 +55,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
55
|
- !ruby/object:Gem::Version
|
41
56
|
version: '0'
|
42
57
|
requirements: []
|
43
|
-
|
44
|
-
rubygems_version: 2.2.2
|
58
|
+
rubygems_version: 3.0.6
|
45
59
|
signing_key:
|
46
60
|
specification_version: 4
|
47
61
|
summary: Linear Or Mixed Integer Program Solver
|