Class: Solver

Inherits:
Object show all
Defined in:
lib/solvers/solver.rb

Direct Known Subclasses

Cbc, Glpk, Gurobi, Scip

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, options) ⇒ Solver

Returns a new instance of Solver.

Raises:

  • (StandardError)


5
6
7
8
9
10
11
# File 'lib/solvers/solver.rb', line 5

def initialize(filename, options)
  @options = options
  @filename = filename
  @outfile = get_output_filename
  raise StandardError.new("Couldn't find solver #{executable}!") if `which #{executable}`.length == 0
  @solver_exists = true
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/solvers/solver.rb', line 2

def options
  @options
end

#outfileObject (readonly)

Returns the value of attribute outfile.



2
3
4
# File 'lib/solvers/solver.rb', line 2

def outfile
  @outfile
end

#unsuccessfulObject

Returns the value of attribute unsuccessful.



3
4
5
# File 'lib/solvers/solver.rb', line 3

def unsuccessful
  @unsuccessful
end

Class Method Details

.exists?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/solvers/solver.rb', line 41

def self.exists?
  return `which #{self.executable}`.length != 0
end

Instance Method Details

#exec(command) ⇒ Object



25
26
27
# File 'lib/solvers/solver.rb', line 25

def exec(command)
  Rulp.exec(command)
end

#executableObject



21
22
23
# File 'lib/solvers/solver.rb', line 21

def executable
  self.class.executable
end

#get_output_filenameObject



13
14
15
# File 'lib/solvers/solver.rb', line 13

def get_output_filename
  "/tmp/rulp-#{Random.rand(0..1000)}.sol"
end

#next_pipeObject



45
46
47
48
49
50
51
52
# File 'lib/solvers/solver.rb', line 45

def next_pipe
  filename = "./tmp/_rulp_pipe"
  file_index = 1
  file_index += 1 while File.exists?("#{filename}_#{file_index}")
  pipe = "#{filename}_#{file_index}"
  `mkfifo #{pipe}`
  pipe
end

#remove_lp_fileObject



29
30
31
# File 'lib/solvers/solver.rb', line 29

def remove_lp_file
  FileUtils.rm(@filename)
end

#remove_sol_fileObject



33
34
35
# File 'lib/solvers/solver.rb', line 33

def remove_sol_file
  FileUtils.rm(@outfile)
end

#solver_exists?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/solvers/solver.rb', line 37

def solver_exists?
  @solver_exists || false
end

#store_results(variables) ⇒ Object



17
18
19
# File 'lib/solvers/solver.rb', line 17

def store_results(variables)
  puts "Not yet implemented"
end

#with_pipe(pipe) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/solvers/solver.rb', line 54

def with_pipe(pipe)
  output = open(pipe, 'w+')
  thread = Thread.new{
    yield output
    output.flush
  }
  return thread, output
end