Class: OPL::Helper
- Inherits:
-
Object
- Object
- OPL::Helper
- Defined in:
- lib/opl.rb
Class Method Summary collapse
- .add_ones(text, lp) ⇒ Object
- .check_options_syntax(options) ⇒ Object
-
.coefficients(text, lp) ⇒ Object
text is one side of the equation.
- .data_coefficients ⇒ Object
- .forall(text) ⇒ Object
- .get_all_vars(constraints, lp) ⇒ Object
- .get_coefficient_variable_pairs(text) ⇒ Object
- .get_column_bounds(bound_info, all_variables) ⇒ Object
- .get_constants(text) ⇒ Object
- .mass_product(array_of_arrays, base = []) ⇒ Object
- .operator(constraint) ⇒ Object
- .parse_data(data_info) ⇒ Object
- .produce_variable_type_hash(variable_types, all_variables) ⇒ Object
- .put_constants_on_rhs(text) ⇒ Object
- .put_variables_on_lhs(text) ⇒ Object
- .remove_constants(text) ⇒ Object
- .sides(text) ⇒ Object
- .split_equals(constraint) ⇒ Object
- .split_equals_a(constraints) ⇒ Object
- .sub_forall(equation, indexvalues = {:indices => [], :values => []}) ⇒ Object
- .sub_rhs_with_summed_constants(constraint) ⇒ Object
- .sub_sum(equation, lp, indexvalues = {:indices => [], :values => []}) ⇒ Object
- .substitute_data(text, lp) ⇒ Object
- .sum(text, lp, indexvalues = {:indices => [], :values => []}) ⇒ Object
- .sum_constants(text) ⇒ Object
- .sum_indices(constraint) ⇒ Object
- .sum_variables(formatted_constraint, lp) ⇒ Object
-
.variables(text, lp) ⇒ Object
parameter is one side of the equation.
Class Method Details
.add_ones(text, lp) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/opl.rb', line 257 def self.add_ones(text, lp) equation = text equation = "#"+equation equation.scan(/[#+-][a-z]/).each do |p| if p.include?("+") q = p.gsub("+", "+1*") elsif p.include?("-") q = p.gsub("-","-1*") elsif p.include?("#") q = p.gsub("#","#1*") end equation = equation.gsub(p,q) end equation.gsub("#","") end |
.check_options_syntax(options) ⇒ Object
717 718 719 720 721 722 723 724 725 726 727 728 729 730 |
# File 'lib/opl.rb', line 717 def self.() return if .empty? .each do |option| if option.include?(":") title = option.gsub(" ","").split(":")[0] value = option.gsub(" ","").split(":")[1] if !["nonnegative", "integer", "boolean", "data", "epsilon"].include?(title.downcase) raise "Did not recognize the TITLE parameter '#{title}' in the options." end else raise "Options parameter '#{option}' does not have a colon in it. The proper syntax of an option is TITLE: VALUE" end end end |
.coefficients(text, lp) ⇒ Object
text is one side of the equation
368 369 370 371 372 373 374 375 |
# File 'lib/opl.rb', line 368 def self.coefficients(text, lp)#text is one side of the equation equation = self.add_ones(text, lp) if equation[0]=="-" equation.scan(/[+-][\d\.]+/) else ("#"+equation).scan(/[#+-][\d\.]+/).map{|e|e.gsub("#","+")} end end |
.data_coefficients ⇒ Object
377 378 379 |
# File 'lib/opl.rb', line 377 def self.data_coefficients end |
.forall(text) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/opl.rb', line 170 def self.forall(text) #in: "i in (0..2), x[i] <= 5" #out: ["x[0] <= 5", "x[1] <= 5", "x[2] <= 5"] helper = self text = text.sub_paren_with_array if ((text.gsub(" ","")).scan(/\]\,/).size) + ((text.gsub(" ","")).scan(/\)\,/).size) != text.gsub(" ","").scan(/in/).size raise "The following forall() constraint is incorrectly formatted: #{text}. Please see the examples in test.rb for forall() constraints. I suspect you are missing a comma somewhere." end final_constraints = [] if text.include?("sum") indices = text.split("sum")[0].scan(/[a-z] in/).map{|sc|sc[0]} values = text.split("sum")[0].scan(/\s\[[\-\s\d+,]+\]/).map{|e|e.gsub(" ", "").scan(/[\-\d]+/)} else indices = text.scan(/[a-z] in/).map{|sc|sc[0]} values = text.scan(/\s\[[\-\s\d+,]+\]/).map{|e|e.gsub(" ", "").scan(/[\-\d]+/)} end #TODO: the indices and values should only be those #of the forall(), not of any sum() that is #inside the forall() index_value_pairs = indices.zip(values) variable = text.scan(/[a-z]\[/)[0].gsub("[","") value_combinations = helper.mass_product(values) value_combinations.each_index do |vc_index| value_combination = value_combinations[vc_index] value_combination = [value_combination] unless value_combination.is_a?(Array) if text.include?("sum") constraint = "sum"+text.split("sum")[1..-1].join("sum") else constraint = text.split(",")[-1].gsub(" ","") end e = constraint value_combination.each_index do |i| index = indices[i] value = value_combination[i] e = e.gsub("("+index, "("+value) e = e.gsub(index+")", value+")") e = e.gsub("["+index, "["+value) e = e.gsub(index+"]", value+"]") e = e.gsub("=>"+index, "=>"+value) e = e.gsub("<="+index, "<="+value) e = e.gsub(">"+index, ">"+value) e = e.gsub("<"+index, "<"+value) e = e.gsub("="+index, "="+value) e = e.gsub("=> "+index, "=> "+value) e = e.gsub("<= "+index, "<= "+value) e = e.gsub("> "+index, "> "+value) e = e.gsub("< "+index, "< "+value) e = e.gsub("= "+index, "= "+value) end final_constraints += [e] end final_constraints end |
.get_all_vars(constraints, lp) ⇒ Object
386 387 388 389 390 391 392 393 394 |
# File 'lib/opl.rb', line 386 def self.get_all_vars(constraints, lp) all_vars = [] constraints.each do |constraint| constraint = constraint.gsub(" ", "") value = constraint.split(":")[1] || constraint all_vars << self.variables(value, lp) end all_vars.flatten.uniq end |
.get_coefficient_variable_pairs(text) ⇒ Object
474 475 476 |
# File 'lib/opl.rb', line 474 def self.get_coefficient_variable_pairs(text) text.scan(/\d*[\*]*[a-z]\[*\d*\]*/) end |
.get_column_bounds(bound_info, all_variables) ⇒ Object
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
# File 'lib/opl.rb', line 608 def self.get_column_bounds(bound_info, all_variables) #in: ["NONNEGATIVE: x1"] #out: {:x1 => {:lower => 0}} column_bounds = {} bound_info.each do |info| type = info.gsub(" ","").split(":")[0] if type.downcase == "nonnegative" bounds = {:lower => 0} end variables = info.split(":")[1].gsub(" ","").split(",") variables.each do |root_var| all_variables_with_root = all_variables.find_all{|var|var.include?("[") && var.split("[")[0]==root_var}+[root_var] all_variables_with_root.each do |var| column_bounds[var.to_sym] = bounds end end end column_bounds end |
.get_constants(text) ⇒ Object
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/opl.rb', line 396 def self.get_constants(text) #in: "-8 + x + y + 3" #out: "[-8, +3]" text = text.gsub(" ","") text = text+"#" cs = [] potential_constants = text.scan(/[\d\.]+[^a-z^\[^\]^\d^\.^\)^\*]/) constants = potential_constants.find_all{|c|![*('a'..'z'),*('A'..'Z'),"["].include?(text[text.index(c)-1])} searchable_text = text constants.each_index do |i| constant = constants[i] c = constant.scan(/[\d\.]+/)[0] index = searchable_text.index(constant) if index == 0 c = "+"+c else constant = constant.gsub('+','[+]') constant = constant.gsub('-','[-]') c = searchable_text.scan(/[\-\+]#{constant}/)[0] end cs << c.scan(/[\-\+][\d\.]+/)[0] searchable_text[index] = "**" end return({:formatted => cs, :unformatted => constants}) end |
.mass_product(array_of_arrays, base = []) ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/opl.rb', line 159 def self.mass_product(array_of_arrays, base=[]) return(base) if array_of_arrays.empty? array = array_of_arrays[0] new_array_of_arrays = array_of_arrays[1..-1] if base==[] self.mass_product(new_array_of_arrays, array) else self.mass_product(new_array_of_arrays, base.product(array).map{|e|e.flatten}) end end |
.operator(constraint) ⇒ Object
478 479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'lib/opl.rb', line 478 def self.operator(constraint) if constraint.include?(">=") ">=" elsif constraint.include?("<=") "<=" elsif constraint.include?(">") ">" elsif constraint.include?("<") "<" elsif constraint.include?("=") "=" end end |
.parse_data(data_info) ⇒ Object
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
# File 'lib/opl.rb', line 628 def self.parse_data(data_info) #in: "DATA: {d => [1, 0.3, 1.5], o => [10.3, 4.0005, -1]}" #out: [data_object_1, data_object_2] data_hash_string = data_info.gsub(" ","").split(":")[1] data_string = data_hash_string.gsub("{",",").gsub("}",",") names = data_string.scan(/,[a-z]/).map{|comma_name|comma_name.gsub(",","")} string_values = data_string.scan(/\=\>[\[\d\.\]\,\-]+,/).map{|scanned_value|scanned_value[2..-2]} values = string_values.map{|sv|sv.to_a} data_hash = {} names.each_index do |i| name = names[i] value = values[i] value = value[0] if value.size == 1 data_hash[name] = value end return(data_hash) end |
.produce_variable_type_hash(variable_types, all_variables) ⇒ Object
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
# File 'lib/opl.rb', line 555 def self.produce_variable_type_hash(variable_types, all_variables) #in: ["BOOLEAN: x, y", "INTEGER: z"] #out: {:x => 3, :y => 3, :z => 2} variable_type_hash = {} variable_types.each do |vt| type = vt.gsub(" ","").split(":")[0] if type.downcase == "boolean" type_number = 3 elsif type.downcase == "integer" type_number = 2 end variables = vt.split(":")[1].gsub(" ","").split(",") variables.each do |root_var| all_variables_with_root = all_variables.find_all{|var|var.include?("[") && var.split("[")[0]==root_var}+[root_var] all_variables_with_root.each do |var| variable_type_hash[var.to_sym] = type_number end end end variable_type_hash end |
.put_constants_on_rhs(text) ⇒ Object
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/opl.rb', line 422 def self.put_constants_on_rhs(text) #in: "-8 + x + y + 3 <= 100" #out: "x + y <= 100 + 5" text = text.gsub(" ","") s = self.sides(text) constants_results = self.get_constants(s[:lhs]) constants = [] constants_results[:formatted].each_index do |i| formatted_constant = constants_results[:formatted][i] unformatted_constant = constants_results[:unformatted][i] unless unformatted_constant.include?("*") constants << formatted_constant end end unless constants.empty? sum = constants.map{|cc|cc.to_f}.inject("+").to_s if sum.include?("-") sum = sum.gsub("-","+") else sum = "-"+sum end lhs = s[:lhs].gsub(" ","")+"#" constants_results[:unformatted].each do |constant| index = lhs.index(constant) if index == 0 lhs = lhs[(constant.size-1)..(lhs.size-1)] else lhs = lhs[0..(index-2)]+lhs[(index+(constant.size-1))..(lhs.size-1)] end end text = text.gsub(s[:lhs], lhs[0..-2]) text += sum end return(text) end |
.put_variables_on_lhs(text) ⇒ Object
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/opl.rb', line 492 def self.put_variables_on_lhs(text) #in: "x + y - x[3] <= 3z + 2x[2] - 10" #out: "x + y - x[3] - 3z - 2x[2] <= -10" text = text.gsub(" ", "") s = self.sides(text) oper = self.operator(text) rhs = s[:rhs] lhs = s[:lhs] coefficient_variable_pairs = self.get_coefficient_variable_pairs(rhs) add_to_left = [] remove_from_right = [] coefficient_variable_pairs.each do |cvp| index = rhs.index(cvp) if index == 0 add_to_left << "-"+cvp remove_from_right << cvp else if rhs[index-1] == "+" add_to_left << "-"+cvp remove_from_right << "+"+cvp else add_to_left << "+"+cvp remove_from_right << "-"+cvp end end end new_lhs = lhs+add_to_left.join("") text = text.gsub(lhs+oper, new_lhs+oper) new_rhs = rhs remove_from_right.each do |rfr| new_rhs = new_rhs.gsub(rfr, "") end new_rhs = "0" if new_rhs == "" text = text.gsub(oper+rhs, oper+new_rhs) return(text) end |
.remove_constants(text) ⇒ Object
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
# File 'lib/opl.rb', line 682 def self.remove_constants(text) helper = self text = text.gsub(" ","") text = "+"+text if text[0]!="-" text = text+"#" constants = helper.get_constants(text) replacements = [] constants[:formatted].each_index do |i| formatted = constants[:formatted][i] unformatted = constants[:unformatted][i] if formatted.include?("+") if unformatted.include?("+") replacements << ["+"+unformatted, "+"] elsif unformatted.include?("-") replacements << ["-"+unformatted, "-"] elsif unformatted.include?("#") replacements << [formatted+"#",""] end elsif formatted.include?("-") if unformatted.include?("+") replacements << ["-"+unformatted, "+"] elsif unformatted.include?("-") replacements << ["-"+unformatted, "-"] elsif unformatted.include?("#") replacements << [formatted+"#",""] end end end replacements.each do |replacement| text = text.gsub(replacement[0],replacement[1]) end text = text[1..-1] if text[0] == "+" return(text) end |
.sides(text) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/opl.rb', line 240 def self.sides(text) equation = text if equation.include?("<=") char = "<=" elsif equation.include?(">=") char = ">=" elsif equation.include?("<") char = "<" elsif equation.include?(">") char = ">" elsif equation.include?("=") char = "=" end sides = equation.split(char) {:lhs => sides[0], :rhs => sides[1]} end |
.split_equals(constraint) ⇒ Object
529 530 531 |
# File 'lib/opl.rb', line 529 def self.split_equals(constraint) [constraint.gsub("=", "<="), constraint.gsub("=", ">=")] end |
.split_equals_a(constraints) ⇒ Object
533 534 535 536 537 538 539 540 541 |
# File 'lib/opl.rb', line 533 def self.split_equals_a(constraints) constraints.map do |constraint| if (constraint.split("") & ["<=",">=","<",">"]).empty? self.split_equals(constraint) else constraint end end.flatten end |
.sub_forall(equation, indexvalues = {:indices => [], :values => []}) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/opl.rb', line 224 def self.sub_forall(equation, indexvalues={:indices => [], :values => []}) #in: "forall(i in (0..2), x[i] <= 5)" #out: ["x[0] <= 5", "x[1] <= 5", "x[2] <= 5"] return equation unless equation.include?("forall") foralls = (equation+"#").split("forall(").map{|ee|ee.split(")")[0..-2].join(")")}.find_all{|eee|eee!=""} constraints = [] if foralls.empty? return(equation) else foralls.each do |text| constraints << self.forall(text) end return(constraints.flatten) end end |
.sub_rhs_with_summed_constants(constraint) ⇒ Object
469 470 471 472 |
# File 'lib/opl.rb', line 469 def self.sub_rhs_with_summed_constants(constraint) rhs = self.sides(constraint)[:rhs] constraint.gsub(rhs, self.sum_constants(rhs)) end |
.sub_sum(equation, lp, indexvalues = {:indices => [], :values => []}) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/opl.rb', line 335 def self.sub_sum(equation, lp, indexvalues={:indices => [], :values => []}) #in: "sum(i in (0..3), x[i]) <= 100" #out: "x[0]+x[1]+x[2]+x[3] <= 100" sums = (equation+"#").split("sum(").map{|ee|ee.split(")")[0..-2].join(")")}.find_all{|eee|eee!=""}.find_all{|eeee|!eeee.include?("forall")} sums.each do |text| e = text unless indexvalues[:indices].empty? indexvalues[:indices].each_index do |i| index = indexvalues[:indices][i] value = indexvalues[:values][i].to_s e = e.gsub("("+index, "("+value) e = e.gsub(index+")", value+")") e = e.gsub("["+index, "["+value) e = e.gsub(index+"]", value+"]") e = e.gsub("=>"+index, "=>"+value) e = e.gsub("<="+index, "<="+value) e = e.gsub(">"+index, ">"+value) e = e.gsub("<"+index, "<"+value) e = e.gsub("="+index, "="+value) e = e.gsub("=> "+index, "=> "+value) e = e.gsub("<= "+index, "<= "+value) e = e.gsub("> "+index, "> "+value) e = e.gsub("< "+index, "< "+value) e = e.gsub("= "+index, "= "+value) end end equation = equation.gsub(text, e) result = self.sum(text, lp) equation = equation.gsub("sum("+text+")", result) end return(equation) end |
.substitute_data(text, lp) ⇒ Object
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 |
# File 'lib/opl.rb', line 646 def self.substitute_data(text, lp) helper = self potential_things_to_substitute = helper.variables(text, lp) data_names = lp.data.map{|d|d.name} things_to_substitute = {} data_values = {} lp.data.each do |data| dname = data.name dvalue = data.value targets = potential_things_to_substitute.find_all do |ptts| dname == ptts[0] end things_to_substitute[dname] = targets targets.each do |target| indices = target.scan(/\d+/).map{|ind|ind.to_i} indices = "" if indices.empty? if dvalue.is_a?(Array) value = dvalue.values_at_a(indices) else value = dvalue end data_values[dname+indices.to_s.gsub(",","][").gsub(" ","")] = value end end data_values.keys.each do |key| name = key value = data_values[key] text = text.gsub(name, value.to_s) end plus_minus = text.scan(/\+[\ ]+-/) plus_minus.each do |pm| text = text.gsub(pm,"-") end return(text) end |
.sum(text, lp, indexvalues = {:indices => [], :values => []}) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/opl.rb', line 273 def self.sum(text, lp, indexvalues={:indices => [], :values => []}) #in: "i in [0,1], j in [4,-5], 3x[i][j]" #out: "3x[0][4] + 3x[0][-5] + 3x[1][4] + 3x[1][-5]" text = text.sub_paren_with_array if (text.gsub(" ","")).scan(/\]\,/).size != text.scan(/in/).size raise "The following sum() constraint is incorrectly formatted: #{text}. Please see the examples in test.rb for sum() constraints. I suspect you are missing a comma somewhere." elsif (text.gsub(" ","").include?("=") || text.gsub(" ","").include?("<") || text.gsub(" ","").include?(">")) raise "The following sum() constraint cannot have a equalities in it (a.k.a. =, <, >): #{text}" end final_text = "" element = text.split(",")[-1].gsub(" ","") indices = text.scan(/[a-z] in/).map{|sc|sc[0]} input_indices = indexvalues[:indices] - indices if not input_indices.empty? input_values = input_indices.map{|ii|indexvalues[:values][indexvalues[:indices].index(ii)]} else input_values = [] end values = text.scan(/\s\[[\-\s\d+,]+\]/).map{|e|e.gsub(" ", "").scan(/[\-\d]+/)} indices += input_indices values += input_values index_value_pairs = indices.zip(values) variable = text.scan(/[a-z]\[/)[0].gsub("[","") coefficient_a = text.split(",")[-1].split("[")[0].scan(/\-?[\d\*]+[a-z]/) if coefficient_a.empty? if text.split(",")[-1].split("[")[0].include?("-") coefficient = "-1" else coefficient = "1" end else coefficient = coefficient_a[0].scan(/[\d\-]+/) end value_combinations = OPL::Helper.mass_product(values) value_combinations.each_index do |vc_index| value_combination = value_combinations[vc_index] e = element value_combination = [value_combination] unless value_combination.is_a?(Array) value_combination.each_index do |i| index = indices[i] value = value_combination[i] e = e.gsub("("+index, "("+value) e = e.gsub(index+")", value+")") e = e.gsub("["+index, "["+value) e = e.gsub(index+"]", value+"]") e = e.gsub("=>"+index, "=>"+value) e = e.gsub("<="+index, "<="+value) e = e.gsub(">"+index, ">"+value) e = e.gsub("<"+index, "<"+value) e = e.gsub("="+index, "="+value) e = e.gsub("=> "+index, "=> "+value) e = e.gsub("<= "+index, "<= "+value) e = e.gsub("> "+index, "> "+value) e = e.gsub("< "+index, "< "+value) e = e.gsub("= "+index, "= "+value) end e = "+"+e unless (coefficient.include?("-") || vc_index==0) final_text += e end final_text end |
.sum_constants(text) ⇒ Object
458 459 460 461 462 463 464 465 466 467 |
# File 'lib/opl.rb', line 458 def self.sum_constants(text) #in: "100+ 10-3" #out: "107" constants = self.get_constants(text)[:formatted] if constants.to_s.include?(".") constants.map{|c|c.to_f}.inject("+").to_s else constants.map{|c|c.to_i}.inject("+").to_s end end |
.sum_indices(constraint) ⇒ Object
543 544 545 546 547 548 549 550 551 552 553 |
# File 'lib/opl.rb', line 543 def self.sum_indices(constraint) #pieces_to_sub = constraint.scan(/[a-z]\[\d[\d\+\-]+\]/) pieces_to_sub = constraint.scan(/[a-z\]]\[\d[\d\+\-]+\]/) pieces_to_sub.each do |piece| characters_to_sum = piece.scan(/[\d\+\-]+/)[0] index_sum = self.sum_constants(characters_to_sum) new_piece = piece.gsub(characters_to_sum, index_sum) constraint = constraint.gsub(piece, new_piece) end return(constraint) end |
.sum_variables(formatted_constraint, lp) ⇒ Object
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 |
# File 'lib/opl.rb', line 577 def self.sum_variables(formatted_constraint, lp) #in: x + y - z + x[3] + z + y - z + x - y <= 0 #out: 2*x + y - z + x[3] <= 0 helper = self lhs = helper.sides(formatted_constraint)[:lhs] formatted_lhs = helper.add_ones(lhs, lp) vars = helper.variables(formatted_lhs, lp) coefs = helper.coefficients(formatted_lhs, lp) var_coef_hash = {} vars.each_index do |i| var = vars[i] coef = coefs[i] if var_coef_hash[var] var_coef_hash[var] += coefs[i].to_f else var_coef_hash[var] = coefs[i].to_f end end new_lhs = "" var_coef_hash.keys.each do |key| coef = var_coef_hash[key].to_s var = key coef = "+"+coef unless coef.include?("-") new_lhs += coef+"*"+var end if new_lhs[0] == "+" new_lhs = new_lhs[1..-1] end formatted_constraint.gsub(lhs, new_lhs) end |
.variables(text, lp) ⇒ Object
parameter is one side of the equation
381 382 383 384 |
# File 'lib/opl.rb', line 381 def self.variables(text, lp)#parameter is one side of the equation equation = self.add_ones(text, lp) equation.scan(/[a-z]+[\[\]\d]*/) end |