Skip to content

Commit

Permalink
cleaning initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
hofarah committed Nov 8, 2021
1 parent 23c31f9 commit a5bb39d
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 211 deletions.
41 changes: 0 additions & 41 deletions src/A_Star.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,47 +30,6 @@ public static void search(State initialState) {
}
}

private static boolean isGoal(State state) {
for (int i = 0; i < state.getGraph().size(); i++) {
if (state.getGraph().getNode(i).getColor() == Color.Red
|| state.getGraph().getNode(i).getColor() == Color.Black) {
return false;
}
}
return true;
}

private static void result(State state) {
Stack<State> states = new Stack<State>();
while (true) {
states.push(state);
if (state.getParentState() == null) {
break;
} else {
state = state.getParentState();
}
}
try {
FileWriter myWriter = new FileWriter("BfsResult.txt");
System.out.println("initial state : ");
while (!states.empty()) {
State tempState = states.pop();
if (tempState.getSelectedNodeId() != -1) {
System.out.println("selected id : " + tempState.getSelectedNodeId());
}
tempState.getGraph().print();

myWriter.write(tempState.getSelectedNodeId() + " ,");
myWriter.write(tempState.outputGenerator() + "\n");
}
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}

}


48 changes: 4 additions & 44 deletions src/BFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public static void search(State initialState) {
Queue<State> frontier = new LinkedList<State>();
Hashtable<String, Boolean> inFrontier = new Hashtable<>();
Hashtable<String, Boolean> explored = new Hashtable<>();
if (isGoal(initialState)) {
result(initialState);
if (initialState.isGoal()) {
initialState.result();
return;
}
frontier.add(initialState);
Expand All @@ -26,8 +26,8 @@ public static void search(State initialState) {
for (int i = 0; i < children.size(); i++) {
if (!(inFrontier.containsKey(children.get(i).hash()))
&& !(explored.containsKey(children.get(i).hash()))) {
if (isGoal(children.get(i))) {
result(children.get(i));
if (children.get(i).isGoal()) {
children.get(i).result();
return;
}
frontier.add(children.get(i));
Expand All @@ -37,46 +37,6 @@ public static void search(State initialState) {
}
}

private static boolean isGoal(State state) {
for (int i = 0; i < state.getGraph().size(); i++) {
if (state.getGraph().getNode(i).getColor() == Color.Red
|| state.getGraph().getNode(i).getColor() == Color.Black) {
return false;
}
}
return true;
}

private static void result(State state) {
Stack<State> states = new Stack<State>();
while (true) {
states.push(state);
if (state.getParentState() == null) {
break;
} else {
state = state.getParentState();
}
}
try {
FileWriter myWriter = new FileWriter("BfsResult.txt");
System.out.println("initial state : ");
while (!states.empty()) {
State tempState = states.pop();
if (tempState.getSelectedNodeId() != -1) {
System.out.println("selected id : " + tempState.getSelectedNodeId());
}
tempState.getGraph().print();

myWriter.write(tempState.getSelectedNodeId() + " ,");
myWriter.write(tempState.outputGenerator() + "\n");
}
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}


41 changes: 0 additions & 41 deletions src/Greedy.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,47 +33,6 @@ public static void search(State initialState) {
}
}

private static boolean isGoal(State state) {
for (int i = 0; i < state.getGraph().size(); i++) {
if (state.getGraph().getNode(i).getColor() == Color.Red
|| state.getGraph().getNode(i).getColor() == Color.Black) {
return false;
}
}
return true;
}

private static void result(State state) {
Stack<State> states = new Stack<State>();
while (true) {
states.push(state);
if (state.getParentState() == null) {
break;
} else {
state = state.getParentState();
}
}
try {
FileWriter myWriter = new FileWriter("BfsResult.txt");
System.out.println("initial state : ");
while (!states.empty()) {
State tempState = states.pop();
if (tempState.getSelectedNodeId() != -1) {
System.out.println("selected id : " + tempState.getSelectedNodeId());
}
tempState.getGraph().print();

myWriter.write(tempState.getSelectedNodeId() + " ,");
myWriter.write(tempState.outputGenerator() + "\n");
}
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}

}


41 changes: 0 additions & 41 deletions src/IDA_Star.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,47 +45,6 @@ public static void search(State initialState) {
}
}

private static boolean isGoal(State state) {
for (int i = 0; i < state.getGraph().size(); i++) {
if (state.getGraph().getNode(i).getColor() == Color.Red
|| state.getGraph().getNode(i).getColor() == Color.Black) {
return false;
}
}
return true;
}

private static void result(State state) {
Stack<State> states = new Stack<State>();
while (true) {
states.push(state);
if (state.getParentState() == null) {
break;
} else {
state = state.getParentState();
}
}
try {
FileWriter myWriter = new FileWriter("BfsResult.txt");
System.out.println("initial state : ");
while (!states.empty()) {
State tempState = states.pop();
if (tempState.getSelectedNodeId() != -1) {
System.out.println("selected id : " + tempState.getSelectedNodeId());
}
tempState.getGraph().print();

myWriter.write(tempState.getSelectedNodeId() + " ,");
myWriter.write(tempState.outputGenerator() + "\n");
}
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}

}


47 changes: 3 additions & 44 deletions src/RBFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ static float NewRBFS(State s, float best) {
if (best < s.f) {
return s.f;
}
if (isGoal(s)) {
result(s);
if (s.isGoal()) {
s.result();
return Float.MIN_VALUE;
}
ArrayList<State> children = s.successor();
Expand Down Expand Up @@ -46,51 +46,10 @@ static float NewRBFS(State s, float best) {
}

public static void search(State initialState) {
NewRBFS(initialState, 1000000000);
NewRBFS(initialState, Float.MAX_VALUE);
}


private static boolean isGoal(State state) {
for (int i = 0; i < state.getGraph().size(); i++) {
if (state.getGraph().getNode(i).getColor() == Color.Red
|| state.getGraph().getNode(i).getColor() == Color.Black) {
return false;
}
}
return true;
}


private static void result(State state) {
Stack<State> states = new Stack<State>();
while (true) {
states.push(state);
if (state.getParentState() == null) {
break;
} else {
state = state.getParentState();
}
}
try {
FileWriter myWriter = new FileWriter("BfsResult.txt");
System.out.println("initial state : ");
while (!states.empty()) {
State tempState = states.pop();
if (tempState.getSelectedNodeId() != -1) {
System.out.println("selected id : " + tempState.getSelectedNodeId());
}
tempState.getGraph().print();

myWriter.write(tempState.getSelectedNodeId() + " ,");
myWriter.write(tempState.outputGenerator() + "\n");
}
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}

}

Expand Down

0 comments on commit a5bb39d

Please sign in to comment.