-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathForest.java
202 lines (171 loc) · 6.11 KB
/
Forest.java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import java.util.ArrayList;
public class Forest {
private ArrayList<ArrayList<Double>> probtreeburn=new ArrayList<>();//prob of each tree can burn or not
private ArrayList<ArrayList<Integer>> forest=new ArrayList<>();
private ArrayList<Integer> tmpburni;//store burnning point i
private ArrayList<Integer> tmpburnj;//store burnning point j tmpburni.add(30);
private double probtree = 0;
private double probburn = 0;
private double probtreestartburn = 0;
private int size = 0 ;
double random;
public Forest(int size,double probburn,double probtreestartburn,double probtree){
tmpburni=new ArrayList<>();
tmpburnj=new ArrayList<>();
this.size = size;
this.probburn = probburn;
this.probtree=probtree;
this.probtreestartburn=probtreestartburn;
build();
//randomStartBurn(2);
assignTree();
}
public void reConstruct(){
build();
//randomStartBurn(2);
assignTree();
}
public void setSize(int sz){
size=sz;
}
public void setProbBurn(double pb){
probburn=pb;
}
public void setProbTreeStartBurn(double tsb){
probtreestartburn=tsb;
}
public void setProbTree(double pt){
probburn=pt;
}
public int getSize(){
return size;
}
public ArrayList<ArrayList<Double>> getBurnProb(){
return probtreeburn;
}
public void build(){
probtreeburn.clear();
forest.clear();
for (int i = 0; i < size; i++) {
ArrayList<Double> tmpProb=new ArrayList<>();
ArrayList<Integer> tmpForest=new ArrayList<>();
for (int j = 0; j < size; j++) {
//System.otu.print(1);
if(i == 0 || j == 0 || i == size - 1 || j == size - 1) {
//border
tmpForest.add(0);
tmpProb.add(randomProb());
}/*else if (i == size / 2 && j == size / 2) {
//assign center point is burning
tmpForest.add(2);
tmpburni.add(i);
tmpburnj.add(j);
tmpProb.add(randomProb());
}*/else {
//normal tree
tmpForest.add(1);
tmpProb.add(randomProb());
}
}
//System.out.println();
probtreeburn.add(tmpProb);
forest.add(tmpForest);
}
}
public void randomStartBurn(int c){
// 0 0 0 0
// 0 1 1 0
// 0 1 1 0
// 0 0 0 0
tmpburni.clear();
tmpburnj.clear();
for(int k=0;k<c;k++){
double range = ((size-2) - 1) + 1;
int i=(int)(Math.random() * range) + 1;//between 1 to size-2
int j=(int)(Math.random() * range) + 1;//
forest.get(i).set(j,2);
tmpburni.add(i);
tmpburnj.add(j);
}
}
public boolean isBurning(){
// if(tmpburni.size()!=0) return true;
// return false;
return tmpburni.size()!=0;
}
public ArrayList<ArrayList<Integer>> getForest(){
return forest;
}
public ArrayList<ArrayList<Double>> getProbTree(){
return probtreeburn;
}
public double randomProb() {
random = Math.random();
return random;
}
public void fireSpread() {
ArrayList<Integer> tmpi=new ArrayList<>();
ArrayList<Integer> tmpj=new ArrayList<>();
for (int k = 0; k < tmpburni.size(); k++) {
//for (int j = 1; j < forest.get(i).size()-1; j++) {
// if (forest.get(i).get(j) == 2) {
int i=tmpburni.get(k);
int j=tmpburnj.get(k);
//RIGHT
if(forest.get(i+1).get(j) == 1 && probtreeburn.get(i+1).get(j) < probburn){
forest.get(i+1).set(j,2);//set burn to 2
tmpi.add(i+1);
tmpj.add(j);
}
//LEFT
if(forest.get(i-1).get(j) == 1 && probtreeburn.get(i-1).get(j) < probburn){
forest.get(i-1).set(j,2);
tmpi.add(i-1);
tmpj.add(j);
}
//TOP
if(forest.get(i).get(j+1) == 1 && probtreeburn.get(i).get(j+1) < probburn){
forest.get(i).set(j+1,2);
tmpi.add(i);
tmpj.add(j+1);
}
//BOTTOM
if(forest.get(i).get(j-1) == 1 && probtreeburn.get(i).get(j-1) < probburn){
forest.get(i).set(j-1,2);
tmpi.add(i);
tmpj.add(j-1);
}
//SET old 2 to 0
forest.get(i).set(j,0);
//}
//}
}
//assign new arraylist of point that has 2
tmpburni=tmpi;
tmpburnj=tmpj;
}
public void assignTree(){
for(int i =1;i<forest.size()-1;i++){
for(int j = 1;j<forest.size()-1;j++){
if(randomProb() < probtree){
forest.get(i).set(j,1);
}else{
forest.get(i).set(j,0);
}
}
}
}
public void assignFire(double probtreestartburnIn){
for(int i =1;i<forest.size()-1;i++){
for(int j = 1;j<forest.size()-1;j++){
if(forest.get(i).get(j) ==1){
if(randomProb() < probtreestartburnIn){
forest.get(i).set(j,2);
tmpburni.add(i);
tmpburnj.add(j);
}
}
}
}
}
}