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

📄 pathfinderalgorythms.cs

📁 包括Pheromones Algorythm、Memory Algorythm和Hill Climbing Algorythm I
💻 CS
📖 第 1 页 / 共 5 页
字号:
using System;
using System.Collections;
using Square;
using Fuzzy_Logic_Library;

namespace PathFinder
{
	/// <summary>
	/// Summary description for PathFinderAlgorythms.
	/// </summary>
	public class PathFinderAlgorythms
	{

		public PathFinderAlgorythms()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public void BasicAlgorythm( 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;

		///		currentAnt.Log.Clear();

				/// get the pheromone counts
						
				if( bIsTopAvailable == true && topSquare != null
					&& topSquare.SquareType != "NEST" )
					nTopPheromone = topSquare.PathFoodPheromone;
				if( bIsRightAvailable == true && rightSquare != null
					&& rightSquare.SquareType != "NEST" )
					nRightPheromone = rightSquare.PathFoodPheromone;
				if( bIsBottomAvailable == true && bottomSquare != null
					&& bottomSquare.SquareType != "NEST" )
					nBottomPheromone = bottomSquare.PathFoodPheromone;
				if( bIsLeftAvailable == true && leftSquare != null
					&& leftSquare.SquareType != "NEST" )
					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;


					if( nTopPheromone > 0  )
						topDecision.IncrementDecision();
							
					if( nTopPheromone > nRightPheromone )
						topDecision.IncrementDecision();

					if( nTopPheromone > nBottomPheromone )
						topDecision.IncrementDecision();

					if( nTopPheromone > nLeftPheromone )
						topDecision.IncrementDecision();
								

					/// should we follow the highest pheromone

					if( currentAnt.DontAlwaysFollowStrongestPheromone == true )
						topDecision.DecrementDecision();

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

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

					if( nRightPheromone > 0 )
						rightDecision.IncrementDecision();

					if( nRightPheromone > nTopPheromone )
						rightDecision.IncrementDecision();

					if( nRightPheromone > nBottomPheromone )
						rightDecision.IncrementDecision(); 

					if( nRightPheromone > nLeftPheromone )
						rightDecision.IncrementDecision();

					if( currentAnt.DontAlwaysFollowStrongestPheromone == true )
						rightDecision.DecrementDecision();

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

				}


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

					if( nBottomPheromone > 0 )
						bottomDecision.IncrementDecision();

					if( nBottomPheromone > nTopPheromone )
						bottomDecision.IncrementDecision();

					if( nBottomPheromone > nRightPheromone )
						bottomDecision.IncrementDecision();

					if( nBottomPheromone > nLeftPheromone )
						bottomDecision.IncrementDecision();

					if( currentAnt.DontAlwaysFollowStrongestPheromone == true )
						bottomDecision.DecrementDecision();

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


				}

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

					if( nLeftPheromone > 0 )
						leftDecision.IncrementDecision();

					if( nLeftPheromone > nTopPheromone )
						leftDecision.IncrementDecision();

					if( nLeftPheromone > nRightPheromone )
						leftDecision.IncrementDecision();

					if( nLeftPheromone > nBottomPheromone )
						leftDecision.IncrementDecision();

					if( currentAnt.DontAlwaysFollowStrongestPheromone == true )
						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( topSquare == null )
					topDecision.IsValid = false;
				if( rightSquare == null )
					rightDecision.IsValid = false;
				if( bottomSquare == null )
					bottomDecision.IsValid = false;
				if( leftSquare == null )
					leftDecision.IsValid = false;

				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 );

				/// Should not move backwards ( unless we really want to )
						
				bool bIsBackward = false;

				switch( decision.Name )
				{
					case "TOP": 
					{
						if( currentAnt.LastDirectionTravelled == ( int )DirectionLastTravelledSet.BOTTOM )
							bIsBackward = true;
					}break;
					case "RIGHT":
					{
						if( currentAnt.LastDirectionTravelled == ( int )DirectionLastTravelledSet.LEFT )
							bIsBackward = true;
					}break;
					case "BOTTOM":
					{
						if( currentAnt.LastDirectionTravelled == ( int )DirectionLastTravelledSet.TOP )
							bIsBackward = true;
					}break;
					case "LEFT":
					{
						if( currentAnt.LastDirectionTravelled == ( int )DirectionLastTravelledSet.RIGHT )
							bIsBackward = true;
					}break;													
				}

				/// get the next location by making the current suggestion invalid

				if( bIsBackward == true && currentAnt.IsReturningHome == false )
				{
					switch( decision.Name )
					{
						case "TOP": topDecision.IsValid = false; break;
						case "RIGHT": rightDecision.IsValid = false; break;
						case "BOTTOM": bottomDecision.IsValid = false; break;
						case "LEFT": leftDecision.IsValid = false; break;
					}
								
					if( topSquare != null 
						&& topDecision.IsValid == true ) 
						decision = topDecision;
					else if( rightSquare != null 
						&& rightDecision.IsValid == true ) 
						decision = rightDecision;
					else if( bottomSquare != null 
						&& bottomDecision.IsValid == true )
						decision = bottomDecision;
					else if( leftSquare != null
						&& leftDecision.IsValid == true )
						decision = leftDecision;
					else
					{
						/// allow backwards move
					}
				}


				/// if returning home backwards is a valid location
						
				if( currentAnt.IsReturningHome == true )
				{

					/// if one is the home square move to it
					if( topSquare != null 
						&& topSquare.SquareType == "NEST" )
					{
						decision = topDecision;
					}
					if( rightSquare != null 
						&& rightSquare.SquareType == "NEST" )
					{
						decision = rightDecision;
					}
					if( bottomSquare != null 
						&& bottomSquare.SquareType == "NEST" )
					{
						decision = bottomDecision;
					}
					if( leftSquare != null 
						&& leftSquare.SquareType == "NEST" )
					{
						decision = leftDecision;
					}
				}

				/// try to avoid any squares that are already occupied
						

				if( topSquare != null
					&& topSquare.IsCritterOnSquare == true )
					topDecision.IsValid = false;
				if( rightSquare != null 
					&& rightSquare.IsCritterOnSquare == true )
					rightDecision.IsValid = false;
				if( bottomSquare != null
					&& bottomSquare.IsCritterOnSquare == true )
					bottomDecision.IsValid = false;
				if( leftSquare != null 
					&& leftSquare.IsCritterOnSquare == true )
					leftDecision.IsValid = false;
						
				string strDirection = decision.Name;
				string strPreviousDirection;

				switch( currentAnt.GetDirectionTravelledAt( 2 ) )
				{
					case ( int )DirectionLastTravelledSet.NONE: strPreviousDirection = "NONE"; break;
					case ( int )DirectionLastTravelledSet.TOP: strPreviousDirection = "TOP"; break;
					case ( int )DirectionLastTravelledSet.RIGHT: strPreviousDirection = "RIGHT"; break;
					case ( int )DirectionLastTravelledSet.BOTTOM: strPreviousDirection = "BOTTOM"; break;
					case ( int )DirectionLastTravelledSet.LEFT: strPreviousDirection = "LEFT"; break;
					default: strPreviousDirection = "NONE"; break;
				}

				if( strDirection == strPreviousDirection )
				{
					switch( strDirection )
					{
						case "TOP": 
						{
							if( topSquare != null 
								&& topDecision.IsValid == true )
								///	&& topDecision.Name != strDirection )
								decision = topDecision;
						}break;
						case "RIGHT":
						{
							if( rightSquare != null 
								&& rightDecision.IsValid == true ) 
								///		&& rightDecision.Name != strDirection )
								decision = rightDecision;
						}break;
						case "BOTTOM":
						{
							if( bottomSquare != null 

⌨️ 快捷键说明

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