Introduced ilp solver interface. becopyilp.c uses it now.
[libfirm] / ir / be / lpp.h
1 /**
2  * Author:      Daniel Grund
3  * Date:                16.05.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  */
7
8 #include <stdio.h>
9
10 typedef enum _opt_t {minimize, maximize} opt_t;
11 typedef enum _cst_t {objective=0, equal=1, less=2, greater=3} cst_t;
12 typedef enum _var_t {invalid=0, rhs=1, real=2, binary=3} var_t;
13 typedef enum _sol_state_t {unknown=0, no_solution_file=1, infeasible=2, unbounded=3, feasible=4, optimal=5} sol_state_t;
14 typedef struct _lpp_t lpp_t;
15
16 #define ERR_NAME_NOT_ALLOWED -2
17
18 /**
19  * Creates a new problem. Optimization type is minimize or maximize.
20  * Implicit row with name "obj" is inserted.
21  * Implicit col with name "rhs" is inserted.
22  */
23 lpp_t *new_lpp(const char *name, opt_t opt_type);
24
25 void free_lpp(lpp_t *lpp);
26
27 /**
28  * Adds a constraint to a problem. If a constraint with the same name already
29  * exists nothing is altered, and the index of the existing entry is returned.
30  * @param cst_name The name of the constraint (1st char only alpha-numeric!). If NULL, a default name will be used.
31  * @param cst_type The type of constraint: objective, equality, less-or-equal, greater-or-equal
32  * @param rhs The right hand side value to set for this constraint.
33  * @return The (new or existing) index of the constraint
34  */
35 int lpp_add_cst(lpp_t *lpp, const char *cst_name, cst_t cst_type, double rhs);
36
37 /**
38  * Returns the internal index of a constraint.
39  * @param cst_name The name of the constraint
40  * @return The internal index of constraint @p cst_name or -1 if it does not exist.
41  */
42 int lpp_get_cst_idx(lpp_t *lpp, char *cst_name);
43
44 /**
45  * Returns the name of a constraint.
46  * @param index The internal index of a constraint.
47  */
48 const char *lpp_get_cst_name(lpp_t *lpp, int index);
49
50 /**
51  * Adds a variable to a problem. If a variable with the same name already
52  * exists nothing is altered, and the index of the existing entry is returned.
53  * @param var_name The name of the constraint (1st char only alpha-numeric!). If NULL, a default name will be used.
54  * @param var_type The type of variable: real, binary
55  * @param obj The objactive value coefficient for this variable.
56  * @return The (new or existing) index of the variable
57  *
58  * NOTE: common integer or semi-continous vars are not (yet) implemented
59  */
60 int lpp_add_var(lpp_t *lpp, const char *var_name, var_t var_type, double obj);
61
62 /**
63  * Returns the internal index of a variable.
64  * @param cst_name The name of the variable
65  * @return The internal index of variable @p var_name or -1 if it does not exist.
66  */
67 int lpp_get_var_idx(lpp_t *lpp, char *var_name);
68
69 /**
70  * Returns the name of a variable.
71  * @param index The internal index of a variable.
72  */
73 const char *lpp_get_var_name(lpp_t *lpp, int index);
74
75 /**
76  * Sets the factor of the variable @p var_name in constraint @p cst_name to @p value.
77  * Use "obj" as constraint name to set factors in the objective function.
78  * Use "rhs" as variable name to set the right hand side values.
79  * @return -1 if constraint or variable name does not exist.
80  *                      0 otherwise
81  */
82 int lpp_set_factor(lpp_t *lpp, char *cst_name, char *var_name, double value);
83
84 /**
85  * Same as lpp_set_factor but uses the internal indices instead of names.
86  * "obj" and "rhs" both have index 0.
87  * @return -1 if an index was invalid
88  *                      0 otherwise
89  */
90 int lpp_set_factor_fast(lpp_t *lpp, int cst_idx, int var_idx, double value);
91
92 /**
93  * Set a starting value for a var.
94  * @param var_idx The index of the variable to set the value for.
95  * @param value The value to set.
96  */
97 void lpp_set_start_value(lpp_t *lpp, int var_idx, double value);
98
99 /**
100  * Solve the problem.
101  * @return -1 if an error ocurred, 0 otherwise
102  */
103 int lpp_solve(lpp_t *lpp, int use_start_values);
104
105 /**
106  * @return The solution values of the variables from index begin to index end.
107  */
108 sol_state_t lpp_get_solution(lpp_t *lpp, double *values, int begin, int end);