Class: ORTools::ProductCst

Inherits:
LinearExpr show all
Defined in:
lib/or_tools/product_cst.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from LinearExpr

#*, #+, #-, #-@, #/, #<=, #==, #>=, #coeffs, #coerce, #inspect, #solution_value

Constructor Details

#initialize(expr, coef) ⇒ ProductCst

Returns a new instance of ProductCst.

Raises:

  • (TypeError)


5
6
7
8
9
10
# File 'lib/or_tools/product_cst.rb', line 5

def initialize(expr, coef)
  @expr = cast_to_lin_exp(expr)
  # TODO improve message
  raise TypeError, "expected numeric" unless coef.is_a?(Numeric)
  @coef = coef
end

Instance Attribute Details

#coefObject (readonly)

Returns the value of attribute coef.



3
4
5
# File 'lib/or_tools/product_cst.rb', line 3

def coef
  @coef
end

#exprObject (readonly)

Returns the value of attribute expr.



3
4
5
# File 'lib/or_tools/product_cst.rb', line 3

def expr
  @expr
end

Instance Method Details

#add_self_to_coeff_map_or_stack(coeffs, multiplier, stack) ⇒ Object



24
25
26
27
28
29
# File 'lib/or_tools/product_cst.rb', line 24

def add_self_to_coeff_map_or_stack(coeffs, multiplier, stack)
  current_multiplier = multiplier * @coef
  if current_multiplier
    stack << [current_multiplier, @expr]
  end
end

#cast_to_lin_exp(v) ⇒ Object



31
32
33
# File 'lib/or_tools/product_cst.rb', line 31

def cast_to_lin_exp(v)
  v.is_a?(Numeric) ? Constant.new(v) : v
end

#to_sObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/or_tools/product_cst.rb', line 12

def to_s
  if @coef == -1
    "-#{@expr}"
  else
    expr = @expr.to_s
    if expr.include?("+") || expr.include?("-")
      expr = "(#{expr})"
    end
    "#{@coef} * #{expr}"
  end
end