Project

General

Profile

Statistics
| Branch: | Revision:

cool / src / lib / GMLMIP-0.1 / rules / GML_premise.c @ 7c4d2eb4

History | View | Annotate | Download (6.12 KB)

1
#include "GML_premise.h"
2

    
3
GML_Premise::GML_Premise() : Premise<int>(){
4
}
5

    
6
GML_Premise::GML_Premise(int _n, int _m, int* _a, int *_b) : Premise<int>(_n,_m,_a,_b){
7
//test_counter = 0;
8
}
9

    
10

    
11
/* Deprecated Functions which searched for solutions to the side condition first.
12
vector<Conclusion> GML_Premise::construct_conclusions(){
13
        double limit = bound();
14
        vector<Conclusion> set;
15
        glp_prob *side_condition = glp_create_prob();
16
        
17
        // Parameters while solving with glpk
18
        glp_iocp* parameters = new glp_iocp;
19
        glp_init_iocp(parameters);
20
        parameters->presolve = GLP_ON;
21
        parameters->msg_lev = GLP_MSG_ERR;
22
        
23
        // Load numbers into LP
24
        load_side_condition(side_condition, limit);
25

26
        // output for testing.
27
        //glp_write_lp(side_condition, NULL, "output.txt");
28

29
        // Solve depth first        
30
        solve_side_condition(side_condition, parameters, 0, 0, 0, limit, set);
31
        
32
        //Clean-up
33
        delete parameters;
34
        glp_delete_prob(side_condition);
35
        
36
        return set;
37
}
38

39
void GML_Premise::load_side_condition(glp_prob* sc, double limit){
40
        glp_set_obj_dir(sc, GLP_MIN);
41
        glp_add_rows(sc, 1);
42
        glp_set_row_bnds(sc, 1, GLP_LO, 1.0, 0.0);
43

44
        glp_add_cols(sc, n+m);
45
        
46
        for(int i=1; i < (n+m+1); i++){
47
                glp_set_col_kind(sc, i, GLP_IV); //integer variables
48
                glp_set_col_bnds(sc, i,  GLP_DB, 0, limit);
49
        }
50
        
51
        // Arrays for inputting into glpk
52
        double ar[n+m+1];
53
        int ia[n+m+1];
54
        int ja[n+m+1];
55
        
56
        for(int i=1; i < n+m+1; i++){
57
                   ia[i]=1;
58
             ja[i]=i;
59
     }
60
     
61
    int counter=1;        
62
        for(int i=0; i < n; i++){
63
                ar[counter]= a[i]+1;
64
                counter++;
65
        }
66

67
        for(int j=0; j < m; j++){
68
                ar[counter]=(-b[j]);
69
                counter++;
70
        }
71

72
        // Set objective function        
73
        for(int i=1; i < n+m+1; i++)
74
                glp_set_obj_coef(sc, i, ar[i]);
75
                
76
        glp_load_matrix(sc, n+m, ia, ja, ar);
77
}
78

79
void GML_Premise::solve_side_condition(glp_prob* sc, glp_iocp* parameters, int r_or_s, int index, int new_bound, double limit, vector<Conclusion>& set){
80
        int r[n];
81
        int s[m];
82
        int old_bound;
83
        Conclusion conc;
84
        
85
        if(r_or_s == 1){
86
                if(new_bound < 1)
87
                        return;
88
                // add r_i < bound    
89
                old_bound = glp_get_col_ub(sc, index+1);
90
                if(new_bound == 1)
91
                        glp_set_col_bnds(sc, index+1,  GLP_FX, 0, 0);
92
                else                        
93
                        glp_set_col_bnds(sc, index+1,  GLP_DB, 0, new_bound-1); // Minus once since need strictly less than. Glpk is less or equal.
94
        }
95
        
96
        if(r_or_s == 2){
97
                if(new_bound+1 > limit)
98
                        return;
99
                // add bound s_j > bound
100
                old_bound = glp_get_col_lb(sc, index+n+1);
101
                if(new_bound+1 == limit)
102
                        glp_set_col_bnds(sc, index+n+1,  GLP_FX, limit, limit);
103
                else
104
                        glp_set_col_bnds(sc, index+n+1,  GLP_DB, new_bound+1, limit);
105
        }
106
        
107
        //Test output 
108
        //char str[15];
109
        //sprintf(str, "output%d.txt", test_counter);
110
        //glp_write_lp(sc, NULL, str);        
111
        //test_counter++;
112
        
113
                        
114
        // Solve
115
        int result = glp_intopt(sc, parameters);
116
        
117
        if(result == 0) { //if no errors from solver
118
                if(glp_mip_status(sc)==GLP_OPT || glp_mip_status(sc)==GLP_FEAS){ // if feasible
119
                
120
                // Load r and s from solution
121
                for(int i=0; i < n; i++)
122
                        r[i] = static_cast<int>(glp_mip_col_val(sc, i+1));
123
                        
124
                for(int j=0; j < m; j++)
125
                        s[j] = static_cast<int>(glp_mip_col_val(sc, j+n+1));
126

127
                 // do all valuations
128
                 all_valuations(parameters, r, s, conc);
129
         
130

131
                 //if result isnt in list of conclusions then add it
132
                 bool already_have = false;
133
                 for(unsigned int i=0; i < set.size(); i++)
134
                         if( conc == set[i])
135
                                 already_have = true;
136
                 if(!already_have)
137
                        set.push_back(conc);
138
                         
139
                // Recursively branch and solve the others - depth first
140
                for(int i=0; i < n; i++)
141
                        solve_side_condition(sc, parameters, 1, i, r[i], limit, set);
142
                for(int j=0; j < m; j++)
143
                        solve_side_condition(sc, parameters, 2, j, s[j], limit, set);
144
                }
145
        }
146
        else if (result != GLP_ENOPFS) { // if not a feasibility error
147
                cout << "glpk error: " << result << endl;
148
                exit(1);
149
        }
150
        //remove bounds (go back up before branching down again).
151
        if(r_or_s == 1)
152
                glp_set_col_bnds(sc, index+1,  GLP_DB, 0, old_bound);
153
        
154
        if(r_or_s == 2)
155
                glp_set_col_bnds(sc, index+n+1,  GLP_DB, old_bound, limit);
156
}
157

158
void GML_Premise::all_valuations(glp_iocp* parameters, int* r, int* s, Conclusion& result){
159
//Testing output
160
cout << "====================" << endl;
161
        for(int i=0; i < n; i++)
162
                cout << "r[" << i << "] = " << r[i] << endl;
163
        
164
        for(int j=0; j < m; j++)
165
                cout << "s[" << j << "] = " << s[j] << endl;
166

167

168
glp_prob *conclusion = glp_create_prob();
169
glp_set_obj_dir(conclusion, GLP_MIN);
170
glp_add_rows(conclusion, 2);
171
glp_set_row_bnds(conclusion, 1, GLP_LO, 1, 0); // strictly greater than zero
172

173
glp_add_cols(conclusion, n+m);
174

175
for(int i=1; i < (n+m+1); i++)
176
        glp_set_col_kind(conclusion, i, GLP_BV);
177

178
// Arrays for inputting into glpk
179
        double ar[2*(n+m)+1];
180
        int ia[2*(n+m)+1];
181
        int ja[2*(n+m)+1];
182
        
183
        for(int i=1; i < n+m+1; i++){
184
                   ia[i]=1;
185
                   ia[n+m+i]=2;
186
             ja[i]=i;
187
             ja[n+m+i]=i;
188
     }
189
     
190
    int counter=1;        
191
        for(int i=0; i < n; i++){
192
                ar[counter]= r[i];
193
                counter++;
194
        }
195

196
        for(int j=0; j < m; j++){
197
                ar[counter]=(-s[j]);
198
                counter++;
199
        }
200
        
201
        for(int i=0; i < n+m; i++){
202
                ar[counter]=pow(2,i);
203
                counter++;
204
        }
205

206
        // Set objective function        
207
        for(int i=1; i < n+m+1; i++)
208
                glp_set_obj_coef(conclusion, i, pow(2,i-1));
209
                
210
        glp_load_matrix(conclusion, 2*(n+m), ia, ja, ar);
211
        
212
                glp_set_row_bnds(conclusion, 2, GLP_LO, 0, 0);
213
                
214
        // output for testing
215
        //glp_write_lp(conclusion, NULL, "valuations.txt");        
216
        
217
        solve_conclusion_lp(conclusion, parameters, 0, result);
218
        
219
        glp_delete_prob(conclusion);
220
}
221

222
void GML_Premise::solve_conclusion_lp(glp_prob* conclusion, glp_iocp* parameters, int min_limit, Conclusion& conc){
223
        glp_set_row_bnds(conclusion, 2, GLP_LO, min_limit, 0);
224
        
225
        int result = glp_intopt(conclusion, parameters);
226
        
227
        if(result == 0){//if no errors from solver
228
                if(glp_mip_status(conclusion)==GLP_OPT || glp_mip_status(conclusion)==GLP_FEAS){ // if feasible
229
                        int p[n+m];
230
                        for(int i=0; i<n+m; i++)
231
                                p[i]=glp_mip_col_val(conclusion, i+1);
232
                                //cout << glp_mip_col_val(conclusion, i+1);
233
                        Clause c(n+m,p);
234
                        conc.push_back(c);
235
                
236
                        solve_conclusion_lp(conclusion, parameters, glp_mip_obj_val(conclusion)+1, conc);
237
                }
238
        }
239
        else if (result != GLP_ENOPFS) { // if not a feasibility error
240
                cout << "glpk error: " << result << endl;
241
                exit(1);        
242
        }
243
}
244

245

246
double GML_Premise::bound(){
247
        return 2;
248
        //return pow(2,6 * sizeoflp(n, m, a, b) * (n+m) * (n+m) * (n+m));
249
}
250

251
END OF DEPRECATED FUNCTIONS */