Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

GLPK/Julia

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

The GLPK.jl package allows use of the GLPK in julia in different ways. In an increasing level of abstraction these are: (a) directly (by wrapping the C API); (b) as interface to MathOptInterface or (c) as the "solver engine" of the algebraic modelling language JuMP.jl. For example to solve the problem `Maxx,y x + 2y` s.t. `x+y = 3` and `x,y >=0` you could write: (a) Using the GLPK C API wrapper: using Pkg; Pkg.add("GLPK") # Just once to install the packages using GLPK lp = GLPK.Prob() GLPK.set_obj_dir(lp, GLPK.MAX) GLPK.add_rows(lp, 1) GLPK.set_row_bnds(lp, 1, GLPK.FX, 3.0, 3.0) GLPK.add_cols(lp, 2) GLPK.set_col_bnds(lp, 1, GLPK.LO, 0.0, +Inf) GLPK.set_obj_coef(lp, 1, 1.0) GLPK.set_col_bnds(lp, 2, GLPK.LO, 0.0, +Inf) GLPK.set_obj_coef(lp, 2, 2.0) GLPK.set_mat_row(lp, 1, [1,2], [1,1]) # coefficients given in sparse form param = GLPK.SimplexParam() flag = GLPK.simplex(lp, param) # solves GLPK.get_obj_val(lp) # 6 GLPK.get_col_prim(lp, 1) # 0 GLPK.get_col_prim(lp, 2) # 3 (c) using the JuMP API: using Pkg; Pkg.add(["GLPK","JuMP"]) # Just once to install the packages using GLPK, JuMP model = Model(GLPK.Optimizer) set_optimizer_attribute(model, "msg_lev", GLPK.MSG_OFF) @variables model begin x >= 0 # could be parts of n-dimensional arrays, like x[p in plants, m in markets] y >= 0 end @constraints model begin bound1, x+y == 3 end @objective model Max begin x + 2*y end print(model) optimize!(model) objective_value(model) # 6 value(x) # 0 value(y) # 3