simplex 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -6
  3. data/lib/simplex.rb +14 -5
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d586cdecfc5ebff9463168a641095b14c66f3eb
4
- data.tar.gz: 1b99632bec24b3ea8ecf2191e3991f359c45a39a
3
+ metadata.gz: 8c7dddbf0b80831adfd609722385b79e61c3e541
4
+ data.tar.gz: 5a21258e6ab16af709a34c45848f9e5c68ced58c
5
5
  SHA512:
6
- metadata.gz: 30a1509d8f7a9555ed74f3e8ee2148f077d99762e35db770ec5234a4e001ca508fde8e22c9c169f47ebd4bbdcf7a4d4b88d48b2c765f0f5877fd4432ce42198d
7
- data.tar.gz: cdda93f918effea817079e509e17790c864fb681f5427b9d27061649873d7d91035fd60269be7f402521edcdc95a47f35bccb990c4af14cab6261e21aeae4123
6
+ metadata.gz: 769f0edd69b3770e369f6dbb5cbf10c8d1f1dda626392dcf01397fa8874fd38b32251fb76ac104a96171841e8e48dd6ef6c876626b39520f974228f27f7c9e6b
7
+ data.tar.gz: 5e7010f1267a55da2a2219508fb896e3a547c66d4605d8a0f349eccd2126d4c3d6ca7d696bdb3c4f1f25c2b6f307fe1fe1c9ef700b35e3271460d6736a43a4f3
data/README.md CHANGED
@@ -4,7 +4,7 @@ simplex
4
4
 
5
5
  A naive pure-Ruby implementation of the Simplex algorithm for solving linear programming problems. Solves maximizations in standard form.
6
6
 
7
- ## Why?
7
+ ### Why?
8
8
 
9
9
  I wrote this because I needed to solve some small allocation problems for a web game I'm writing,
10
10
  and there didn't seem to be any Ruby 2.0 bindings for the "pro" solver libraries,
@@ -14,16 +14,16 @@ and anyway they are hard to install on Heroku.
14
14
  and when you can accept not that great performance.
15
15
  * *Don't use it for*: large LP problems, when you have access to native solvers, when you need very fast solving time.
16
16
 
17
- ## Usage
17
+ ### Usage
18
18
 
19
19
  To solve the linear programming problem:
20
20
 
21
- max x + y
21
+ max x + y
22
22
 
23
- 2x + y <= 4
24
- x + 2y <= 3
23
+ 2x + y <= 4
24
+ x + 2y <= 3
25
25
 
26
- x, y >= 0
26
+ x, y >= 0
27
27
 
28
28
  Like this:
29
29
 
@@ -35,4 +35,5 @@ Like this:
35
35
  ],
36
36
  [4, 3] # .. and the rhs of the inequalities
37
37
  ).solution
38
+ # => [(5/3), (2/3)]
38
39
 
@@ -11,7 +11,8 @@ class Vector
11
11
  end
12
12
 
13
13
  class Simplex
14
- DEFAULT_MAX_ITERATIONS = 1_000_000
14
+ DEFAULT_MAX_ITERATIONS = 10_000
15
+
15
16
  attr_accessor :max_iterations
16
17
 
17
18
  def initialize(c, a, b)
@@ -38,8 +39,10 @@ class Simplex
38
39
  def solve
39
40
  return if @solved
40
41
  i = 0
41
- while can_improve? and i < @max_iterations
42
+ while can_improve?
42
43
  i += 1
44
+ raise "Too many iterations" if i > max_iterations
45
+
43
46
  pivot_column = entering_variable_ix
44
47
  pivot_row = minimum_coefficient_ratio_row_ix(pivot_column)
45
48
  leaving_var = leaving_variable(pivot_row)
@@ -103,7 +106,7 @@ class Simplex
103
106
  current_min_index = nil
104
107
  @c.each_with_index do |v, i|
105
108
  if v < 0
106
- if current_min_value == nil || v <= current_min_value
109
+ if current_min_value == nil || v < current_min_value
107
110
  current_min_value = v
108
111
  current_min_index = i
109
112
  end
@@ -125,8 +128,11 @@ class Simplex
125
128
  current_min_index = nil
126
129
  0.upto(@a.row_count - 1) do |row_ix|
127
130
  next if @a[row_ix, column_ix] == 0
128
- ratio = Rational(@b[row_ix], @a[row_ix, column_ix])
129
- if ratio > 0 && (!current_min_value || ratio < current_min_value)
131
+ b_val = @b[row_ix]
132
+ a_val = @a[row_ix, column_ix]
133
+ ratio = Rational(b_val, a_val)
134
+ is_negative = (@b[row_ix] < 0 || @a[row_ix, column_ix] < 0) && !(@b[row_ix] < 0 && @a[row_ix, column_ix] < 0)
135
+ if !is_negative && (!current_min_value || ratio <= current_min_value)
130
136
  current_min_value = ratio
131
137
  current_min_index = row_ix
132
138
  end
@@ -135,3 +141,6 @@ class Simplex
135
141
  end
136
142
 
137
143
  end
144
+
145
+
146
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplex
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Lucraft
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-15 00:00:00.000000000 Z
11
+ date: 2013-09-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Naive implementation of the simplex linear programming algorithm in pure
14
14
  Ruby.
@@ -38,7 +38,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
38
  version: '0'
39
39
  requirements: []
40
40
  rubyforge_project:
41
- rubygems_version: 2.0.6
41
+ rubygems_version: 2.0.3
42
42
  signing_key:
43
43
  specification_version: 4
44
44
  summary: Simplex linear programming solver