2 * Copyright (C) 2005-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Interface for specifying an milp. Does not define a solution method.
23 * @author Daniel Grund
33 #include "sp_matrix.h"
35 typedef enum _lpp_opt_t {
40 typedef enum _lpp_cst_t {
47 typedef enum _lpp_var_t {
54 typedef enum _lpp_sol_state_t {
63 typedef enum _lpp_value_kind_t {
69 typedef enum _lpp_emphasis_t {
77 typedef struct _name_t {
78 const char *name; /**< the name of the var/constraint supplied by user */
79 int nr; /**< the col/row number in the matrix */
80 lpp_value_kind_t value_kind;
88 typedef struct _lpp_t {
89 /* The problem data */
90 const char *name; /**< A textual name for this problem */
91 FILE *log; /**< The log file. */
92 lpp_opt_t opt_type; /**< Optimization direction */
93 struct obstack obst; /**< Obstack for variable names */
94 sp_matrix_t *m; /**< The matrix holding objective, constraints and rhs */
96 /* Cst/Var to Nr mapping */
97 set *cst2nr; /**< Holds name_t's for constraints */
98 set *var2nr; /**< Holds name_t's for variables */
100 /* Nr to Cst/Var mapping */
101 int cst_size, var_size; /**< Size of the csts/vars-arrays below */
102 int cst_next, var_next; /**< Next free position in arrays below */
103 lpp_name_t **csts; /**< Pointers to the elements in the cst2nr set */
104 lpp_name_t **vars; /**< Pointers to the elements in the var2nr set */
105 double objval; /**< OUT: Value of the objective function. */
106 double best_bound; /**< OUT: best bound to the integer solution. */
107 double grow_factor; /**< The factor by which the vars and constraints are enlarged */
109 /* Solving options */
110 int set_bound; /**< IN: Boolean flag to set a bound for the objective function. */
111 double bound; /**< IN: The bound. Only valid if set_bound == 1. */
112 double time_limit_secs; /**< IN: Time limit to obey while solving (0.0 means no time limit) */
115 lpp_sol_state_t sol_state; /**< State of the solution */
116 double sol_time; /**< Time in seconds */
117 unsigned iterations; /**< Number of iterations CPLEX needed to solve the ILP (whatever this means) */
120 unsigned next_name_number; /**< for internal use only */
121 lpp_emphasis_t emphasis; /**< On what should CPLEX concentrate (feasibility, bestbound, ...) */
123 /* some statistic stuff */
124 unsigned send_time; /**< in case of solve_net: send time in usec */
125 unsigned recv_time; /**< in case of solve_net: recv time in usec */
126 unsigned n_elems; /**< number of elements stored in the matrix */
127 unsigned matrix_mem; /**< memory used by matrix elements (in bytes) */
128 double density; /**< density of the matrix (percentage) */
131 #define ERR_NAME_NOT_ALLOWED -2
134 * Creates a new problem. Optimization type is minimize or maximize.
135 * Implicit row with name "obj" is inserted.
136 * Implicit col with name "rhs" is inserted.
138 lpp_t *new_lpp(const char *name, lpp_opt_t opt_type);
141 * Creates a new problem. Optimization type is minimize or maximize.
142 * Implicit row with name "obj" is inserted.
143 * Implicit col with name "rhs" is inserted.
144 * @param estimated_vars The estimated number of variables for the problem
145 * @param estimated_csts The estimated number of constraints for the problem
146 * @param grow_factor By which factor should the problem grow, if there are
147 * more variables or constraints than estimated.
149 lpp_t *new_lpp_userdef(const char *name, lpp_opt_t opt_type,
150 int estimated_vars, int estimated_csts, double grow_factor);
153 * Frees the matrix embedded in the LPP.
155 void free_lpp_matrix(lpp_t *lpp);
158 * Frees all memory allocated for LPP data structure.
160 void free_lpp(lpp_t *lpp);
163 * @return The constant term in the objective function
165 double lpp_get_fix_costs(lpp_t *lpp);
168 * Sets the constant term in the objective function to @p value
170 void lpp_set_fix_costs(lpp_t *lpp, double value);
173 * Adds a constraint to a problem. If a constraint with the same name already
174 * exists nothing is altered, and the index of the existing entry is returned.
175 * @param cst_name The name of the constraint (1st char only alpha-numeric!). If NULL, a default name will be used.
176 * @param cst_type The type of constraint: objective, equality, less-or-equal, greater-or-equal
177 * @param rhs The right hand side value to set for this constraint.
178 * @return The (new or existing) index of the constraint
180 int lpp_add_cst(lpp_t *lpp, const char *cst_name, lpp_cst_t cst_type, double rhs);
183 * Adds a constraint to a problem. If a constraint with the same name already
184 * exists it dies a horribly cruel death
185 * @param cst_name The name of the constraint (1st char only alpha-numeric!). If NULL, a default name will be used.
186 * @param cst_type The type of constraint: objective, equality, less-or-equal, greater-or-equal
187 * @param rhs The right hand side value to set for this constraint.
188 * @return The (new or existing) index of the constraint
190 int lpp_add_cst_uniq(lpp_t *lpp, const char *cst_name, lpp_cst_t cst_type, double rhs);
193 * Returns the internal index of a constraint.
194 * @param cst_name The name of the constraint
195 * @return The internal index of constraint @p cst_name or -1 if it does not exist.
197 int lpp_get_cst_idx(lpp_t *lpp, const char *cst_name);
200 * Returns the name of a constraint.
201 * @param index The internal index of a constraint.
202 * @param buf A buffer to hold the name of the constraint
203 * @param buf_size Size of the buffer
205 void lpp_get_cst_name(lpp_t *lpp, int index, char *buf, size_t buf_size);
208 * Adds a variable to a problem. If a variable with the same name already
209 * exists nothing is altered, and the index of the existing entry is returned.
210 * @param var_name The name of the constraint (1st char only alpha-numeric!). If NULL, a default name will be used.
211 * @param var_type The type of variable: real, binary
212 * @param obj The objective value coefficient for this variable.
213 * @return The (new or existing) index of the variable
215 * NOTE: common integer or semi-continuous vars are not (yet) implemented
217 int lpp_add_var(lpp_t *lpp, const char *var_name, lpp_var_t var_type, double obj);
220 * Same as lpp_add_var() but the user can supply a default value.
222 int lpp_add_var_default(lpp_t *lpp, const char *var_name, lpp_var_t var_type, double obj, double startval);
225 * Returns the internal index of a variable.
226 * @param cst_name The name of the variable
227 * @return The internal index of variable @p var_name or -1 if it does not exist.
229 int lpp_get_var_idx(lpp_t *lpp, const char *var_name);
232 * Returns the name of a variable.
233 * @param index The internal index of a variable.
234 * @param buf A buffer to hold the name of the variable
235 * @param buf_size Size of the buffer
237 void lpp_get_var_name(lpp_t *lpp, int index, char *buf, size_t buf_size);
240 * Sets the factor of the variable @p var_name in constraint @p cst_name to @p value.
241 * @return -1 if constraint or variable name does not exist.
244 int lpp_set_factor(lpp_t *lpp, const char *cst_name, const char *var_name, double value);
247 * Same as lpp_set_factor but uses the internal indices instead of names.
248 * @return -1 if an index was invalid
251 int lpp_set_factor_fast(lpp_t *lpp, int cst_idx, int var_idx, double value);
253 int lpp_set_factor_fast_bulk(lpp_t *lpp, int cst_idx, int *var_idx, int num_vars, double value);
256 * Set a starting value for a var.
257 * @param var_idx The index of the variable to set the value for.
258 * @param value The value to set.
260 void lpp_set_start_value(lpp_t *lpp, int var_idx, double value);
263 * @return The solution values of the variables from index begin to index end.
265 lpp_sol_state_t lpp_get_solution(lpp_t *lpp, double *values, int begin, int end);
268 * Dumps the lpp into a file with name @p filename in MPS-format
270 void lpp_dump(lpp_t *lpp, const char *filename);
273 * Set the log file, where the solver should write to.
274 * @param lpp The problem.
275 * @param log The logfile. NULL for no logging.
277 void lpp_set_log(lpp_t *lpp, FILE *log);
280 * Check the start values and list conflicting constraints.
282 void lpp_check_startvals(lpp_t *lpp);
285 * Dump problem into a text file.
286 * @param lpp The problem.
289 void lpp_dump_plain(lpp_t *lpp, FILE *f);
291 #define lpp_get_iter_cnt(lpp) ((lpp)->iterations)
292 #define lpp_get_sol_time(lpp) ((lpp)->sol_time)
293 #define lpp_get_sol_state(lpp) ((lpp)->sol_state)
294 #define lpp_get_var_count(lpp) ((lpp)->var_next-1)
295 #define lpp_get_cst_count(lpp) ((lpp)->cst_next-1)
296 #define lpp_get_sol_state(lpp) ((lpp)->sol_state)
297 #define lpp_get_var_sol(lpp,idx) ((lpp)->vars[idx]->value)
298 #define lpp_is_sol_valid(lpp) (lpp_get_sol_state(lpp) >= lpp_feasible)
300 #define lpp_set_time_limit(lpp,secs) ((lpp)->time_limit_secs = secs)
303 * Set a bound for the objective function.
304 * @param lpp The problem.
305 * @param bound A bound for the objective function.
306 * If the problem is a minimization problem, the bound
307 * is a lower bound. If it is a maximization problem,
308 * the bound is an upper bound.
310 #define lpp_set_bound(lpp,bnd) ((lpp)->set_bound = 1, (lpp)->bound = (bnd))
314 * @param lpp The problem.
316 #define lpp_unset_bound(lpp) ((lpp)->set_bound = 0)
320 * @param lpp The problem.
321 * @param host The host to solve on.
322 * @param solver The solver to use.
324 void lpp_solve(lpp_t *lpp, const char* host, const char* solver);