class RAMS::Model
A Model
is a collection of:
* Variables * Constraints * An objective function and sense
An example of a simple model:
m = RAMS::Model.new x1 = m.variable(type: :binary) x2 = m.variable m.constrain(x1 + x2 <= 1)
Models can be maximized or minimized by different solvers.
m.sense = :max m.objective = (x1 + (2 * x2)) m.solver = :glpk m.verbose = true m.solve
Constants
- SOLVERS
Attributes
args[RW]
constraints[R]
objective[W]
sense[R]
solver[R]
variables[R]
verbose[RW]
Public Class Methods
new()
click to toggle source
# File lib/rams/model.rb, line 46 def initialize @solver = :glpk @sense = :max @objective = nil @verbose = false @args = [] @variables = {} @constraints = {} end
Public Instance Methods
constrain(constraint)
click to toggle source
# File lib/rams/model.rb, line 71 def constrain(constraint) constraints[constraint.name] = constraint end
objective()
click to toggle source
# File lib/rams/model.rb, line 75 def objective @objective || Expression.new(variables.values.first => 0) end
sense=(sense)
click to toggle source
# File lib/rams/model.rb, line 61 def sense=(sense) raise(ArgumentError, 'sense must be :min or :max') unless sense == :min || sense == :max @sense = sense end
solve()
click to toggle source
# File lib/rams/model.rb, line 79 def solve raise(ArgumentError, 'model has no variables') if variables.empty? raise(ArgumentError, 'model has no constraints') if constraints.empty? SOLVERS[solver].solve self end
solver=(solver)
click to toggle source
# File lib/rams/model.rb, line 56 def solver=(solver) raise(ArgumentError, "valid solvers: #{SOLVERS.keys.join(' ')}") if SOLVERS[solver].nil? @solver = solver end
to_lp()
click to toggle source
# File lib/rams/model.rb, line 85 def to_lp RAMS::Formatters::LP.format self end
variable(low: 0.0, high: nil, type: :continuous)
click to toggle source
# File lib/rams/model.rb, line 66 def variable(low: 0.0, high: nil, type: :continuous) v = Variable.new low: low, high: high, type: type variables[v.name] = v end