class RAMS::Solvers::Solver

Generic solver interface

Public Instance Methods

solve(model) click to toggle source
# File lib/rams/solvers/solver.rb, line 8
def solve(model)
  model_file = write_model_file model
  begin
    get_solution model, model_file.path
  ensure
    model_file.unlink
  end
end

Private Instance Methods

call_solver(model, model_path, solution_path) click to toggle source

rubocop:disable MethodLength

# File lib/rams/solvers/solver.rb, line 42
def call_solver(model, model_path, solution_path)
  command = solver_command(model_path, solution_path, model.args)
  _, stdout, stderr, exit_code = Open3.popen3(*command)

  begin
    output = stdout.gets(nil) || ''
    error = output + (stderr.gets(nil) || '')
    puts output if model.verbose && output != ''
    raise error unless exit_code.value == 0
    return output
  ensure
    stdout.close
    stderr.close
  end
end
get_solution(model, model_path) click to toggle source
# File lib/rams/solvers/solver.rb, line 26
def get_solution(model, model_path)
  solution_path = model_path + '.sol'
  begin
    solve_and_parse model, model_path, solution_path
  ensure
    File.delete(solution_path) if File.exist?(solution_path)
  end
end
parse_dual(_model, _lines) click to toggle source
# File lib/rams/solvers/solver.rb, line 85
def parse_dual(_model, _lines)
  raise NotImplementedError
end
parse_objective(_model, _lines) click to toggle source
# File lib/rams/solvers/solver.rb, line 77
def parse_objective(_model, _lines)
  raise NotImplementedError
end
parse_primal(_model, _lines) click to toggle source
# File lib/rams/solvers/solver.rb, line 81
def parse_primal(_model, _lines)
  raise NotImplementedError
end
parse_solution(model, solution_text) click to toggle source
# File lib/rams/solvers/solver.rb, line 63
def parse_solution(model, solution_text)
  lines = solution_text.split "\n"
  RAMS::Solution.new(
    parse_status(model, lines),
    parse_objective(model, lines),
    parse_primal(model, lines),
    parse_dual(model, lines)
  )
end
parse_status(_model, _lines) click to toggle source
# File lib/rams/solvers/solver.rb, line 73
def parse_status(_model, _lines)
  raise NotImplementedError
end
solve_and_parse(model, model_path, solution_path) click to toggle source
# File lib/rams/solvers/solver.rb, line 35
def solve_and_parse(model, model_path, solution_path)
  call_solver model, model_path, solution_path
  return RAMS::Solution.new(:unknown, nil, {}, {}) unless File.exist? solution_path
  parse_solution model, File.read(solution_path)
end
solver_command(_model_file, _solution_path, _args) click to toggle source

rubocop:enable MethodLength

# File lib/rams/solvers/solver.rb, line 59
def solver_command(_model_file, _solution_path, _args)
  raise NotImplementedError
end
write_model_file(model) click to toggle source
# File lib/rams/solvers/solver.rb, line 19
def write_model_file(model)
  model_file = Tempfile.new ['', '.lp']
  model_file.write model.to_lp
  model_file.close
  model_file
end