# encoding: utf-8 if RUBY_PLATFORM != "java" require 'rubygems' ; # pour (c)ruby puts "execution of require 'rubygems' done (because not java platform)" end # This require works either with ruby or with jruby-1.2 (which contains a dummy ffi gem) # require 'rubygems' require 'ffi' require 'pp' module Glpk extend FFI::Library # one should load the libglpk.so or like file in your lib path # To way to make this # # 1 - The best way: work on osx leopard, but not yet on every linux # version # # ffi_lib "glpk" # # 2 - Other way is to load the file directly, but you need to know were # it is located. FFI should provide an easier way to make this # One suppose the env variable contains the path to the glpk library # if %x(uname -a) =~ /Darwin/ libsuffix=".dylib" else libsuffix=".so" end # # glpk_lib_file=ENV["GLPK_LIB"] + "/libglpk" + libsuffix # # pp glpk_lib_file ffi_lib ENV["GLPK_LIB"] + "/libglpk" + libsuffix # constants (see glpk.h) # stead replace_in_selection: # rs -l {#define\s+(\w+)\s+(\d+)\s*(?:/\*(.+)\*/)?} {\1 = \2 # \3} GLP_MIN = 1 # minimization GLP_MAX = 2 # maximization # kind of structural variable: GLP_CV = 1 # continuous variable GLP_IV = 2 # integer variable GLP_BV = 3 # binary variable # type of auxiliary/structural variable: GLP_FR = 1 # free variable GLP_LO = 2 # variable with lower bound GLP_UP = 3 # variable with upper bound GLP_DB = 4 # double-bounded variable GLP_FX = 5 # fixed variable # 16/0/2015 (diam) the binded function is called `glp_create_prob_` so that # the ffi-glpk user function can be called `glp_create_prob` # # attach_function :glp_create_prob, [], :pointer # __OLD__ # attach_function :glp_delete_prob, [:pointer], :void # attach_function :glp_create_prob_, :glp_create_prob, [], :pointer attach_function :glp_delete_prob_, :glp_delete_prob, [:pointer], :void attach_function :glp_set_prob_name, [:pointer, :string], :pointer attach_function :glp_get_prob_name, [:pointer], :string attach_function :glp_set_obj_dir, [:pointer, :int], :int attach_function :glp_get_obj_dir, [:pointer], :int attach_function :glp_set_obj_coef,[:pointer, :int, :double], :void attach_function :glp_get_obj_coef, [:pointer, :int], :double attach_function :glp_get_obj_val, [:pointer], :double attach_function :glp_mip_obj_val, [:pointer], :double attach_function :glp_add_rows, [:pointer, :int], :int attach_function :glp_set_row_name, [:pointer, :int, :string], :int attach_function :glp_set_row_bnds, [:pointer, :int, :int, :double, :double], :int attach_function :glp_add_cols, [:pointer, :int], :int attach_function :glp_set_col_name, [:pointer, :int, :string], :void attach_function :glp_set_col_bnds, [:pointer,:int,:int,:double,:double],:void attach_function :glp_set_col_kind, [:pointer, :int, :int], :void attach_function :glp_get_col_prim, [:pointer, :int], :double attach_function :glp_mip_col_val, [:pointer, :int], :double attach_function :glp_load_matrix, [:pointer, :int, :pointer, :pointer, :pointer], :void attach_function :glp_simplex, [:pointer, :int], :void; attach_function :glp_intopt, [:pointer, :int], :void; # bug diam 16/02/2015: new problem "constructor" to cleanup attributes def glp_create_prob @ia_vector = Array.new @ja_vector = Array.new @ar_vector = Array.new glp_create_prob_ end # bug diam 16/02/2015: new problem "constructor" to cleanup attributes def glp_delete_prob glpk_pb glp_delete_prob_ glpk_pb @ia_vector = nil @ja_vector = nil @ar_vector = nil end # could become a method of a higher level shell over this Glpk module # def glp_set_aij_coef(idx_cst, idx_var, coef) @ia_vector = Array.new unless @ia_vector @ja_vector = Array.new unless @ja_vector @ar_vector = Array.new unless @ar_vector @ia_vector << idx_cst @ja_vector << idx_var @ar_vector << coef end def glp_load_matrix2 lp glp_load_matrix(lp, @ar_vector.length - 1, to_array_of_int32(@ia_vector), to_array_of_int32(@ja_vector), to_array_of_double(@ar_vector)); end def to_array_of_int32 tab tab_p = FFI::MemoryPointer.new(:int, tab.length) tab_p.put_array_of_int32(0, tab) return tab_p end def to_array_of_double tab tab_p = FFI::MemoryPointer.new(:double, tab.length) tab_p.put_array_of_double(0, tab) return tab_p end end #./