naev 0.11.5
nlua_linopt.c File Reference

Handles Linear linoptization in Lua. More...

#include "nlua_linopt.h"
#include "log.h"
#include "nstring.h"
#include "nluadef.h"

Go to the source code of this file.

Data Structures

struct  LuaLinOpt_t
 Our cute little linear program wrapper. More...
 

Macros

#define LINOPT_MAX_TM   1000
 
#define METH_DEF   GLP_PRIMAL
 
#define PRICING_DEF   GLP_PT_PSE
 
#define R_TEST_DEF   GLP_RT_HAR
 
#define PRESOLVE_DEF   GLP_OFF
 
#define BR_TECH_DEF   GLP_BR_DTH
 
#define BT_TECH_DEF   GLP_BT_BLB
 
#define PP_TECH_DEF   GLP_PP_ALL
 
#define FP_HEUR_DEF   GLP_OFF
 
#define GMI_CUTS_DEF   GLP_OFF
 
#define MIR_CUTS_DEF   GLP_OFF
 
#define COV_CUTS_DEF   GLP_OFF
 
#define CLQ_CUTS_DEF   GLP_OFF
 
#define STRCHK(val, ret)
 
#define GETOPT_IOCP(name, func, def)
 
#define GETOPT_SMCP(name, func, def)
 

Functions

static int linoptL_gc (lua_State *L)
 Frees a linopt.
 
static int linoptL_eq (lua_State *L)
 Compares two linopts to see if they are the same.
 
static int linoptL_new (lua_State *L)
 Opens a new linopt.
 
static int linoptL_size (lua_State *L)
 Adds columns to the linear program.
 
static int linoptL_addcols (lua_State *L)
 Adds columns to the linear program.
 
static int linoptL_addrows (lua_State *L)
 Adds rows to the linear program.
 
static int linoptL_setcol (lua_State *L)
 Adds an optimization column.
 
static int linoptL_setrow (lua_State *L)
 Adds an optimization row.
 
static int linoptL_loadmatrix (lua_State *L)
 Loads the entire matrix for the linear program.
 
static int linoptL_solve (lua_State *L)
 Solves the linear optimization problem.
 
static int linoptL_readProblem (lua_State *L)
 Reads an optimization problem from a file for debugging purposes.
 
static int linoptL_writeProblem (lua_State *L)
 Writes an optimization problem to a file for debugging purposes.
 
int nlua_loadLinOpt (nlua_env env)
 Loads the linopt library.
 
LuaLinOpt_tlua_tolinopt (lua_State *L, int ind)
 Lua bindings to interact with linopts.
 
LuaLinOpt_tluaL_checklinopt (lua_State *L, int ind)
 Gets linopt at index or raises error if there is no linopt at index.
 
LuaLinOpt_tlua_pushlinopt (lua_State *L, LuaLinOpt_t linopt)
 Pushes a linopt on the stack.
 
int lua_islinopt (lua_State *L, int ind)
 Checks to see if ind is a linopt.
 
static const char * linopt_status (int retval)
 
static const char * linopt_error (int retval)
 
static int opt_meth (const char *str, int def)
 
static int opt_pricing (const char *str, int def)
 
static int opt_r_test (const char *str, int def)
 
static int opt_br_tech (const char *str, int def)
 
static int opt_bt_tech (const char *str, int def)
 
static int opt_pp_tech (const char *str, int def)
 
static int opt_onoff (const char *str, int def)
 

Variables

static const luaL_Reg linoptL_methods []
 

Detailed Description

Handles Linear linoptization in Lua.

Definition in file nlua_linopt.c.

Macro Definition Documentation

◆ BR_TECH_DEF

#define BR_TECH_DEF   GLP_BR_DTH

Definition at line 492 of file nlua_linopt.c.

◆ BT_TECH_DEF

#define BT_TECH_DEF   GLP_BT_BLB

Definition at line 493 of file nlua_linopt.c.

◆ CLQ_CUTS_DEF

#define CLQ_CUTS_DEF   GLP_OFF

Definition at line 501 of file nlua_linopt.c.

◆ COV_CUTS_DEF

#define COV_CUTS_DEF   GLP_OFF

Definition at line 500 of file nlua_linopt.c.

◆ FP_HEUR_DEF

#define FP_HEUR_DEF   GLP_OFF

Definition at line 496 of file nlua_linopt.c.

◆ GETOPT_IOCP

#define GETOPT_IOCP ( name,
func,
def )
Value:
do {lua_getfield(L,2,#name); parm_iocp.name = func( luaL_optstring(L,-1,NULL), def ); lua_pop(L,1); } while (0)

Definition at line 581 of file nlua_linopt.c.

◆ GETOPT_SMCP

#define GETOPT_SMCP ( name,
func,
def )
Value:
do {lua_getfield(L,2,#name); parm_smcp.name = func( luaL_optstring(L,-1,NULL), def ); lua_pop(L,1); } while (0)

Definition at line 582 of file nlua_linopt.c.

◆ GMI_CUTS_DEF

#define GMI_CUTS_DEF   GLP_OFF

Definition at line 498 of file nlua_linopt.c.

◆ LINOPT_MAX_TM

#define LINOPT_MAX_TM   1000

Maximum time to optimize (in ms). Applied to linear relaxation and MIP independently.

Definition at line 25 of file nlua_linopt.c.

◆ METH_DEF

#define METH_DEF   GLP_PRIMAL

Definition at line 487 of file nlua_linopt.c.

◆ MIR_CUTS_DEF

#define MIR_CUTS_DEF   GLP_OFF

Definition at line 499 of file nlua_linopt.c.

◆ PP_TECH_DEF

#define PP_TECH_DEF   GLP_PP_ALL

Definition at line 494 of file nlua_linopt.c.

◆ PRESOLVE_DEF

#define PRESOLVE_DEF   GLP_OFF

Definition at line 490 of file nlua_linopt.c.

◆ PRICING_DEF

#define PRICING_DEF   GLP_PT_PSE

Definition at line 488 of file nlua_linopt.c.

◆ R_TEST_DEF

#define R_TEST_DEF   GLP_RT_HAR

Definition at line 489 of file nlua_linopt.c.

◆ STRCHK

#define STRCHK ( val,
ret )
Value:
if (strcmp(str,(val))==0) return (ret);

Definition at line 515 of file nlua_linopt.c.

Function Documentation

◆ linopt_error()

static const char * linopt_error ( int retval)
static

Definition at line 441 of file nlua_linopt.c.

◆ linopt_status()

static const char * linopt_status ( int retval)
static

Definition at line 422 of file nlua_linopt.c.

◆ linoptL_addcols()

static int linoptL_addcols ( lua_State * L)
static

Adds columns to the linear program.

Lua function parameter: LinOpt lp Linear program to modify. Lua function parameter: number cols Number of columns to add.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: add_cols

Definition at line 240 of file nlua_linopt.c.

◆ linoptL_addrows()

static int linoptL_addrows ( lua_State * L)
static

Adds rows to the linear program.

Lua function parameter: LinOpt lp Linear program to modify. Lua function parameter: number rows Number of rows to add.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: add_rows

Definition at line 256 of file nlua_linopt.c.

◆ linoptL_eq()

static int linoptL_eq ( lua_State * L)
static

Compares two linopts to see if they are the same.

Lua function parameter: Optim d1 Optim 1 to compare. Lua function parameter: Optim d2 Optim 2 to compare. Lua return parameter: boolean true if both linopts are the same.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: __eq

Definition at line 169 of file nlua_linopt.c.

◆ linoptL_gc()

static int linoptL_gc ( lua_State * L)
static

Frees a linopt.

Lua function parameter: Optim linopt Optim to free.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: __gc

Definition at line 154 of file nlua_linopt.c.

◆ linoptL_loadmatrix()

static int linoptL_loadmatrix ( lua_State * L)
static

Loads the entire matrix for the linear program.

Lua function parameter: LinOpt lp Linear program to modify. Lua function parameter: number row_indices Indices of the rows. Lua function parameter: number col_indices Indices of the columns. Lua function parameter: number coefficients Values of the coefficients.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: load_matrix

Definition at line 375 of file nlua_linopt.c.

◆ linoptL_new()

static int linoptL_new ( lua_State * L)
static

Opens a new linopt.

Lua function parameter:[opt=nil] string name Name of the optimization program. Lua function parameter: number cols Number of columns in the optimization program. Lua function parameter: number rows Number of rows in the optimization program. Lua function parameter:[opt=false] boolean maximize Whether to maximize (rather than minimize) the function. Lua return parameter: Optim New linopt object.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: new

Definition at line 188 of file nlua_linopt.c.

◆ linoptL_readProblem()

static int linoptL_readProblem ( lua_State * L)
static

Reads an optimization problem from a file for debugging purposes.

Lua function parameter: string fname Path to the file. Lua function parameter:[opt=false] boolean glpk_format Whether the program is in GLPK format instead of MPS format. Lua function parameter:[opt=false] boolean maximize Whether to maximize (rather than minimize) the function. Lua return parameter: LinOpt Linear program in the file.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: read_problem

Definition at line 730 of file nlua_linopt.c.

◆ linoptL_setcol()

static int linoptL_setcol ( lua_State * L)
static

Adds an optimization column.

Lua function parameter: LinOpt lp Linear program to modify. Lua function parameter: number index Index of the column to set. Lua function parameter: string name Name of the column being added. Lua function parameter: number coefficient Coefficient of the objective function being added. Lua function parameter:[opt="real"] string kind Kind of the column being added. Can be either "real", "integer", or "binary". Lua function parameter:[opt=nil] number lb Lower bound of the column. Lua function parameter:[opt=nil] number ub Upper bound of the column.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: set_col

Definition at line 277 of file nlua_linopt.c.

◆ linoptL_setrow()

static int linoptL_setrow ( lua_State * L)
static

Adds an optimization row.

Lua function parameter: LinOpt lp Linear program to modify. Lua function parameter: number index Index of the row to set. Lua function parameter: string name Name of the row being added. Lua function parameter:[opt=nil] number lb Lower bound of the row. Lua function parameter:[opt=nil] number ub Upper bound of the row.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: set_row

Definition at line 333 of file nlua_linopt.c.

◆ linoptL_size()

static int linoptL_size ( lua_State * L)
static

Adds columns to the linear program.

Lua function parameter: LinOpt lp Linear program to modify. Lua return parameter: number Number of columns in the linear program. Lua return parameter: number Number of rows in the linear program.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: size

Definition at line 225 of file nlua_linopt.c.

◆ linoptL_solve()

static int linoptL_solve ( lua_State * L)
static

Solves the linear optimization problem.

Lua function parameter: LinOpt lp Linear program to modify. Lua return parameter: number The value of the primal funcation. Lua return parameter: table Table of column values.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: solve

Definition at line 591 of file nlua_linopt.c.

◆ linoptL_writeProblem()

static int linoptL_writeProblem ( lua_State * L)
static

Writes an optimization problem to a file for debugging purposes.

Lua function parameter: LinOpt lp Linear program to write. Lua function parameter: string fname Path to write the program to. Lua function parameter:[opt=false] boolean glpk_format Whether to write the program in GLPK format instead of MPS format.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: write_problem

Definition at line 765 of file nlua_linopt.c.

◆ lua_islinopt()

int lua_islinopt ( lua_State * L,
int ind )

Checks to see if ind is a linopt.

Parameters
LLua state to check.
indIndex position to check.
Returns
1 if ind is a linopt.

Definition at line 132 of file nlua_linopt.c.

◆ lua_pushlinopt()

LuaLinOpt_t * lua_pushlinopt ( lua_State * L,
LuaLinOpt_t linopt )

Pushes a linopt on the stack.

Parameters
LLua state to push linopt into.
linoptOptim to push.
Returns
Newly pushed linopt.

Definition at line 116 of file nlua_linopt.c.

◆ lua_tolinopt()

LuaLinOpt_t * lua_tolinopt ( lua_State * L,
int ind )

Lua bindings to interact with linopts.

Lua module: linopt

Gets linopt at index.

Parameters
LLua state to get linopt from.
indIndex position to find the linopt.
Returns
Optim found at the index in the state.

Definition at line 89 of file nlua_linopt.c.

◆ luaL_checklinopt()

LuaLinOpt_t * luaL_checklinopt ( lua_State * L,
int ind )

Gets linopt at index or raises error if there is no linopt at index.

Parameters
LLua state to get linopt from.
indIndex position to find linopt.
Returns
Optim found at the index in the state.

Definition at line 101 of file nlua_linopt.c.

◆ nlua_loadLinOpt()

int nlua_loadLinOpt ( nlua_env env)

Loads the linopt library.

Parameters
envEnvironment to load linopt library into.
Returns
0 on success.

Definition at line 71 of file nlua_linopt.c.

◆ opt_br_tech()

static int opt_br_tech ( const char * str,
int def )
static

Definition at line 541 of file nlua_linopt.c.

◆ opt_bt_tech()

static int opt_bt_tech ( const char * str,
int def )
static

Definition at line 552 of file nlua_linopt.c.

◆ opt_meth()

static int opt_meth ( const char * str,
int def )
static

Definition at line 516 of file nlua_linopt.c.

◆ opt_onoff()

static int opt_onoff ( const char * str,
int def )
static

Definition at line 571 of file nlua_linopt.c.

◆ opt_pp_tech()

static int opt_pp_tech ( const char * str,
int def )
static

Definition at line 562 of file nlua_linopt.c.

◆ opt_pricing()

static int opt_pricing ( const char * str,
int def )
static

Definition at line 525 of file nlua_linopt.c.

◆ opt_r_test()

static int opt_r_test ( const char * str,
int def )
static

Definition at line 533 of file nlua_linopt.c.

Variable Documentation

◆ linoptL_methods

const luaL_Reg linoptL_methods[]
static
Initial value:
= {
{ "__gc", linoptL_gc },
{ "__eq", linoptL_eq },
{ "new", linoptL_new },
{ "size", linoptL_size },
{ "add_cols", linoptL_addcols },
{ "add_rows", linoptL_addrows },
{ "set_col", linoptL_setcol },
{ "set_row", linoptL_setrow },
{ "load_matrix", linoptL_loadmatrix },
{ "solve", linoptL_solve },
{ "read_problem", linoptL_readProblem },
{ "write_problem", linoptL_writeProblem },
{0,0}
}
static int linoptL_loadmatrix(lua_State *L)
Loads the entire matrix for the linear program.
static int linoptL_setcol(lua_State *L)
Adds an optimization column.
static int linoptL_solve(lua_State *L)
Solves the linear optimization problem.
static int linoptL_gc(lua_State *L)
Frees a linopt.
static int linoptL_readProblem(lua_State *L)
Reads an optimization problem from a file for debugging purposes.
static int linoptL_size(lua_State *L)
Adds columns to the linear program.
static int linoptL_addrows(lua_State *L)
Adds rows to the linear program.
static int linoptL_new(lua_State *L)
Opens a new linopt.
static int linoptL_addcols(lua_State *L)
Adds columns to the linear program.
static int linoptL_setrow(lua_State *L)
Adds an optimization row.
static int linoptL_eq(lua_State *L)
Compares two linopts to see if they are the same.
static int linoptL_writeProblem(lua_State *L)
Writes an optimization problem to a file for debugging purposes.

Optim metatable methods.

Definition at line 49 of file nlua_linopt.c.