Class: Rglpk::Problem

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProblem

Returns a new instance of Problem.


65
66
67
68
69
70
71
72
73
# File 'lib/rglpk.rb', line 65

def initialize
  @lp = Glpk_wrapper.glp_create_prob
  @obj = ObjectiveFunction.new(self)
  @rows = RowArray.new
  @cols = ColArray.new
  Glpk_wrapper.glp_create_index(@lp)
  
  ObjectSpace.define_finalizer(self, self.class.finalizer(@lp))
end

Instance Attribute Details

#colsObject

Returns the value of attribute cols


63
64
65
# File 'lib/rglpk.rb', line 63

def cols
  @cols
end

#lpObject

Returns the value of attribute lp


63
64
65
# File 'lib/rglpk.rb', line 63

def lp
  @lp
end

#objObject

Returns the value of attribute obj


63
64
65
# File 'lib/rglpk.rb', line 63

def obj
  @obj
end

#rowsObject

Returns the value of attribute rows


63
64
65
# File 'lib/rglpk.rb', line 63

def rows
  @rows
end

Class Method Details

.finalizer(lp) ⇒ Object


75
76
77
78
79
80
# File 'lib/rglpk.rb', line 75

def self.finalizer(lp)
  proc do
    Glpk_wrapper.glp_delete_index(lp)
    Glpk_wrapper.glp_delete_prob(lp)
  end
end

Instance Method Details

#add_colObject


108
109
110
111
112
113
# File 'lib/rglpk.rb', line 108

def add_col
  Glpk_wrapper.glp_add_cols(@lp, 1)
  new_column = Column.new(self, @cols.size + 1)
  @cols.send(:push, new_column)
  new_column
end

#add_cols(n) ⇒ Object


115
116
117
118
119
120
# File 'lib/rglpk.rb', line 115

def add_cols(n)
  Glpk_wrapper.glp_add_cols(@lp, n)
  s = @cols.size
  n.times{|i| @cols.send(:push, Column.new(self, s + i + 1))}
  @cols
end

#add_rowObject


94
95
96
97
98
99
# File 'lib/rglpk.rb', line 94

def add_row
  Glpk_wrapper.glp_add_rows(@lp, 1)
  new_row = Row.new(self, @rows.size + 1)
  @rows.send(:push, new_row)
  new_row
end

#add_rows(n) ⇒ Object


101
102
103
104
105
106
# File 'lib/rglpk.rb', line 101

def add_rows(n)
  Glpk_wrapper.glp_add_rows(@lp, n)
  s = @rows.size
  n.times{|i| @rows.send(:push, Row.new(self, s + i + 1))}
  @rows
end

#del_cols(a) ⇒ Object


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rglpk.rb', line 141

def del_cols(a)
  # Ensure the array of rows to delete is sorted and unique.
  a = a.sort.uniq

  r = Glpk_wrapper.new_intArray(a.size + 1)
  a.each_with_index{|n, i| Glpk_wrapper.intArray_setitem(r, i + 1, n)}
  Glpk_wrapper.glp_del_cols(@lp, a.size, r)
  Glpk_wrapper.delete_intArray(r)

  a.each do |n|
    @cols.send(:delete_at, n)
    a.each_with_index do |nn, i|
      a[i] -= 1
    end
  end
  @cols.each_with_index{|c, i| c.j = i + 1}
  a
end

#del_rows(a) ⇒ Object


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rglpk.rb', line 122

def del_rows(a)
  # Ensure the array of rows to delete is sorted and unique.
  a = a.sort.uniq

  r = Glpk_wrapper.new_intArray(a.size + 1)
  a.each_with_index{|n, i| Glpk_wrapper.intArray_setitem(r, i + 1, n)}
  Glpk_wrapper.glp_del_rows(@lp, a.size, r)
  Glpk_wrapper.delete_intArray(r)

  a.each do |n|
    @rows.send(:delete_at, n)
    a.each_with_index do |nn, i|
      a[i] -= 1
    end
  end
  @rows.each_with_index{|r, i| r.i = i + 1}
  a
end

#mip(options = {}) ⇒ Object


212
213
214
215
216
217
218
219
220
221
# File 'lib/rglpk.rb', line 212

def mip(options = {})
  parm = Glpk_wrapper::Glp_iocp.new
  Glpk_wrapper.glp_init_iocp(parm)
  
  # Default to errors only temrinal output.
  parm.msg_lev = GLP_MSG_ERR
  
  apply_options_to_parm(options, parm)
  Glpk_wrapper.glp_intopt(@lp, parm)
end

#mip_statusObject


223
224
225
# File 'lib/rglpk.rb', line 223

def mip_status
  Glpk_wrapper.glp_mip_status(@lp)
end

#nameObject


86
87
88
# File 'lib/rglpk.rb', line 86

def name
  Glpk_wrapper.glp_get_prob_name(@lp)
end

#name=(n) ⇒ Object


82
83
84
# File 'lib/rglpk.rb', line 82

def name=(n)
  Glpk_wrapper.glp_set_prob_name(@lp, n)
end

#nzObject


90
91
92
# File 'lib/rglpk.rb', line 90

def nz
  Glpk_wrapper.glp_get_num_nz(@lp)
end

#set_matrix(v) ⇒ Object


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rglpk.rb', line 160

def set_matrix(v)
  nc = Glpk_wrapper.glp_get_num_cols(@lp)
  
  ia = Glpk_wrapper.new_intArray(v.size + 1)
  ja = Glpk_wrapper.new_intArray(v.size + 1)
  ar = Glpk_wrapper.new_doubleArray(v.size + 1)
  
  v.each_with_index do |x, y|
    rn = (y + nc) / nc
    cn = (y % nc) + 1

    Glpk_wrapper.intArray_setitem(ia, y + 1, rn)
    Glpk_wrapper.intArray_setitem(ja, y + 1, cn)
    Glpk_wrapper.doubleArray_setitem(ar, y + 1, x)
  end
  
  Glpk_wrapper.glp_load_matrix(@lp, v.size, ia, ja, ar)
  
  Glpk_wrapper.delete_intArray(ia)
  Glpk_wrapper.delete_intArray(ja)
  Glpk_wrapper.delete_doubleArray(ar)
end

#simplex(options = {}) ⇒ Object


197
198
199
200
201
202
203
204
205
206
# File 'lib/rglpk.rb', line 197

def simplex(options = {})
  parm = Glpk_wrapper::Glp_smcp.new
  Glpk_wrapper.glp_init_smcp(parm)

  # Default to errors only temrinal output.
  parm.msg_lev = GLP_MSG_ERR
  
  apply_options_to_parm(options, parm)
  Glpk_wrapper.glp_simplex(@lp, parm)
end

#statusObject


208
209
210
# File 'lib/rglpk.rb', line 208

def status
  Glpk_wrapper.glp_get_status(@lp)
end

#write_lp(filename) ⇒ Object


228
229
230
# File 'lib/rglpk.rb', line 228

def write_lp(filename)
  Glpk_wrapper.glp_write_lp(@lp, nil, filename)
end