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



111
112
113
114
115
116
# File 'lib/rglpk.rb', line 111

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



118
119
120
121
122
123
124
125
126
# File 'lib/rglpk.rb', line 118

def add_cols(n)
  Glpk_wrapper.glp_add_cols(@lp, n)
  s = @cols.size
  n.times.map do |i|
    new_col = Column.new(self, s + i + 1)
    @cols.send(:push, new_col)
    new_col
  end
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
107
108
109
# File 'lib/rglpk.rb', line 101

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

#del_cols(a) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rglpk.rb', line 147

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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rglpk.rb', line 128

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



218
219
220
221
222
223
224
225
226
227
# File 'lib/rglpk.rb', line 218

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



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

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rglpk.rb', line 166

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



203
204
205
206
207
208
209
210
211
212
# File 'lib/rglpk.rb', line 203

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



214
215
216
# File 'lib/rglpk.rb', line 214

def status
  Glpk_wrapper.glp_get_status(@lp)
end

#write_lp(filename) ⇒ Object



234
235
236
# File 'lib/rglpk.rb', line 234

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