-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCounter.java
More file actions
240 lines (222 loc) · 8.77 KB
/
Counter.java
File metadata and controls
240 lines (222 loc) · 8.77 KB
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
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
package com.company;
import java.util.ArrayList;
import java.util.Random;
public class Counter {
String tempX = "000000";
private int nState = 10;
private int nCombinations = 64;
String[][] forbiddenCombinations = new String[nState][nCombinations];
private String[] state = new String[nState];
private String tempState = Variables.startState;
private int flagState;
private String tempY;
public String getTempState() {
return tempState;
}
public void setTempState(String tempState) {
this.tempState = tempState;
}
void InputState() {
// if(Main.fileOrNot.compareTo("N") == 0 || Main.fileOrNot.compareTo("n") == 0) {
// flagState = 7;
// state[0] = "00001";
// state[1] = "00010";
// state[2] = "00011";
// state[3] = "00100";
// state[4] = "00101";
// state[5] = "00110";
// state[6] = "01000";
// state[7] = " ";
// state[8] = " ";
// state[9] = " ";
// }
// if(Main.fileOrNot.compareTo("Y") == 0 || Main.fileOrNot.compareTo("y") == 0) {
flagState = 1;
state[0] = Variables.startState;
for(int i=1; i < nState; i++)
state[i] = " ";
// }
}
void InputForbiddenCombinations(String[][]mass){
for(int i=0; i < nState; i++)
for(int j = 0; j < nCombinations; j++)
mass[i][j] = " ";
}
Lock lock = new Lock();
ArrayList<String> listY = new ArrayList<>();
static int countPodborov = 0;
void BruteForce() {
for (int i = 0; i < nCombinations; i++) {
countPodborov++;
int tempI = i + nCombinations;
tempX = (Integer.toBinaryString(tempI));
tempX = tempX.substring(1);
tempY = lock.LockAlgorithm(tempX);
if (CheckY()) {
listY.add(tempState);
CheckState();
break;
}
else {
for(int j = 1; j < (nCombinations-1) - i; j++) {
if (BanCheck(j)) {
i += j;
}
else
break;
}
if (i == nCombinations - 1) {
int count = 0;
for(int k=0; k < nCombinations; k++) {
if (forbiddenCombinations[NumberState(getTempState())][k].compareTo(" ") != 0)
count++;
}
if(forbiddenCombinations[NumberState(getTempState())][0].compareTo(" ") == 0 || (forbiddenCombinations[NumberState(tempState)][0].compareTo(deadlockList[NumberState(tempState)][0]) == 0) && count == 1) {
//System.out.println("+");
DeadlockState();
break;
}
RandForce();
//System.out.println("+");
//FullForce();
tempX = whiteList[NumberState(getTempState())];
tempY = lock.LockAlgorithm(tempX);
setTempState(tempY);
listY.add(tempState);
// CheckY();
break;
}
}
}
}
boolean CheckState() {
for(int i = 0; i < nState; i++) {
if (getTempState().compareTo(state[i]) == 0)
return true;
}
state[flagState] = getTempState();
flagState ++;
return false;
}
boolean CheckY() {
if (tempY.compareTo(getTempState()) != 0) {
//if (tempX.compareTo("111111") != 0)
AddForbiddenCombinations();
setTempState(tempY) ;
return true;
}
else
return false;
}
int NumberState(String y){
for (int i = 0; i < nState; i++){
if(y.compareTo(state[i]) == 0)
return i;
}
return 1000;
}
boolean BanCheck(int num) {
String strBan = tempX;
int intTempX;
intTempX = (Integer.parseInt(strBan, 2));
intTempX = intTempX + nCombinations + num;
strBan = (Integer.toBinaryString(intTempX));
strBan = strBan.substring(1);
for (int i=0; i< nCombinations; i++) {
if (forbiddenCombinations[NumberState(getTempState())][i].compareTo(strBan) == 0 ) {
//System.out.println(strBan);
return true;
}
}
return false;
}
boolean AddForbiddenCombinations(){
for(int i=0; i < nCombinations; i++) {
if (forbiddenCombinations[NumberState(getTempState())][i].compareTo(tempX) == 0)
return false;
}
for(int i=0; i < nCombinations; i++) {
if (forbiddenCombinations[NumberState(getTempState())][i].compareTo(" ") == 0) {
forbiddenCombinations[NumberState(getTempState())][i] = tempX;
//System.out.println(forbiddenCombinations[NumberState(tempState)][i]);
return true;
}
}
return false;
}
String[] whiteList = new String[nState];
void RandForce(){
Random rand = new Random();
int count = 0;
for(int i=0; i < nCombinations; i++) {
if (forbiddenCombinations[NumberState(getTempState())][i].compareTo(" ") != 0)
count++;
}
int randCombination = rand.nextInt(count);
//System.out.println(randCombination);
randCombination = CheckDeadlock(randCombination,count,rand);
String whiteCombination = forbiddenCombinations[NumberState(getTempState())][randCombination];
whiteList[NumberState(getTempState())] = whiteCombination;
}
int[] countCombinations = new int[nCombinations];
void InputCountCombinations(){
for (int i=0; i<nCombinations;i++)
countCombinations[i] = -1;
}
void FullForce(){
// System.out.println("+");
int count = 0;
for(int i=0; i < nCombinations; i++) {
if (forbiddenCombinations[NumberState(getTempState())][i].compareTo(" ") != 0)
count++;
}
countCombinations[NumberState(getTempState())] ++;
countCombinations[NumberState(getTempState())] %= count;
if(forbiddenCombinations[NumberState(getTempState())][ countCombinations[NumberState(getTempState())]].compareTo(deadlockList[NumberState(getTempState())][0]) == 0) {
// System.out.println("+");
countCombinations[NumberState(getTempState())] ++;
countCombinations[NumberState(getTempState())] %= count;
}
String whiteCombination = forbiddenCombinations[NumberState(getTempState())][ countCombinations[NumberState(getTempState())]];
// System.out.println(whiteCombination);
whiteList[NumberState(getTempState())] = whiteCombination;
}
int CheckDeadlock(int randCombination, int count, Random rand){
//System.out.println("+");
while (1==1) {
boolean noDead = false;
for (int i = 0; i < nCombinations; i++) {
noDead = true;
if (forbiddenCombinations[NumberState(getTempState())][randCombination].compareTo(deadlockList[NumberState(getTempState())][i]) == 0) {
randCombination = rand.nextInt(count);
noDead = false;
break;
}
}
if (noDead) {
// System.out.println(randCombination);
return randCombination;
}
}
}
String[][] deadlockList = new String[nState][nCombinations];
void DeadlockState(){
// System.out.println(mas[1]);
for(int i=0; i < nCombinations; i++) {
if(forbiddenCombinations[NumberState(listY.get(listY.size() - 2))][i].compareTo(" ") == 0){
// System.out.println(mas[1]);
//System.out.println(listY.get(listY.size() - 2));
for (int j = 0; j < nCombinations; j++) {
if(deadlockList[NumberState(listY.get(listY.size() - 2))][j].compareTo(" ") == 0) {
deadlockList[NumberState(listY.get(listY.size() - 2))][j] = forbiddenCombinations[NumberState(listY.get(listY.size() - 2))][i - 1];
break;
}
}
break;
}
}
tempY = Variables.startState;
setTempState(Variables.startState);
lock.setY(Variables.startState);
}
}