私はJavaを扱うのが初めてで、私が取っているコースはコードを示していますが、それを実行しようとしているときです。親が設定されていないため、null ポインター例外が返されます。
では、抽象クラスで親を渡すにはどうすればよいでしょうか? これはAI検索に基づいています!ありがとう!!
コードは次のとおりです。
これは、農夫オオカミ山羊のキャベツ問題に基づいています。
抽象状態:
package hw1;
public abstract class AbstractState implements State
{
public State parent = null;
private double distance = 0;
public AbstractState(){}
public AbstractState(State Parent)
{
this.parent = parent;
this.distance = parent.getDistance() +1;
}
public State getParent()
{
return parent;
}
public double getDistance()
{
return distance;
}
}
州
package hw1;
import java.util.Set;
public interface State
{
public Iterable<State> getPossibleMoves();
public boolean isSolution();
public double getHeuristic();
public double getDistance();
public State getParent();
}
FarmerWolfヤギキャベツ:
package hw1;
import java.util.HashSet;
import java.util.Set;
public class FarmerWolfGoatState extends AbstractState
{
enum Side
{
EAST
{
public Side getOpposite()
{
return WEST;
}
},
WEST
{
public Side getOpposite()
{
return EAST;
}
};
abstract public Side getOpposite();
}
private Side farmer = Side.EAST;
private Side wolf = Side.EAST;
private Side goat = Side.EAST;
private Side cabbage = Side.EAST;
public FarmerWolfGoatState()
{}
public FarmerWolfGoatState(FarmerWolfGoatState parent, Side farmer, Side wolf, Side goat, Side Cabbage)
{
super(parent);
this.farmer = farmer;
this.wolf = wolf;
this.goat = goat;
this.cabbage = cabbage;
}
@Override
public Iterable<State> getPossibleMoves() {
Set<State> moves = new HashSet<State>();
if(farmer == wolf)
new FarmerWolfGoatState(this,
farmer.getOpposite()
,wolf.getOpposite()
,goat,cabbage).addIfSafe(moves);
if(farmer == goat)
new FarmerWolfGoatState(this,
farmer.getOpposite()
,wolf
,goat.getOpposite(),cabbage).addIfSafe(moves);
if(farmer == cabbage)
new FarmerWolfGoatState(this,
farmer.getOpposite()
,wolf
,goat,cabbage.getOpposite()).addIfSafe(moves);
new FarmerWolfGoatState(this, farmer.getOpposite(), wolf, goat, cabbage).addIfSafe(moves);
return moves;
}
@Override
public boolean isSolution() {
//returns true if all of the them are on the west side and not the east.
return farmer == Side.WEST && wolf == Side.WEST && goat==Side.WEST && cabbage == Side.WEST;
}
@Override
public double getHeuristic() {
// TODO Auto-generated method stub
return 0;
}
public final void addIfSafe(Set<State> moves)
{
boolean unsafe = (farmer!= wolf && farmer != goat) || (farmer != goat && farmer != cabbage);
if(!unsafe)
moves.add(this);
}
public boolean equals(Object o)
{
if(o == null || !(o instanceof FarmerWolfGoatState))
return false;
FarmerWolfGoatState fwgs = (FarmerWolfGoatState)o;
return farmer == fwgs.farmer &&
wolf == fwgs.wolf &&
cabbage == fwgs.cabbage &&
goat == fwgs.goat;
}
public int hashCode()
{
return(farmer == Side.EAST ? 1 : 0) +
(wolf == Side.EAST ? 2 : 0) +
(cabbage == Side.EAST ? 4: 0)+
(goat == Side.EAST ? 8 : 0);
}
}
主に解決しようとしています..
package hw1;
import hw1.FarmerWolfGoatState.Side;
public class Homework1 {
/**
* @param args
*/
public static void main(String[] args)
{
FarmerWolfGoatState parentState = new FarmerWolfGoatState();
FarmerWolfGoatState nextState = new FarmerWolfGoatState(parentState,Side.EAST,Side.EAST,Side.EAST,Side.WEST);
while(!nextState.isSolution())
{
nextState.getPossibleMoves();
}
}
}
スタックトレース:
Exception in thread "main" java.lang.NullPointerException
at hw1.AbstractState.<init>(AbstractState.java:12)
at hw1.FarmerWolfGoatState.<init>(FarmerWolfGoatState.java:38)
at hw1.Homework1.main(Homework1.java:15)
ソルバーも与えられましたが、それを使用する必要がありますか?
here it is.
package hw1;
import java.util.Stack;
public class DepthFirstSolver extends AbstractSolver
{
private Stack<State> stack = new Stack<State>();
@Override
protected void addState(State s)
{
if(!stack.contains(s))
{
stack.push(s);
}
}
@Override
protected boolean hasElements()
{
return !stack.empty();
}
@Override
protected State nextState()
{
return stack.pop();
}
@Override
protected void clearOpen()
{
stack.clear();
}
}
package hw1;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public abstract class AbstractSolver implements Solver
{
private Set<State> closed= new HashSet<State>();
public List<State> solve(State initialState)
{
closed.clear();
clearOpen();
while(hasElements())
{
State s = nextState();
if(s.isSolution())
return findPath(s);
closed.add(s);
Iterable<State> moves = s.getPossibleMoves();
for(State move : moves)
if(!closed.contains(move))
addState(move);
}
return null;
}
public int getVisitedStateCount()
{
return closed.size();
}
private List<State> findPath(State solution)
{
LinkedList<State> path = new LinkedList<State>();
while(solution!=null)
{
path.addFirst(solution);
solution = solution.getParent();
}
return path;
}
protected abstract boolean hasElements();
protected abstract State nextState();
protected abstract void addState(State s);
protected abstract void clearOpen();
}