opl 1.0.1 → 1.1.0

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.
Files changed (2) hide show
  1. data/lib/opl.rb +35 -11
  2. metadata +2 -2
data/lib/opl.rb CHANGED
@@ -9,20 +9,10 @@ require "rglpk"
9
9
  # "x >= 0"
10
10
  # ]))
11
11
 
12
- #put a warning about branch and cut in the github description
13
-
14
- #1.1
15
- #setting variable values in constraints
16
- #and then using that variable value in further constraints
17
-
18
12
  #1.2
19
- #summing of variables
20
- #e.g. x1 + x1 <= 3
21
-
22
- #1.3
23
13
  #allow a POSITIVE: x option or NEGATIVE: x
24
14
 
25
- #1.4
15
+ #1.3
26
16
  #float coefficients
27
17
 
28
18
  #2.0
@@ -471,6 +461,37 @@ class OPL
471
461
  end
472
462
  variable_type_hash
473
463
  end
464
+
465
+ def self.sum_variables(formatted_constraint)
466
+ #in: x + y - z + x[3] + z + y - z + x - y <= 0
467
+ #out: 2*x + y - z + x[3] <= 0
468
+ helper = self
469
+ lhs = helper.sides(formatted_constraint)[:lhs]
470
+ formatted_lhs = helper.add_ones(lhs)
471
+ vars = helper.variables(formatted_lhs)
472
+ coefs = helper.coefficients(formatted_lhs)
473
+ var_coef_hash = {}
474
+ vars.each_index do |i|
475
+ var = vars[i]
476
+ coef = coefs[i]
477
+ if var_coef_hash[var]
478
+ var_coef_hash[var] += coefs[i].to_f
479
+ else
480
+ var_coef_hash[var] = coefs[i].to_f
481
+ end
482
+ end
483
+ new_lhs = ""
484
+ var_coef_hash.keys.each do |key|
485
+ coef = var_coef_hash[key].to_s
486
+ var = key
487
+ coef = "+"+coef unless coef.include?("-")
488
+ new_lhs += coef+"*"+var
489
+ end
490
+ if new_lhs[0] == "+"
491
+ new_lhs = new_lhs[1..-1]
492
+ end
493
+ formatted_constraint.gsub(lhs, new_lhs)
494
+ end
474
495
  end
475
496
 
476
497
  class LinearProgram
@@ -560,6 +581,9 @@ def subject_to(constraints, options=[])
560
581
  constraints = constraints.map do |constraint|
561
582
  OPL::Helper.sub_rhs_with_summed_constants(constraint)
562
583
  end
584
+ constraints = constraints.map do |constraint|
585
+ OPL::Helper.sum_variables(constraint)
586
+ end
563
587
  all_vars = OPL::Helper.get_all_vars(constraints)
564
588
  variable_type_hash = OPL::Helper.produce_variable_type_hash(variable_types, all_vars)
565
589
  rows = []
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opl
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-24 00:00:00.000000000 Z
12
+ date: 2013-09-25 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Built on top of the glpk gem for linear programming. The syntax is copied
15
15
  from OPL Studio, which remains my favorite linear programming software, but the