bestFirst方法计算给定目标状态和给定初始值“ MI”的路径(使用MIU系统)。当我尝试在主类中调用bestFirst方法时,该方法永不终止。例如,如果您尝试使用输入目标状态“ MUUUIIIII”,则BFS(breadthFirstSearch)输出应为“找不到目标字符串MUUUIIIII”,并且bestFirst应输出答案。
谁能找到原因? 谢谢
主类代码:
public class main {
public static void main(String[] args) {
MIU miu = new MIU();
Scanner sc = new Scanner(System.in);
String s = "";
ArrayList<String> p = new ArrayList<String>();
System.out.println("\n" +"What is your goal string?");
String goalStr = sc.nextLine();
System.out.println("BFS:");
try
{
ArrayList<String> curPath = miu.breadthFirstSearch(goalStr);
for (int i=0; i<curPath.size(); i++)
{
System.out.print(curPath.get(i) + ", ");
}
} catch (NullPointerException E)
{
System.out.println("Cannot find the goal String " + goalStr);
}
System.out.println("\n" + "\n" + "Best First: ");
try
{
ArrayList<String> path = miu.bestFirst(goalStr);
for (int i=0; i<path.size(); i++)
{
System.out.print(path.get(i) + ", ");
}
} catch (NullPointerException E)
{
System.out.println("Cannot find the goal String " + goalStr);
}
System.out.println("\n" + "\n" + "A*: ");
try
{
ArrayList<String> path = miu.AStar(goalStr);
for (int i=0; i<path.size(); i++)
{
System.out.print(path.get(i) + ", ");
}
} catch (NullPointerException E)
{
System.out.println("Cannot find the goal String " + goalStr);
}
}
}
MIU类代码:
public class MIU {
public ArrayList<String> nextStates = new ArrayList<String>();
public ArrayList<ArrayList<String>> paths = new ArrayList<>();
public void rule1(String s)
{
String n= "";
if (s.charAt(s.length() - 1) == 'I')
{
n = s+'U';
if(!nextStates.contains(n))
nextStates.add(n);
}
}
public void rule2(String s)
{
String n = "";
if(s.charAt(0) == 'M')
{
n = s.substring(0, 1) + s.substring(1, s.length()) + s.substring(1, s.length());
if(!nextStates.contains(n)) nextStates.add(n);
}
}
public void rule3(String s)
{
String n= "";
if(s.contains("III"))
{
for (int i=0; i<s.length()-2; i++)
{
if (s.charAt(i) == 'I' && s.charAt(i+1) == 'I' && s.charAt(i+2) == 'I')
{
n = s.substring(0, i) + 'U' + s.substring(i+3, s.length());
if(!nextStates.contains(n)) nextStates.add(n);
}
}
}
}
public void rule4(String s)
{
String n= "";
if(s.contains("UU"))
{
for (int i=0; i<s.length()-2; i++) {
if (s.charAt(i) == 'U' && s.charAt(i + 1) == 'U') {
n = s.substring(0, i) + s.substring(i + 2, s.length());
if(!nextStates.contains(n)) nextStates.add(n);
}
}
}
}
public ArrayList<String> next_States(String s)
{
rule1(s);
rule2(s);
rule3(s);
rule4(s);
return nextStates;
}
public ArrayList<ArrayList<String>> extendPath(ArrayList<String> p)
{
nextStates.clear();
String last = p.get(p.size()-1);
next_States(last);
for (int i=0; i<nextStates.size(); i++)
{
ArrayList<String> temp_states = new ArrayList<>();
temp_states.addAll(p);
temp_states.add(nextStates.get(i));
paths.add(temp_states);
}
return paths;
}
public ArrayList<String> breadthFirstSearch (String goalString)
{
nextStates.clear();
//agenda is a list of paths
//path is a list of states
ArrayList<ArrayList<String>> agenda = new ArrayList<>();
ArrayList<String> mi = new ArrayList<>();
mi.add("MI");
agenda.add(mi);
ArrayList<String> currentPath = agenda.get(0);
int extendPathCounter = 0;
int lim = 300;
while (lim >= extendPathCounter)
{
if (currentPath.get(currentPath.size()-1).equals(goalString))
{
System.out.println("Lenght of path = " + currentPath.size());
System.out.println("Number of calls of extendPath = " + extendPathCounter);
System.out.println("Size of agenda = " + agenda.size());
return currentPath;
}
else {
paths.clear();
extendPath(currentPath);
extendPathCounter++;
for (int i=0; i<paths.size(); i++)
{
agenda.add(paths.get(i));
}
agenda.remove(0);
currentPath = agenda.get(0);
}
}
return null; // TODO should return an empty list
}
public int estimateSteps(String currentString, String goalString)
{
if (currentString.equals(goalString)) return 0;
else return 1;
}
public ArrayList<String> bestFirst(String goalString)
{
nextStates.clear();
//agenda is a list of paths
//path is a list of states
ArrayList<ArrayList<String>> agenda = new ArrayList<>();
ArrayList<String> mi = new ArrayList<>();
mi.add("MI");
agenda.add(mi);
ArrayList<String> currentPath;
int extendPathCounter = 0;
while (agenda.size()!=0)
{
currentPath = agenda.get(0);
int index = 0;
for (int i=0; i<agenda.size(); i++)
{
if(estimateSteps(goalString, currentPath.get(currentPath.size()-1)) > estimateSteps(goalString, agenda.get(i).get(agenda.get(i).size()-1)))
{
currentPath = agenda.get(i);
index = i;
}
}
agenda.remove(index); //remove the one that is the current path
if (currentPath.get(currentPath.size()-1).equals(goalString))
{
System.out.println("Lenght of path = " + currentPath.size());
System.out.println("Number of calls of extendPath = " + extendPathCounter);
System.out.println("Size of agenda = " + agenda.size());
return currentPath;
}
else
{
paths.clear();
extendPath(currentPath);
extendPathCounter++;
for (int i=0; i<paths.size(); i++)
{
agenda.add(paths.get(i));
}
}
}
return null;
}
public ArrayList<String> AStar(String goalString)
{
nextStates.clear();
//agenda is a list of paths
//path is a list of states
ArrayList<ArrayList<String>> agenda = new ArrayList<>();
ArrayList<String> mi = new ArrayList<>();
mi.add("MI");
agenda.add(mi);
ArrayList<String> currentPath;
int extendPathCounter = 0;
while (agenda.size()!=0)
{
int heuristics;
int pathLen;
int sum;
currentPath = agenda.get(0);
int index = 0;
heuristics = estimateSteps(goalString, currentPath.get(currentPath.size()-1));
pathLen = currentPath.size();
sum = heuristics + pathLen;
for (int i= 0; i<agenda.size(); i++)
{
int h = estimateSteps(goalString, agenda.get(i).get(agenda.get(i).size()-1));
int pl = agenda.get(i).size();
int s = h+pl;
if(s<sum)
{
currentPath = agenda.get(i);
index = i;
}
}
agenda.remove(index);
if (currentPath.get(currentPath.size()-1).equals(goalString))
{
System.out.println("Lenght of path = " + currentPath.size());
System.out.println("Number of calls of extendPath = " + extendPathCounter);
System.out.println("Size of agenda = " + agenda.size());
return currentPath;
}
else
{
paths.clear();
extendPath(currentPath);
extendPathCounter++;
for (int i=0; i<paths.size(); i++)
{
agenda.add(paths.get(i));
}
}
}
return null;
}
}