Project

General

Profile

Statistics
| Branch: | Revision:

cool / src / lib / GMLMIP-0.1 / formulas / PML_formula.c @ 7c4d2eb4

History | View | Annotate | Download (2.53 KB)

1 e034e268 Thorsten WiƟmann
#include "PML_formula.h"
2
3
bdd PML_Formula::modal(bdd *b, int n, int m){
4
        if(n > m){
5
                cout << "error: " << n << "/" << m << " is not in [0,1]" << endl;
6
                exit(1);
7
        }
8
        Rational r(n,m);
9
        return modal_var(b, r);
10
}
11
12
13
void PML_Formula::load_linear_program(glp_prob* problem, Premise<Rational>& prem) {
14
        int n = prem.get_n();
15
        int m = prem.get_m();
16
        int no_of_valuations = prem.get_total_valuations();
17
        
18
        // Additional 2 for side condition and requirements that at least one r_i,s_j is non zero        
19
        glp_add_rows(problem, no_of_valuations+2);
20
21
        // Additional column for k
22
        glp_add_cols(problem, n+m+1);
23
        
24
        for(int i = 1; i < no_of_valuations+1; i++)
25
                glp_set_row_bnds(problem, i, GLP_UP, 0.0, 0.0); // <= 0
26
        
27
        // Set row bounds for side condition - recall we have -k in the extra column.
28
        if(m==0)
29
        glp_set_row_bnds(problem, no_of_valuations+1, GLP_LO, 1.0, 0.0); // >= 1        
30
        else
31
        glp_set_row_bnds(problem, no_of_valuations+1, GLP_LO, 0.0, 0.0); // >= 0        
32
        
33
        // Set row bounds for additional constraint
34
        glp_set_row_bnds(problem, no_of_valuations+2, GLP_LO, 1.0, 0.0); // >= 1        
35
        
36
        // Set variable bounds
37
        for(int i=1; i < (n+m+1); i++){
38
                glp_set_col_kind(problem, i, GLP_IV); //integer variables
39
                glp_set_col_bnds(problem, i,  GLP_LO, 0, 0); // r_i, s_i >= 0
40
        }
41
        
42
        // K is positive integer
43
        glp_set_col_kind(problem, n+m+1, GLP_IV); //integer k
44
        glp_set_col_bnds(problem, n+m+1, GLP_LO, 1.0, 0.0); // k >= 1
45
        
46
        
47
        // Arrays for inputting into glpk
48
        const int size = (no_of_valuations+2)*(n+m+1);
49
        double ar[size+1];
50
        int ia[size+1];
51
        int ja[size+1];
52
        
53
        // Recall GLKP takes arrays with 1 - size not 0 - size-1.
54
        for(int i=0; i < size; i++){
55
                   ia[i+1] = (i / (n+m+1)) + 1;
56
                   ja[i+1] = (i % (n+m+1)) + 1;        
57
           }
58
    
59
           int counter=1;
60
           
61
           // Load all valuations
62
        for(int i=0; i < no_of_valuations; i++){
63
                int temp = i;
64
                for(int j=0; j < n; j++){
65
                        ar[counter] = temp & 1;
66
                        temp >>= 1;
67
                        counter++;
68
                }
69
                for(int j=0; j < m; j++){
70
                        ar[counter] = -(temp & 1);
71
                        temp >>= 1;
72
                        counter++;
73
                }
74
                ar[counter] = -1; // - k on each valuation.
75
                counter++;
76
        }
77
        
78
        // Load side condition
79
        for(int i=0; i < n; i++){
80
                ar[counter] = prem.get_a_i(i).get();
81
                counter++;
82
        }
83
        
84
        for(int j=0; j < m; j++){
85
                ar[counter]= -(prem.get_b_i(j).get());
86
                counter++;
87
        }
88
        //side condition has coefficent k of -1
89
        ar[counter] = -1;
90
        counter++;
91
        
92
        //Finally load condition that one must be nonzero.
93
        for(int i=0; i < n+m; i++){
94
                ar[counter] = 1;
95
                counter++;
96
        }
97
        // k coefficient here is zero
98
        ar[counter] = 0;
99
        
100
        glp_load_matrix(problem, size, ia, ja, ar);
101
        //glp_write_lp(problem, NULL, "testy.txt");                
102
}