rulp 0.0.45 → 0.0.46

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 58a47250456efb480a41ea21a923355242c38c27d1e9eb04d17782493a51b083
4
- data.tar.gz: e7b05a98c43d57035049cd1b2537fd3ecb42792e08114d8dd0729229092e96ac
3
+ metadata.gz: 79e259317b851f96717dc4e0e66b07f08cf035efc420eeef915db1f40d7b5389
4
+ data.tar.gz: 68cfcdf7d345b9e1ee0c66b70b822a3edcac50af649a906902ef08db4f673f87
5
5
  SHA512:
6
- metadata.gz: d12655f339c1095d9bc5f3c4620829965e3375ae4025fceeaea0438d97e6f334bdc0b727e0ac975d03ef8b688da4775c6c34cc9b56c0885cfa6fe38cd757d2dc
7
- data.tar.gz: 9a3bd500cabd00c9118ff81cd79da588d629f5ee5788eae993d986161cd2ce3da715965534491f3d088b202154e53f124f7053dea671df606b028db96e24cdb4
6
+ metadata.gz: 0fe7414a15de24cd86043956491056cda383ce81ab21a7010add8715fbb2d2a376ff284faade9f7e66a512b1ae3c62926dcceb9dcf9a2dfe3e497eb11f558b8e
7
+ data.tar.gz: 3596934464b9aa30a43467fdc08e69a935f8d53219d2edf1b8110ca1806e71d5507802bd79d770df4d17be1d35f905e5b1b2a6554f3cbb4b5e78548864ebe9ee
@@ -21,15 +21,14 @@ class Expressions
21
21
  @expressions.map(&:variable)
22
22
  end
23
23
 
24
- [:==, :<, :<=, :>, :>=].each do |constraint_type|
25
- define_method(constraint_type){|value|
24
+ %i[== < <= > >=].each do |constraint_type|
25
+ define_method(constraint_type) do |value|
26
26
  Constraint.new(self, constraint_type, value)
27
- }
27
+ end
28
28
  end
29
29
 
30
30
  def -@
31
- -self.expressions[0]
32
- self
31
+ self.class.new(expressions.map(&:-@))
33
32
  end
34
33
 
35
34
  def -(other)
@@ -38,7 +37,7 @@ class Expressions
38
37
  end
39
38
 
40
39
  def +(other)
41
- Expressions.new(self.expressions + Expressions[other].expressions)
40
+ Expressions.new(expressions + Expressions[other].expressions)
42
41
  end
43
42
 
44
43
  def self.[](value)
@@ -50,7 +49,7 @@ class Expressions
50
49
  end
51
50
 
52
51
  def evaluate
53
- self.expressions.map(&:evaluate).inject(:+)
52
+ expressions.map(&:evaluate).inject(:+)
54
53
  end
55
54
  end
56
55
 
@@ -66,19 +65,19 @@ class Fragment
66
65
  end
67
66
 
68
67
  def +(other)
69
- return Expressions.new([self] + Expressions[other].expressions)
68
+ Expressions.new([self] + Expressions[other].expressions)
70
69
  end
71
70
 
72
71
  def -(other)
73
72
  self.+(-other)
74
73
  end
75
74
 
76
- def *(value)
77
- Fragment.new(@lv, @operand * value)
75
+ def *(other)
76
+ Fragment.new(@lv, @operand * other)
78
77
  end
79
78
 
80
79
  def evaluate
81
- if [TrueClass,FalseClass].include? @lv.value.class
80
+ if [TrueClass, FalseClass].include? @lv.value.class
82
81
  @operand * (@lv.value ? 1 : 0)
83
82
  else
84
83
  @operand * @lv.value
@@ -94,20 +93,18 @@ class Fragment
94
93
  @lv
95
94
  end
96
95
 
97
- [:==, :<, :<=, :>, :>=].each do |constraint_type|
98
- define_method(constraint_type){|value|
96
+ %i[== < <= > >=].each do |constraint_type|
97
+ define_method(constraint_type) do |value|
99
98
  Constraint.new(Expressions.new(self), constraint_type, value)
100
- }
99
+ end
101
100
  end
102
101
 
103
102
  def to_s
104
- @as_str ||= begin
105
- case @operand
106
- when -1 then " - #{@lv}"
107
- when 1 then " + #{@lv}"
108
- when ->(op){ op < 0} then " - #{@operand.abs} #{@lv}"
109
- else " + #{@operand} #{@lv}"
110
- end
111
- end
103
+ @as_str ||= case @operand
104
+ when -1 then " - #{@lv}"
105
+ when 1 then " + #{@lv}"
106
+ when ->(op) { op < 0 } then " - #{@operand.abs} #{@lv}"
107
+ else " + #{@operand} #{@lv}"
108
+ end
112
109
  end
113
- end
110
+ end
data/lib/rulp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rulp
2
- VERSION = '0.0.45'
3
- end
2
+ VERSION = '0.0.46'
3
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'test_helper'
2
+
3
+ class NegateExpressionTest < Minitest::Test
4
+
5
+ def test_negate_expression
6
+ @problem = Rulp::Max(- (X1_i + 2 * X2_i))
7
+ @problem[
8
+ X1_i + X2_i == 10,
9
+ X1_i >= 0,
10
+ X2_i >= 0
11
+ ]
12
+
13
+ @problem.solve
14
+ assert X1_i.value == 10
15
+ assert X2_i.value == 0
16
+ end
17
+
18
+ def test_double_negate_expression
19
+ @problem = Rulp::Max(-(-(X1_i + 2 * X2_i)))
20
+ @problem[
21
+ X1_i + X2_i == 10,
22
+ X1_i >= 0,
23
+ X2_i >= 0
24
+ ]
25
+
26
+ @problem.solve
27
+ assert X1_i.value == 0
28
+ assert X2_i.value == 10
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ require_relative 'test_helper'
2
+
3
+ class NegateTerm < Minitest::Test
4
+ def setup
5
+ @problem = Rulp::Max(-X1_i + 2 * X2_i)
6
+ @problem[
7
+ X1_i + X2_i == 10,
8
+ X1_i >= 0,
9
+ X2_i >= 0
10
+ ]
11
+
12
+ @problem.solve
13
+ end
14
+
15
+ def test_negate_term
16
+ assert X1_i.value == 0
17
+ assert X2_i.value == 10
18
+ end
19
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rulp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.45
4
+ version: 0.0.46
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wouter Coppieters
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-03 00:00:00.000000000 Z
11
+ date: 2024-04-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple Ruby LP description DSL
14
14
  email: wc@pico.net.nz
@@ -42,6 +42,8 @@ files:
42
42
  - test/test_boolean.rb
43
43
  - test/test_helper.rb
44
44
  - test/test_infeasible.rb
45
+ - test/test_negate_expression.rb
46
+ - test/test_negate_term.rb
45
47
  - test/test_save_to_file.rb
46
48
  - test/test_simple.rb
47
49
  homepage:
@@ -71,5 +73,7 @@ test_files:
71
73
  - test/test_boolean.rb
72
74
  - test/test_helper.rb
73
75
  - test/test_infeasible.rb
76
+ - test/test_negate_expression.rb
77
+ - test/test_negate_term.rb
74
78
  - test/test_save_to_file.rb
75
79
  - test/test_simple.rb