📄 pathfinderalgorythms.cs
字号:
decision = rightDecision;
else
decision = rightDecision.Compare( decision );
}
if( bottomSquare != null
&& bottomDecision.IsValid == true )
{
if( decision.IsValid == false )
decision = bottomDecision;
else
decision = bottomDecision.Compare( decision );
}
if( leftSquare != null
&& leftDecision.IsValid == true )
{
if( decision.IsValid == false )
decision = leftDecision;
else
decision = leftDecision.Compare( decision );
}
/// update the current square before decisions are changed
if( currentAnt.HasFoundFood == true
&& currentAnt.IsReturningHome == true )
{
currentSquare.IncrementPathFoodPheromone();
currentSquare.IncrementPathFoodPheromone();
currentSquare.IncrementPathFoodPheromone();
}
DecideSquareForAnt( ref availableSquares, ref currentAnt, decision );
}
}
public void PheromonesAlgorythm( ref ArrayList availableSquares, ref Critter critter, DirectionLastTravelledSet directionLastTravelled, SquareDirectionsSet squareDirections )
{
/// get the squares
StandardSquare topSquare = ( StandardSquare )availableSquares[ ( int )ArrayPositionSet.TOP ];
StandardSquare rightSquare = ( StandardSquare )availableSquares[ ( int )ArrayPositionSet.RIGHT ];
StandardSquare bottomSquare = ( StandardSquare )availableSquares[ ( int )ArrayPositionSet.BOTTOM ];
StandardSquare leftSquare = ( StandardSquare )availableSquares[ ( int )ArrayPositionSet.LEFT ];
StandardSquare currentSquare = ( StandardSquare )availableSquares[ ( int )ArrayPositionSet.CURRENT ];
bool bIsTopAvailable = false;
bool bIsRightAvailable = false;
bool bIsBottomAvailable = false;
bool bIsLeftAvailable = false;
/// is the top available
if( squareDirections == SquareDirectionsSet.TOPBOTTOM || squareDirections == SquareDirectionsSet.TOPLEFT
|| squareDirections == SquareDirectionsSet.TOPRIGHT || squareDirections == SquareDirectionsSet.ALL
&& topSquare != null && topSquare.SquareType == "BOTTOMLEFT" || topSquare.SquareType == "BOTTOMRIGHT"
|| topSquare.SquareType == "FOURWAYS" || topSquare.SquareType == "TOPBOTTOM" || topSquare.SquareType == "FOOD"
|| topSquare.SquareType == "NEST" )
{
bIsTopAvailable = true;
}
/// is the right available
if( squareDirections == SquareDirectionsSet.BOTTOMRIGHT || squareDirections == SquareDirectionsSet.LEFTRIGHT
|| squareDirections == SquareDirectionsSet.TOPRIGHT || squareDirections == SquareDirectionsSet.ALL
&& rightSquare != null && rightSquare.SquareType == "LEFTRIGHT" || rightSquare.SquareType == "FOURWAYS"
|| rightSquare.SquareType == "BOTTOMLEFT" || rightSquare.SquareType == "TOPLEFT" || rightSquare.SquareType == "FOOD"
|| rightSquare.SquareType == "NEST" )
{
bIsRightAvailable = true;
}
/// is the bottom square available
if( squareDirections == SquareDirectionsSet.BOTTOMLEFT || squareDirections == SquareDirectionsSet.BOTTOMRIGHT
|| squareDirections == SquareDirectionsSet.TOPBOTTOM || squareDirections == SquareDirectionsSet.ALL
&& bottomSquare != null && bottomSquare.SquareType == "TOPBOTTOM" || bottomSquare.SquareType == "FOURWAYS"
|| bottomSquare.SquareType == "TOPLEFT" || bottomSquare.SquareType == "TOPRIGHT" || bottomSquare.SquareType == "FOOD"
|| bottomSquare.SquareType == "NEST" )
{
bIsBottomAvailable = true;
}
/// is the left square available
if( squareDirections == SquareDirectionsSet.BOTTOMLEFT || squareDirections == SquareDirectionsSet.LEFTRIGHT
|| squareDirections == SquareDirectionsSet.TOPLEFT || squareDirections == SquareDirectionsSet.ALL
&& leftSquare != null && leftSquare.SquareType == "LEFTRIGHT" || leftSquare.SquareType == "FOURWAYS"
|| leftSquare.SquareType == "BOTTOMRIGHT" || leftSquare.SquareType == "TOPRIGHT" || leftSquare.SquareType == "FOOD"
|| leftSquare.SquareType == "NEST" )
{
bIsLeftAvailable = true;
}
/// now do the ants
if( critter is Ant )
{
Ant currentAnt = ( Ant )critter;
int nTopPheromone = 0;
int nRightPheromone = 0;
int nBottomPheromone = 0;
int nLeftPheromone = 0;
/// get the pheromone counts
if( bIsTopAvailable == true && topSquare != null )
nTopPheromone = topSquare.PathFoodPheromone;
if( bIsRightAvailable == true && rightSquare != null )
nRightPheromone = rightSquare.PathFoodPheromone;
if( bIsBottomAvailable == true && bottomSquare != null )
nBottomPheromone = bottomSquare.PathFoodPheromone;
if( bIsLeftAvailable == true && leftSquare != null )
nLeftPheromone = leftSquare.PathFoodPheromone;
FuzzyDecision topDecision = new FuzzyDecision( "TOP" );
FuzzyDecision rightDecision = new FuzzyDecision( "RIGHT" );
FuzzyDecision bottomDecision = new FuzzyDecision( "BOTTOM" );
FuzzyDecision leftDecision = new FuzzyDecision( "LEFT" );
/// decide which way you want to move
/// At present there are only the available moves +
/// try to avoid occupied squares
/// work out the top decision
if( bIsTopAvailable == true )
{
/// validates the decision even if no pheromone
topDecision.IncrementDecision();
topDecision.IsValid = true;
}
if( bIsRightAvailable == true )
{
rightDecision.IncrementDecision();
rightDecision.IsValid = true;
}
if( bIsBottomAvailable == true )
{
bottomDecision.IncrementDecision();
bottomDecision.IsValid = true;
}
if( bIsLeftAvailable == true )
{
leftDecision.IncrementDecision();
leftDecision.IsValid = true;
}
/// choose the square
/// Check that the potential squares are valid and that the decision
/// for moving there is a valid decision.
FuzzyDecision decision = new FuzzyDecision();
/// if one is the home square move to it
if( critter.IsReturningHome == true )
{
if( topSquare != null
&& topSquare.SquareType == "NEST" )
{
topDecision.IncrementDecision();
}
if( rightSquare != null
&& rightSquare.SquareType == "NEST" )
{
rightDecision.IncrementDecision();
}
if( bottomSquare != null
&& bottomSquare.SquareType == "NEST" )
{
bottomDecision.IncrementDecision();
}
if( leftSquare != null
&& leftSquare.SquareType == "NEST" )
{
leftDecision.IncrementDecision();
}
}
/// if searching moving backwards is undesirable
if( currentAnt.IsReturningHome == false )
{
if( directionLastTravelled == DirectionLastTravelledSet.TOP
&& bottomDecision.IsValid == true )
bottomDecision.DecrementDecision();
else if( directionLastTravelled == DirectionLastTravelledSet.RIGHT
&& leftDecision.IsValid == true )
leftDecision.DecrementDecision();
else if( directionLastTravelled == DirectionLastTravelledSet.BOTTOM
&& topDecision.IsValid == true )
topDecision.DecrementDecision();
else if( directionLastTravelled == DirectionLastTravelledSet.LEFT
&& rightDecision.IsValid == true )
rightDecision.DecrementDecision();
else
{
}
/// improve the chances of moving forwards
if( directionLastTravelled == DirectionLastTravelledSet.TOP
&& topDecision.IsValid == true )
topDecision.IncrementDecision();
else if( directionLastTravelled == DirectionLastTravelledSet.RIGHT
&& rightDecision.IsValid == true )
rightDecision.IncrementDecision();
else if( directionLastTravelled == DirectionLastTravelledSet.BOTTOM
&& bottomDecision.IsValid == true )
bottomDecision.IncrementDecision();
else if( directionLastTravelled == DirectionLastTravelledSet.LEFT
&& leftDecision.IsValid == true )
leftDecision.IncrementDecision();
else
{
}
}
/// try to avoid any squares that are already occupied
if( currentAnt.TryToAvoidOccupiedSquares == true
&& currentAnt.IsReturningHome == false )
{
if( topSquare != null
&& topSquare.IsCritterOnSquare == true
&& topDecision.IsValid == true )
{
topDecision.DecrementDecision();
topDecision.DecrementDecision();
}
if( rightSquare != null
&& rightSquare.IsCritterOnSquare == true
&& rightDecision.IsValid == true )
{
rightDecision.DecrementDecision();
rightDecision.DecrementDecision();
}
if( bottomSquare != null
&& bottomSquare.IsCritterOnSquare == true
&& bottomDecision.IsValid == true )
{
bottomDecision.DecrementDecision();
bottomDecision.DecrementDecision();
}
if( leftSquare != null
&& leftSquare.IsCritterOnSquare == true
&& leftDecision.IsValid == true )
{
leftDecision.DecrementDecision();
leftDecision.DecrementDecision();
}
}
/// see if we are repeating the same moves over and over
if( currentAnt.IsRepeatingOnce == true
&& currentAnt.IsReturningHome == false
&& currentSquare.Name != "NEST" )
{
/// see if there is another way to move
switch( currentAnt.GetDirectionTravelledAt( 3 ) )
{
case ( int )DirectionLastTravelledSet.NONE: ; break;
case ( int )DirectionLastTravelledSet.TOP:
{
if( rightDecision.IsValid == true )
rightDecision.IncrementDecision();
if( bottomDecision.IsValid == true )
{
bottomDecision.IncrementDecision();
}
if( leftDecision.IsValid == true )
leftDecision.IncrementDecision();
}break;
case ( int )DirectionLastTravelledSet.RIGHT:
{
if( topDecision.IsValid == true )
topDecision.IncrementDecision();
if( bottomDecision.IsValid == true )
bottomDecision.IncrementDecision();
if( leftDecision.IsValid == true )
{
leftDecision.IncrementDecision();
}
}break;
case ( int )DirectionLastTravelledSet.BOTTOM:
{
if( topDecision.IsValid == true )
{
topDecision.IncrementDecision();
}
if( rightDecision.IsValid == true )
rightDecision.IncrementDecision();
if( leftDecision.IsValid == true )
leftDecision.IncrementDecision();
}break;
case ( int )DirectionLastTravelledSet.LEFT:
{
if( topDecision.IsValid == true )
topDecision.IncrementDecision();
if( rightDecision.IsValid == true )
{
rightDecision.IncrementDecision();
}
if( bottomDecision.IsValid == true )
bottomDecision.IncrementDecision();
}break;
default:; break;
}
}
/// check for pheromone following
/// Kind of absolute here
/// For ants that haven't found food
if( currentAnt.IsReturningHome == false
&& topSquare != null
&& topSquare.SquareType != "NEST"
&& topSquare.PathFoodPheromone > 0
&& currentAnt.HasFoundFood == false
&& currentAnt.FindFood == true
&& topDecision.IsValid == true )
{
switch( topSquare.FoodPheromoneDirection )
{
case "BOTTOM":
{
topDecision.IncrementDecision();
topDecision.IncrementDecision();
}break;
default:
{
topDecision.IncrementDecision();
}break;
}
}
if( currentAnt.IsReturningHome == false
&& rightSquare != null
&& rightSquare.SquareType != "NEST"
&& rightSquare.PathFoodPheromone > 0
&& currentAnt.HasFoundFood == false
&& currentAnt.FindFood == true
&& rightDecision.IsValid == true )
{
switch( rightSquare.FoodPheromoneDirection )
{
case "LEFT":
{
rightDecision.IncrementDecision();
rightDecision.IncrementDecision();
}break;
default:
{
rightDecision.IncrementDecision();
}break;
}
}
if( currentAnt.IsReturningHome == false
&& bottomSquare != null
&& bottomSquare.SquareType != "NEST"
&& bottomSquare.PathFoodPheromone > 0
&& currentAnt.HasFoundFood == false
&& currentAnt.FindFood == true
&& bottomDecision.IsValid == true )
{
switch( bottomSquare.FoodPheromoneDirection )
{
case "TOP":
{
bottomDecision.IncrementDecision();
bottomDecision.IncrementDecision();
}break;
default:
{
bottomDecision.IncrementDecision();
}break;
}
}
if( currentAnt.IsReturningHome == false
&& leftSquare != null
&& leftSquare.SquareType != "NEST"
&& leftSquare.PathFoodPheromone > 0
&& currentAnt.HasFoundFood == false
&& currentAnt.FindFood == true
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -