⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pathfinderalgorythms.cs

📁 包括Pheromones Algorythm、Memory Algorythm和Hill Climbing Algorythm I
💻 CS
📖 第 1 页 / 共 5 页
字号:
					&& leftDecision.IsValid == true )
				{
				
					switch( leftSquare.FoodPheromoneDirection )
					{
						case "RIGHT":
						{
							leftDecision.IncrementDecision();
							leftDecision.IncrementDecision();
						}break;
						default: 
						{
							leftDecision.IncrementDecision();
						}break;
					}
				}

				/// check for pheromone following 
				/// for ants that have found food
				/// basically if you are on way home don't go backwards
				
				if( currentAnt.IsReturningHome == true 
					&& topSquare != null
					&& topSquare.SquareType != "NEST"
					&& currentAnt.HasFoundFood == true
					&& topDecision.IsValid == true )
				{
					switch( topSquare.FoodPheromoneDirection )
					{
						case "BOTTOM":
						{
							if( directionLastTravelled == DirectionLastTravelledSet.BOTTOM )
							{
								topDecision.ZeroNumber();
							}
							else
							{
								topDecision.DecrementDecision();
							}
						}break;
						case "NONE":
						{
							topDecision.IncrementDecision();
							bottomDecision.DecrementDecision();
						}break;
						default: topDecision.IncrementDecision(); break;

					}
				}

				if( currentAnt.IsReturningHome == true 
					&& rightSquare != null 
					&& rightSquare.SquareType != "NEST"
					&& currentAnt.HasFoundFood == true 
					&& rightDecision.IsValid == true )
				{
					switch( rightSquare.FoodPheromoneDirection )
					{
						case "LEFT":
						{
							if( directionLastTravelled == DirectionLastTravelledSet.LEFT)
							{
								rightDecision.ZeroNumber();
							}
							else
							{
								rightDecision.DecrementDecision();
							}
						}break; 
						case "NONE":
						{
							rightDecision.IncrementDecision();
							leftDecision.DecrementDecision();
						}break;
						default: rightDecision.IncrementDecision(); break;
					}
				}

				if( currentAnt.IsReturningHome == true 
					&& bottomSquare != null 
					&& bottomSquare.SquareType != "NEST"
					&& currentAnt.HasFoundFood == true 
					&& bottomDecision.IsValid == true )
				{
					switch( bottomSquare.FoodPheromoneDirection )
					{
						case "TOP":
						{
							if( directionLastTravelled == DirectionLastTravelledSet.TOP )
							{
								bottomDecision.ZeroNumber();
							}
							else
							{
								bottomDecision.DecrementDecision();
							}
						}break; 
						case "NONE":
						{
							bottomDecision.IncrementDecision();
							topDecision.DecrementDecision();
						}break;
						default: bottomDecision.IncrementDecision(); break;
					}
				}

				if( currentAnt.IsReturningHome == true 
					&& leftSquare != null 
					&& leftSquare.SquareType != "NEST"
					&& currentAnt.HasFoundFood == true 
					&& leftDecision.IsValid == true )
				{
					switch( leftSquare.FoodPheromoneDirection )
					{
						case "RIGHT":
						{
							if( directionLastTravelled == DirectionLastTravelledSet.RIGHT )
							{
								leftDecision.ZeroNumber();
							}
							else
							{
								leftDecision.DecrementDecision();
							}
						}break; 
						case "NONE":
						{
							leftDecision.IncrementDecision();
							rightDecision.DecrementDecision();
						}break;
						default: leftDecision.IncrementDecision(); break;
					}
				}

						
				/// if one is the food square move to it
						
				if( currentAnt.IsReturningHome == false )
				{
					if( topSquare != null 
						&& topSquare.SquareType == "FOOD" 
						&& currentAnt.HasFoundFood == false
						&& currentAnt.FindFood == true )
					{
						topDecision.IncrementDecision();
						rightDecision.ZeroNumber();
						bottomDecision.ZeroNumber();
						leftDecision.ZeroNumber();
					}
					if( rightSquare != null 
						&& rightSquare.SquareType == "FOOD" 
						&& currentAnt.HasFoundFood == false 
						&& currentAnt.FindFood == true )
					{
						topDecision.ZeroNumber();
						rightDecision.IncrementDecision();
						bottomDecision.ZeroNumber();
						leftDecision.ZeroNumber();
					}
					if( bottomSquare != null 
						&& bottomSquare.SquareType == "FOOD" 
						&& currentAnt.HasFoundFood == false 
						&& currentAnt.FindFood == true )
					{
						topDecision.ZeroNumber();
						rightDecision.ZeroNumber();
						bottomDecision.IncrementDecision();
						leftDecision.ZeroNumber();
					}
					if( leftSquare != null 
						&& leftSquare.SquareType == "FOOD" 
						&& currentAnt.HasFoundFood == false 
						&& currentAnt.FindFood == true )
					{
						topDecision.ZeroNumber();
						rightDecision.ZeroNumber();
						bottomDecision.ZeroNumber();
						leftDecision.IncrementDecision();
					}
				}


				/// make the decision
				if( topSquare != null
					&& topDecision.IsValid == true )
				{
					decision = topDecision.Compare( rightDecision );
				}
				if( rightSquare != null
					&& rightDecision.IsValid == true )
				{
					decision = rightDecision.Compare( decision );
				}
				if( bottomSquare != null
					&& bottomDecision.IsValid == true )
				{
					decision = bottomDecision.Compare( decision );
				}
				if( leftSquare != null
					&& leftDecision.IsValid == true )
				{
					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 MemoryAlgorythm( 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 + 
				/// the do not follow strongest pheromone option +
				/// 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;

					/// should we follow the highest pheromone

					if( currentAnt.DontAlwaysFollowStrongestPheromone == true 
						&& topSquare != null
						&& topSquare.PathFoodPheromone > 0 )
						topDecision.DecrementDecision();

					if( currentAnt.IsReturningHome == false 
						&& topSquare != null 
						&& topSquare.SquareType == "NEST" )
						topDecision.DecrementDecision();
							

				}
						
						
				if( bIsRightAvailable == true )
				{
					rightDecision.IncrementDecision();
					rightDecision.IsValid = true;

					if( currentAnt.DontAlwaysFollowStrongestPheromone == true 
						&& rightSquare != null
						&& rightSquare.PathFoodPheromone > 0 )
						rightDecision.DecrementDecision();

					if( currentAnt.IsReturningHome == false 
						&& rightSquare != null 
						&& rightSquare.SquareType == "NEST" )
						rightDecision.DecrementDecision();

				}


				if( bIsBottomAvailable == true )
				{
					bottomDecision.IncrementDecision();
					bottomDecision.IsValid = true;

					if( currentAnt.DontAlwaysFollowStrongestPheromone == true 
						&& bottomSquare != null
						&& bottomSquare.PathFoodPheromone > 0 )
						bottomDecision.DecrementDecision();

					if( currentAnt.IsReturningHome == false 
						&& bottomSquare != null 
						&& bottomSquare.SquareType == "NEST" )
						bottomDecision.DecrementDecision();


				}

				if( bIsLeftAvailable == true )
				{
					leftDecision.IncrementDecision();
					leftDecision.IsValid = true;

					if( currentAnt.DontAlwaysFollowStrongestPheromone == true 
						&& leftSquare != null
						&& leftSquare.PathFoodPheromone > 0 )
						leftDecision.DecrementDecision();

					if( currentAnt.IsReturningHome == false 
						&& leftSquare != null 
						&& leftSquare.SquareType == "NEST" )
						leftDecision.DecrementDecision();


				}

				/// 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( topSquare != null 
					&& topSquare.SquareType == "NEST" )
					topDecision.IncrementDecision();
				if( rightSquare != null 
					&& rightSquare.SquareType == "NEST" )
					rightDecision.IncrementDecision();
				if( bottomSquare != null 
					&

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -