Class: Quicopt::LookupTable

Inherits:
Object
  • Object
show all
Defined in:
lib/quicopt/structured.rb

Overview

A table of data declared by Model#lookup_table, to be indexed with expressions: table[i] for a vector, table[i, j] for a matrix, positions counted from 0.

price  = m.lookup_table("price", [3.0, 1.0, 4.0, 1.5])
choice = m.int_var(0, 3, "choice")
price[choice]   # the price of the option the service picks
price[2]        # 4.0, the number itself

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, values, shape) ⇒ LookupTable

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Declared with Model#lookup_table, not built directly.



262
263
264
265
266
267
268
# File 'lib/quicopt/structured.rb', line 262

def initialize(model, name, values, shape)
  @model = model
  @name = name
  @values = values
  @shape = shape.freeze
  @reads = {}
end

Instance Attribute Details

#model ⇒ Model (readonly)

Returns the model that declared this table.

Returns:

  • (Model) —

    the model that declared this table



250
251
252
# File 'lib/quicopt/structured.rb', line 250

def model
  @model
end

#name ⇒ String (readonly)

Returns the table's name.

Returns:

  • (String) —

    the table's name



252
253
254
# File 'lib/quicopt/structured.rb', line 252

def name
  @name
end

#shape ⇒ Array<Integer> (readonly)

Returns the extent of each dimension: [length] or [rows, columns].

Returns:

  • (Array<Integer>) —

    the extent of each dimension: [length] or [rows, columns]



257
258
259
# File 'lib/quicopt/structured.rb', line 257

def shape
  @shape
end

#values ⇒ Array<Float>+ (readonly)

Returns the entries, as given.

Returns:

  • (Array<Float>, Array<Array<Float>>) —

    the entries, as given



254
255
256
# File 'lib/quicopt/structured.rb', line 254

def values
  @values
end

Instance Method Details

#[](*index) ⇒ Float, Expression

The entry at the given positions. With whole numbers only, that is the number itself. With an expression among them — route.item_at(k), an integer variable — it is an expression whose value is the entry at the positions the service chooses. A position that is not a whole number is rounded, and one outside the table is moved to its nearest end.

Parameters:

Returns:

Raises:

  • (ArgumentError) —

    on the wrong number of positions, or a whole number outside the table

  • (TypeError) —

    on a position that is neither a whole number nor an expression



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/quicopt/structured.rb', line 282

def [](*index)
  unless index.size == @shape.size
    raise ArgumentError, "#{owner} has #{@shape.size} dimension(s), so it takes #{@shape.size} " \
                         "position(s), got #{index.size}"
  end
  index.zip(@shape).each do |k, extent|
    if k.is_a?(Integer)
      Positions.check(k, extent, "position", owner)
    elsif k.is_a?(Numeric)
      raise TypeError, "#{owner}: a position is a whole number or an expression, got #{k}"
    end
  end
  return index.reduce(@values) { |row, k| row[k] } if index.all?(Integer)

  read(index.map { |k| Expression.cast(k) })
end

#inspect ⇒ String

Returns name and shape.

Returns:

  • (String) —

    name and shape



300
301
302
# File 'lib/quicopt/structured.rb', line 300

def inspect
  "#<Quicopt::LookupTable #{@name} (#{@shape.join(' × ')})>"
end