Blame view

MODFIRE-Prototype/src/CustomPropagator2.java 9.59 KB
44aa78ff   Eduardo Eloy   implementation at...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import org.chocosolver.solver.constraints.Propagator;
import org.chocosolver.solver.constraints.PropagatorPriority;
import org.chocosolver.solver.exception.ContradictionException;
import org.chocosolver.solver.variables.IntVar;
import org.chocosolver.solver.variables.events.IntEventType;
import org.chocosolver.util.ESat;

import java.util.ArrayList;

public class CustomPropagator2 extends org.chocosolver.solver.constraints.Propagator<IntVar> {

    final int ugIndex; // refers to the UG we are starting out from
    final UG[] nodes;
    final int b; // refers to the sum area limit
    //final IntVar[] woodYield;



    public CustomPropagator2(int ugIndex, UG[] nodes, IntVar[] ugs,  int sum, IntVar[] woodYield) {
        super(ugs, PropagatorPriority.LINEAR, false);
        this.ugIndex = ugIndex;
        this.nodes = nodes;
        this.b = sum;

        //this.woodYield = woodYield;

    }

    @Override
    public int getPropagationConditions(int vIdx) { //don't know if this matters
        return IntEventType.combine(IntEventType.INSTANTIATE, IntEventType.INCLOW);
    }


    /*
    method to fill an array list with the prescription values of int ug
    in the year int period
     */
    public ArrayList<Integer> hasCutInYear(int ug, int period) {

        ArrayList<Integer> hasCut = new ArrayList<>();

        for(int i = 0; i < nodes[ug].presc.length; i++) {

            for(int j = 0; j < nodes[ug].years[i].length; j++) {

                if(nodes[ug].years[i][j] == period) {
                    hasCut.add(nodes[ug].presc[i]);
                }
            }
        }
        return hasCut;
    }


    //recursive function that fills an array list with the continuous adjacent UGs that have CUTS.
    public int gPropagate(int first, int ugIndex, int year, int sum) throws Exception{
        if(ugIndex == first){  //if the UG we are on isn't the UG we started out from
844958a5   Eduardo Eloy   fixed infinite lo...
59
60
            if(nodes[ugIndex].valid && vars[ugIndex].isInstantiated() && nodes[ugIndex].time < year) { //and if this UG is instantiaded and hasn't been gladed yet
                nodes[ugIndex].time = year;
44aa78ff   Eduardo Eloy   implementation at...
61
62
63
64
65
66
67
68
69
                sum = sum + (int)nodes[ugIndex].area;

                if(sum > b){
                    throw new Exception();
                }

                for (int rec = 0; rec < nodes[ugIndex].adj.length; rec++) { //and check its adjacencies

                    // to access the adjacent ugs we go through the ADJ array in current UG's node.
844958a5   Eduardo Eloy   fixed infinite lo...
70
                    if(nodes[nodes[ugIndex].adj[rec]].valid &&  vars[nodes[ugIndex].adj[rec]].isInstantiated() && nodes[nodes[ugIndex].adj[rec]].time < year) {
44aa78ff   Eduardo Eloy   implementation at...
71
72
73
74
75
76
77
                        //we only do this if the adjacent UG's haven't been gladed and if they've been instantiated.
                        sum = gPropagate(first, nodes[ugIndex].adj[rec], year, sum);
                    }
                }
            }
        }
        else {
844958a5   Eduardo Eloy   fixed infinite lo...
78
            if (nodes[ugIndex].valid && vars[ugIndex].isInstantiated() && nodes[ugIndex].time < year) { //check if the UG hasn't been gladed and if it's already instantiated
44aa78ff   Eduardo Eloy   implementation at...
79
80
81
82
83
84
85

                int ugPresc = vars[ugIndex].getValue(); //we get the prescription value of the current UG

                ArrayList<Integer> hasCut = hasCutInYear(ugIndex, year); //we get the list of prescriptions of this UG where there is a CUT in this year

                if(hasCut.contains(ugPresc)){ //if the List with all those prescriptions has the prescription the UG has been instantiated with.

844958a5   Eduardo Eloy   fixed infinite lo...
86
                    nodes[ugIndex].time = year;
44aa78ff   Eduardo Eloy   implementation at...
87
88
89
90
91
92
93
94
95

                    sum = sum + (int)nodes[ugIndex].area;

                    if(sum > b){
                        throw new Exception();
                    }

                    for (int rec = 0; rec < nodes[ugIndex].adj.length; rec++) { //and check its adjacencies

844958a5   Eduardo Eloy   fixed infinite lo...
96
                        if(nodes[nodes[ugIndex].adj[rec]].valid && vars[nodes[ugIndex].adj[rec]].isInstantiated() && nodes[nodes[ugIndex].adj[rec]].time < year) {
44aa78ff   Eduardo Eloy   implementation at...
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
                            sum = gPropagate(first, nodes[ugIndex].adj[rec], year, sum);
                        }
                    }
                }
            }
        }
        return sum;
    }

    public void gladePropagate(int ugIndex, int period) throws Exception {


        //the gPropagate function receives the ugIndex twice to differentiate between the first call of the function and the recursive calls
        //then receives the year and an empty array list which will be filled.

        gPropagate(ugIndex, ugIndex, period,0);

    }



    @Override
    public void propagate(int evtmask) throws ContradictionException {

        if(nodes[ugIndex].valid && vars[ugIndex].isInstantiated()) { //if the UG we give as parameter to the propagator is already instantiated

            int ugPresc = vars[ugIndex].getValue(); //we get it's prescription value

            int prescIndex = 0;

            for (int i = 0; i < nodes[ugIndex].presc.length; i++) {
                if (nodes[ugIndex].presc[i] == ugPresc) {
                    prescIndex = i; //and we find the index of this prescription in the UG's PRESC array
                    //we need this index to access the right set of years in the PERIODS array
                }
            }

d1578c9b   Eduardo Eloy   fixed bugs in dat...
134
            //System.out.println(ugIndex + " " + prescIndex);
44aa78ff   Eduardo Eloy   implementation at...
135
136
137
138
139
            if (nodes[ugIndex].years[prescIndex][0] > 0) { //if the first index of the PERIODS array is larger than 0
                //that means that if the UG take this PRESC value then it has cuts,
                // if this value was 0 that means the UG with this presc has no cuts

                for(int i = 0; i < nodes.length; i++)
844958a5   Eduardo Eloy   fixed infinite lo...
140
141
142
                    nodes[i].time = 0;


44aa78ff   Eduardo Eloy   implementation at...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
                for (int p = 0; p < nodes[ugIndex].years[prescIndex].length; p++) { //for every year that our UG has cuts

                    int year = nodes[ugIndex].years[prescIndex][p]; //current year

                    try {
                        gladePropagate(ugIndex, year); //call the gladePropagate function which will return
                    } catch (Exception e) {
                        fails();
                    }
                }
            }
        }

    }



    // same as gPropagate but without the "isInstatiated" checks
    public int g(int first, int ugIndex, int year, int sum) throws Exception{
        if(ugIndex == first){
844958a5   Eduardo Eloy   fixed infinite lo...
163
164
            if(nodes[ugIndex].valid && nodes[ugIndex].time < year) {
                nodes[ugIndex].time = year;
44aa78ff   Eduardo Eloy   implementation at...
165
166
167
168
169
170
171
172
173
174

                sum = sum + (int)nodes[ugIndex].area;

                if(sum > b){
                    throw new Exception();
                }

                for (int rec = 0; rec < nodes[ugIndex].adj.length; rec++) {


844958a5   Eduardo Eloy   fixed infinite lo...
175
                    if(nodes[nodes[ugIndex].adj[rec]].valid && nodes[nodes[ugIndex].adj[rec]].time < year) {
44aa78ff   Eduardo Eloy   implementation at...
176
177
178
179
180
181
                        sum = g(first, nodes[ugIndex].adj[rec], year, sum);
                    }
                }
            }
        }
        else {
844958a5   Eduardo Eloy   fixed infinite lo...
182
            if (nodes[ugIndex].valid && nodes[ugIndex].time < year) {
44aa78ff   Eduardo Eloy   implementation at...
183
184
185
186
187
188

                int ugPresc = vars[ugIndex].getValue();

                ArrayList<Integer> hasCut = hasCutInYear(ugIndex, year);

                if(hasCut.contains(ugPresc)){
844958a5   Eduardo Eloy   fixed infinite lo...
189
                    nodes[ugIndex].time = year;
44aa78ff   Eduardo Eloy   implementation at...
190
191
192
193
194
195
196
197
198

                    sum = sum + (int)nodes[ugIndex].area;

                    if(sum > b){
                        throw new Exception();
                    }

                    for (int rec = 0; rec < nodes[ugIndex].adj.length; rec++) {

844958a5   Eduardo Eloy   fixed infinite lo...
199
                        if(nodes[nodes[ugIndex].adj[rec]].valid && nodes[nodes[ugIndex].adj[rec]].time < year) {
44aa78ff   Eduardo Eloy   implementation at...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
                            sum = g(first, nodes[ugIndex].adj[rec], year, sum);
                        }
                    }
                }
            }
        }
        return sum;
    }



    // same as gladed but without the "isInstatiated" checks because it doesn't call gPropagate
    public int glade(int ugIndex, int period) throws Exception{


        return g(ugIndex, ugIndex, period, 0);

    }



    @Override
    public ESat isEntailed() { //When it gets here everything is already instantiated, but it doesn't backtrack

        //this works basically the same way as the propagate function but it doesn't check for instantiations
        //and returns ESat.FALSE instead of fails()

        int ugPresc = vars[ugIndex].getValue();

        int prescIndex = 0;

        ArrayList<Integer> SUMS = new ArrayList<>(); //for testing purposes

        for (int i = 0; i < nodes[ugIndex].presc.length; i++) {
            if (nodes[ugIndex].presc[i] == ugPresc) {
                prescIndex = i; //we get the index of the PRESC this UG is taking
            }
        }

        if (nodes[ugIndex].years[prescIndex][0] > 0) { //if this UG with this PREC has cuts

            for(int i = 0; i < nodes.length; i++)
844958a5   Eduardo Eloy   fixed infinite lo...
242
                nodes[i].time = 0;
44aa78ff   Eduardo Eloy   implementation at...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

            for (int p = 0; p < nodes[ugIndex].years[prescIndex].length; p++) { //for every year that our UG has cuts

                int year = nodes[ugIndex].years[prescIndex][p]; //current year
                try {
                    int sum = glade(ugIndex, year); //we get the sum
                    SUMS.add(sum);
                } catch (Exception e) {
                    return ESat.FALSE;
                }

                //SUMS.add(sum);
            }
        }

29a181fe   Eduardo Eloy   paretoworking and...
258

44aa78ff   Eduardo Eloy   implementation at...
259
        //uses the SUMS list to print the continuous sum of area cut starting from each UG for each year where that UG has a cut
29a181fe   Eduardo Eloy   paretoworking and...
260
261
262
263
264
265
        for(int i = 0; i < SUMS.size(); i++){
            //System.out.println("UG_"+(ugIndex+1)+" PR_"+nodes[ugIndex].presc[prescIndex]+" Y_"+nodes[ugIndex].periods[prescIndex][i]+" SUM_"+SUMS.get(i));
        }

        //woodYield[ugIndex] = model.intVar((int)nodes[ugIndex].wood_total[prescIndex]);
        return ESat.TRUE;
44aa78ff   Eduardo Eloy   implementation at...
266
267
    }
}