Class: Cbc::Problem

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-cbc/problem.rb

Defined Under Namespace

Classes: CCS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#crsObject

Returns the value of attribute crs.



4
5
6
# File 'lib/ruby-cbc/problem.rb', line 4

def crs
  @crs
end

#modelObject

Returns the value of attribute model.



4
5
6
# File 'lib/ruby-cbc/problem.rb', line 4

def model
  @model
end

#variable_indexObject

Returns the value of attribute variable_index.



4
5
6
# File 'lib/ruby-cbc/problem.rb', line 4

def variable_index
  @variable_index
end

Class Method Details

.crs_to_ccs(crs) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby-cbc/problem.rb', line 26

def self.crs_to_ccs(crs)
  nb_per_column = Array.new(crs.col_idx.max.to_i + 1, 0)
  nb_values = crs.values.size

  crs.col_idx.each { |col_idx| nb_per_column[col_idx] += 1 }

  ccs = CCS.new(Array.new(nb_per_column.size + 1), Array.new(nb_values), Array.new(nb_values))
  ccs.col_ptr[0] = 0
  idx = 0
  while idx < nb_per_column.size
    ccs.col_ptr[idx + 1] = ccs.col_ptr[idx] + nb_per_column[idx]
    idx += 1
  end

  cols_idx = ccs.col_ptr.clone
  row_idx = 0
  end_row_idx = crs.row_ptr.size - 1
  while row_idx < end_row_idx do
    current_idx = crs.row_ptr[row_idx]
    last_idx = crs.row_ptr[row_idx + 1] - 1
    while current_idx <= last_idx do
      col_idx = crs.col_idx[current_idx]
      ccs_col_idx = cols_idx[col_idx]
      cols_idx[col_idx] += 1
      ccs.row_idx[ccs_col_idx] = row_idx
      ccs.values[ccs_col_idx] = crs.values[current_idx]
      current_idx += 1
    end
    row_idx += 1
  end
  ccs
end

.finalizer(cbc_model, int_arrays, double_arrays) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/ruby-cbc/problem.rb', line 189

def self.finalizer(cbc_model, int_arrays, double_arrays)
  proc do
    Cbc_wrapper.Cbc_deleteModel(cbc_model)
    int_arrays.each { |ar| Cbc_wrapper.delete_intArray(ar) }
    double_arrays.each { |ar| Cbc_wrapper.delete_doubleArray(ar) }
  end
end

.from_compressed_row_storage(crs, continuous: false) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/ruby-cbc/problem.rb', line 11

def self.from_compressed_row_storage(crs, continuous: false)
  new.tap do |p|
    p.model = crs.model
    p.variable_index = crs.variable_index
    p.crs = crs
    p.create_cbc_problem(continuous: continuous)
  end
end

.from_model(model, continuous: false) ⇒ Object



6
7
8
9
# File 'lib/ruby-cbc/problem.rb', line 6

def self.from_model(model, continuous: false)
  crs = Util::CompressedRowStorage.from_model(model)
  from_compressed_row_storage(crs, continuous: continuous)
end

Instance Method Details

#best_boundObject

Returns the best know bound so far



177
178
179
# File 'lib/ruby-cbc/problem.rb', line 177

def best_bound
  Cbc_wrapper.Cbc_getBestPossibleObjValue(@cbc_model)
end

#create_cbc_problem(continuous: false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ruby-cbc/problem.rb', line 59

def create_cbc_problem(continuous: false)
  @int_arrays = []
  @double_arrays = []

  ccs = self.class.crs_to_ccs(@crs)
  objective = Array.new(ccs.nb_vars, 0)
  if model.objective
    model.objective.terms.each do |term|
      objective[@variable_index[term.var]] = term.mult
    end
  end

  @cbc_model = Cbc_wrapper.Cbc_newModel
  Cbc_wrapper.Cbc_loadProblem(
    @cbc_model, ccs.nb_vars, @crs.nb_constraints,
    to_int_array(ccs.col_ptr), to_int_array(ccs.row_idx),
    to_double_array(ccs.values), nil, nil, to_double_array(objective),
    nil, nil)


  # Segmentation errors when setting name
  # Cbc_wrapper.Cbc_setProblemName(@cbc_model, model.name) if model.name

  if model.objective
    obj_sense = model.objective.objective_function == Ilp::Objective::MINIMIZE ? 1 : -1
    Cbc_wrapper.Cbc_setObjSense(@cbc_model, obj_sense)
  end

  idx = 0
  while idx < @crs.nb_constraints do
    c = model.constraints[idx]
    set_constraint_bounds(c, idx)
    idx += 1
  end
  idx = 0
  while idx < ccs.nb_vars do
    v = model.vars[idx]
    if continuous
      Cbc_wrapper.Cbc_setContinuous(@cbc_model, idx)
    else
      case v.kind
      when Ilp::Var::INTEGER_KIND, Ilp::Var::BINARY_KIND
        Cbc_wrapper.Cbc_setInteger(@cbc_model, idx)
      when Ilp::Var::CONTINUOUS_KIND
        Cbc_wrapper.Cbc_setContinuous(@cbc_model, idx)
      end
    end
    Cbc_wrapper.Cbc_setColLower(@cbc_model, idx, v.lower_bound) unless v.lower_bound.nil?
    Cbc_wrapper.Cbc_setColUpper(@cbc_model, idx, v.upper_bound) unless v.upper_bound.nil?
    idx += 1
  end

  ObjectSpace.define_finalizer(self, self.class.finalizer(@cbc_model, @int_arrays, @double_arrays))

  @default_solve_params = {
    log: 0,
  }


end

#find_conflictObject



181
182
183
# File 'lib/ruby-cbc/problem.rb', line 181

def find_conflict
  @conflict_set ||= ConflictSolver.new(self).find_conflict
end

#find_conflict_varsObject



185
186
187
# File 'lib/ruby-cbc/problem.rb', line 185

def find_conflict_vars
  @conflict_vars ||= find_conflict.map(&:vars).flatten.uniq
end

#objective_valueObject



172
173
174
# File 'lib/ruby-cbc/problem.rb', line 172

def objective_value
  Cbc_wrapper.Cbc_getObjValue(@cbc_model)
end

#proven_infeasible?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/ruby-cbc/problem.rb', line 160

def proven_infeasible?
  Cbc_wrapper.Cbc_isProvenInfeasible(@cbc_model) == 1
end

#proven_optimal?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/ruby-cbc/problem.rb', line 156

def proven_optimal?
  Cbc_wrapper.Cbc_isProvenOptimal(@cbc_model) == 1
end

#set_constraint_bounds(c, idx) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ruby-cbc/problem.rb', line 120

def set_constraint_bounds(c, idx)
  case c.type
  when Ilp::Constraint::LESS_OR_EQ
    Cbc_wrapper.Cbc_setRowUpper(@cbc_model, idx, c.bound)
  when Ilp::Constraint::GREATER_OR_EQ
    Cbc_wrapper.Cbc_setRowLower(@cbc_model, idx, c.bound)
  when Ilp::Constraint::EQUALS
    Cbc_wrapper.Cbc_setRowUpper(@cbc_model, idx, c.bound)
    Cbc_wrapper.Cbc_setRowLower(@cbc_model, idx, c.bound)
  end
end

#set_time_limit(seconds) ⇒ Object



152
153
154
# File 'lib/ruby-cbc/problem.rb', line 152

def set_time_limit(seconds)
  @default_solve_params[:sec] = seconds
end

#solution_limit_reached?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/ruby-cbc/problem.rb', line 168

def solution_limit_reached?
  Cbc_wrapper.Cbc_isSolutionLimitReached(@cbc_model) == 1
end

#solve(params = {}) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/ruby-cbc/problem.rb', line 132

def solve(params = {})
  @default_solve_params.merge(params).each do |name, value|
    Cbc_wrapper.Cbc_setParameter(@cbc_model, name.to_s, value.to_s)
  end
  Cbc_wrapper.Cbc_solve(@cbc_model)
  @solution = Cbc_wrapper::DoubleArray.frompointer(Cbc_wrapper.Cbc_getColSolution(@cbc_model))
  @double_arrays << @solution
  @solution
end

#time_limit_reached?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/ruby-cbc/problem.rb', line 164

def time_limit_reached?
  Cbc_wrapper.Cbc_isSecondsLimitReached(@cbc_model) == 1
end

#value_of(var) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/ruby-cbc/problem.rb', line 142

def value_of(var)
  idx = @variable_index[var]
  return nil if idx.nil?
  if var.kind == Ilp::Var::CONTINUOUS_KIND
    @solution[idx]
  else
    @solution[idx].round
  end
end

#writeObject



197
198
199
# File 'lib/ruby-cbc/problem.rb', line 197

def write
  Cbc_wrapper.Cbc_writeMps(@cbc_model, "test")
end