2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Interface for specifying an milp. Does not define a solution method.
20 #include "sp_matrix.h"
22 typedef enum _lpp_opt_t {
27 typedef enum _lpp_cst_t {
34 typedef enum _lpp_var_t {
41 typedef enum _lpp_sol_state_t {
50 typedef enum _lpp_value_kind_t {
56 typedef enum _lpp_emphasis_t {
64 typedef struct _name_t {
65 const char *name; /**< the name of the var/constraint supplied by user */
66 int nr; /**< the col/row number in the matrix */
67 lpp_value_kind_t value_kind;
75 typedef struct _lpp_t {
76 /* The problem data */
77 const char *name; /**< A textual name for this problem */
78 FILE *log; /**< The log file. */
79 lpp_opt_t opt_type; /**< Optimization direction */
80 struct obstack obst; /**< Obstack for variable names */
81 sp_matrix_t *m; /**< The matrix holding objective, constraints and rhs */
83 /* Cst/Var to Nr mapping */
84 set *cst2nr; /**< Holds name_t's for constraints */
85 set *var2nr; /**< Holds name_t's for variables */
87 /* Nr to Cst/Var mapping */
88 int cst_size, var_size; /**< Size of the csts/vars-arrays below */
89 int cst_next, var_next; /**< Next free position in arrays below */
90 lpp_name_t **csts; /**< Pointers to the elements in the cst2nr set */
91 lpp_name_t **vars; /**< Pointers to the elements in the var2nr set */
92 double objval; /**< OUT: Value of the objective function. */
93 double best_bound; /**< OUT: best bound to the integer solution. */
94 double grow_factor; /**< The factor by which the vars and constraints are enlarged */
97 bool set_bound; /**< IN: Boolean flag to set a bound for the objective function. */
98 double bound; /**< IN: The bound. Only valid if set_bound == 1. */
99 double time_limit_secs; /**< IN: Time limit to obey while solving (0.0 means no time limit) */
102 lpp_sol_state_t sol_state; /**< State of the solution */
103 double sol_time; /**< Time in seconds */
104 unsigned iterations; /**< Number of iterations CPLEX needed to solve the ILP (whatever this means) */
107 unsigned next_name_number; /**< for internal use only */
108 lpp_emphasis_t emphasis; /**< On what should CPLEX concentrate (feasibility, bestbound, ...) */
110 /* some statistic stuff */
111 unsigned send_time; /**< in case of solve_net: send time in usec */
112 unsigned recv_time; /**< in case of solve_net: recv time in usec */
113 unsigned n_elems; /**< number of elements stored in the matrix */
114 unsigned matrix_mem; /**< memory used by matrix elements (in bytes) */
115 double density; /**< density of the matrix (percentage) */
118 #define ERR_NAME_NOT_ALLOWED -2
121 * Creates a new problem. Optimization type is minimize or maximize.
122 * Implicit row with name "obj" is inserted.
123 * Implicit col with name "rhs" is inserted.
125 lpp_t *lpp_new(const char *name, lpp_opt_t opt_type);
128 * Creates a new problem. Optimization type is minimize or maximize.
129 * Implicit row with name "obj" is inserted.
130 * Implicit col with name "rhs" is inserted.
131 * @param estimated_vars The estimated number of variables for the problem
132 * @param estimated_csts The estimated number of constraints for the problem
133 * @param grow_factor By which factor should the problem grow, if there are
134 * more variables or constraints than estimated.
136 lpp_t *lpp_new_userdef(const char *name, lpp_opt_t opt_type,
137 int estimated_vars, int estimated_csts,
141 * Frees the matrix embedded in the LPP.
143 void lpp_free_matrix(lpp_t *lpp);
146 * Frees all memory allocated for LPP data structure.
148 void lpp_free(lpp_t *lpp);
151 * @return The constant term in the objective function
153 double lpp_get_fix_costs(lpp_t *lpp);
156 * Sets the constant term in the objective function to @p value
158 void lpp_set_fix_costs(lpp_t *lpp, double value);
161 * Adds a constraint to a problem. If a constraint with the same name already
162 * exists nothing is altered, and the index of the existing entry is returned.
163 * @param cst_name The name of the constraint (1st char only alpha-numeric!). If NULL, a default name will be used.
164 * @param cst_type The type of constraint: objective, equality, less-or-equal, greater-or-equal
165 * @param rhs The right hand side value to set for this constraint.
166 * @return The (new or existing) index of the constraint
168 int lpp_add_cst(lpp_t *lpp, const char *cst_name, lpp_cst_t cst_type, double rhs);
171 * Adds a constraint to a problem. If a constraint with the same name already
172 * exists it dies a horribly cruel death
173 * @param cst_name The name of the constraint (1st char only alpha-numeric!). If NULL, a default name will be used.
174 * @param cst_type The type of constraint: objective, equality, less-or-equal, greater-or-equal
175 * @param rhs The right hand side value to set for this constraint.
176 * @return The (new or existing) index of the constraint
178 int lpp_add_cst_uniq(lpp_t *lpp, const char *cst_name, lpp_cst_t cst_type, double rhs);
181 * Returns the internal index of a constraint.
182 * @param cst_name The name of the constraint
183 * @return The internal index of constraint @p cst_name or -1 if it does not exist.
185 int lpp_get_cst_idx(lpp_t *lpp, const char *cst_name);
188 * Returns the name of a constraint.
189 * @param index The internal index of a constraint.
190 * @param buf A buffer to hold the name of the constraint
191 * @param buf_size Size of the buffer
193 void lpp_get_cst_name(lpp_t *lpp, int index, char *buf, size_t buf_size);
196 * Adds a variable to a problem. If a variable with the same name already
197 * exists nothing is altered, and the index of the existing entry is returned.
198 * @param var_name The name of the constraint (1st char only alpha-numeric!). If NULL, a default name will be used.
199 * @param var_type The type of variable: real, binary
200 * @param obj The objective value coefficient for this variable.
201 * @return The (new or existing) index of the variable
203 * NOTE: common integer or semi-continuous vars are not (yet) implemented
205 int lpp_add_var(lpp_t *lpp, const char *var_name, lpp_var_t var_type, double obj);
208 * Same as lpp_add_var() but the user can supply a default value.
210 int lpp_add_var_default(lpp_t *lpp, const char *var_name, lpp_var_t var_type, double obj, double startval);
213 * Returns the internal index of a variable.
214 * @param cst_name The name of the variable
215 * @return The internal index of variable @p var_name or -1 if it does not exist.
217 int lpp_get_var_idx(lpp_t *lpp, const char *var_name);
220 * Returns the name of a variable.
221 * @param index The internal index of a variable.
222 * @param buf A buffer to hold the name of the variable
223 * @param buf_size Size of the buffer
225 void lpp_get_var_name(lpp_t *lpp, int index, char *buf, size_t buf_size);
228 * Sets the factor of the variable @p var_name in constraint @p cst_name to @p value.
229 * @return -1 if constraint or variable name does not exist.
232 int lpp_set_factor(lpp_t *lpp, const char *cst_name, const char *var_name, double value);
235 * Same as lpp_set_factor but uses the internal indices instead of names.
236 * @return -1 if an index was invalid
239 int lpp_set_factor_fast(lpp_t *lpp, int cst_idx, int var_idx, double value);
241 int lpp_set_factor_fast_bulk(lpp_t *lpp, int cst_idx, int *var_idx, int num_vars, double value);
244 * Set a starting value for a var.
245 * @param var_idx The index of the variable to set the value for.
246 * @param value The value to set.
248 void lpp_set_start_value(lpp_t *lpp, int var_idx, double value);
251 * @return The solution values of the variables from index begin to index end.
253 lpp_sol_state_t lpp_get_solution(lpp_t *lpp, double *values, int begin, int end);
256 * Dumps the lpp into a file with name @p filename in MPS-format
258 void lpp_dump(lpp_t *lpp, const char *filename);
261 * Set the log file, where the solver should write to.
262 * @param lpp The problem.
263 * @param log The logfile. NULL for no logging.
265 void lpp_set_log(lpp_t *lpp, FILE *log);
268 * Check the start values and list conflicting constraints.
270 void lpp_check_startvals(lpp_t *lpp);
273 * Dump problem into a text file.
274 * @param lpp The problem.
277 void lpp_dump_plain(lpp_t *lpp, FILE *f);
279 static inline unsigned lpp_get_iter_cnt(const lpp_t *lpp)
281 return lpp->iterations;
284 static inline double lpp_get_sol_time(const lpp_t *lpp)
286 return lpp->sol_time;
289 static inline lpp_sol_state_t lpp_get_sol_state(const lpp_t *lpp)
291 return lpp->sol_state;
294 static inline int lpp_get_var_count(const lpp_t *lpp)
296 return lpp->var_next-1;
299 static inline int lpp_get_cst_count(const lpp_t *lpp)
301 return lpp->cst_next-1;
304 static inline double lpp_get_var_sol(const lpp_t *lpp, int idx)
306 return lpp->vars[idx]->value;
309 static inline bool lpp_is_sol_valid(const lpp_t *lpp)
311 return lpp_get_sol_state(lpp) >= lpp_feasible;
314 static inline void lpp_set_time_limit(lpp_t *lpp, double secs)
316 lpp->time_limit_secs = secs;
320 * Set a bound for the objective function.
321 * @param lpp The problem.
322 * @param bound A bound for the objective function.
323 * If the problem is a minimization problem, the bound
324 * is a lower bound. If it is a maximization problem,
325 * the bound is an upper bound.
327 static inline void lpp_set_bound(lpp_t *lpp, double bound)
329 lpp->set_bound = true;
335 * @param lpp The problem.
337 static inline void lpp_unset_bound(lpp_t *lpp)
339 lpp->set_bound = false;
344 * @param lpp The problem.
345 * @param host The host to solve on.
346 * @param solver The solver to use.
348 void lpp_solve(lpp_t *lpp, const char* host, const char* solver);