Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / C#

FuzzScript - A Fuzzy Logic Control Language

Rate me:
Please Sign up or sign in to vote.
4.97/5 (32 votes)
16 Dec 2009CPOL9 min read 63.3K   2.6K   73  
This article presents a Fuzzy Logic scripting language, FuzzScript, which can be used to include fuzzy controllers in C# applications. One interesting aspect is the possibility to generate an optimized version (hard-coded) of the controller under examination at run time.
KnowledgeBase
{
	InputVariable SENSORLEFT [0, 2000]
	{
		LShoulder (vnear, 20, 40)
		Triangular (near, 10, 55, 100)
		Triangular (medium, 60, 100, 140)
		Triangular (far, 120, 150, 180)
		RShoulder (vfar, 160, 170)
	}

	InputVariable SENSORMIDDLE [0, 2000]
	{
		LShoulder (vnear, 20, 40)
		Triangular (near, 10, 55, 100)
		Triangular (medium, 60, 100, 140)
		Triangular (far, 120, 150, 180)
		RShoulder (vfar, 160, 170)
	}

	InputVariable SENSORRIGHT [0, 2000]
	{
		LShoulder (vnear, 20, 40)
		Triangular (near, 10, 55, 100)
		Triangular (medium, 60, 100, 140)
		Triangular (far, 120, 150, 180)
		RShoulder (vfar, 160, 170)
	}

	OutputVariable VELOCITYANGULAR [-0.4, 0.4]
	{
		LShoulder ( Bignegative, -0.3, -0.2)
		Triangular ( negative, -0.3, -0.2, -0.1)
		Triangular ( Smallnegative, -0.2, -0.1, 0)
		Triangular ( NoChange, -0.1, 0, 0.1)
		Triangular ( Smallpositive, 0, 0.1, 0.2)
		Triangular ( positive, 0.1, 0.2, 0.3)
		RShoulder ( Bigpositive, 0.2, 0.3)
	}


	OutputVariable VELOCITYLINEAR [-0.1, 5]
	{
		LShoulder (Reverse, -1, 0)
		Triangular (Stop, -0.1, 0, 0.1)
		Triangular (Slow, 0, 0.3, 0.6)
		Triangular (Medium, 0.3, 0.6, 0.9)
		RShoulder (Fast, 0.6, 5)
	}
}

Predicates
{
}


RuleBase 
{
	If SENSORLEFT is vfar  and SENSORRIGHT is vfar Then VELOCITYANGULAR is nochange and VELOCITYLINEAR is fast
	If SENSORLEFT is vfar  and SENSORRIGHT is far Then VELOCITYANGULAR is nochange and VELOCITYLINEAR is fast
	If SENSORLEFT is vfar  and SENSORRIGHT is medium Then VELOCITYANGULAR is nochange and VELOCITYLINEAR is fast
	If SENSORLEFT is vfar  and SENSORRIGHT is near Then VELOCITYANGULAR is smallnegative and VELOCITYLINEAR is medium
	If SENSORLEFT is vfar  and SENSORRIGHT is vnear Then VELOCITYANGULAR is bignegative and VELOCITYLINEAR is stop 

	If SENSORLEFT is far  and SENSORRIGHT is vfar Then VELOCITYANGULAR is nochange and VELOCITYLINEAR is fast
	If SENSORLEFT is far  and SENSORRIGHT is far Then VELOCITYANGULAR is nochange and VELOCITYLINEAR is fast
	If SENSORLEFT is far  and sensorright is medium Then VELOCITYANGULAR is nochange and VELOCITYLINEAR is fast
	If SENSORLEFT is far  and sensorright is near Then VELOCITYANGULAR is smallnegative and VELOCITYLINEAR is medium
	If SENSORLEFT is far  and sensorright is vnear Then VELOCITYANGULAR is negative and VELOCITYLINEAR is stop 

	If SENSORLEFT is medium  and sensorright is vfar Then VELOCITYANGULAR is nochange and VELOCITYLINEAR is  fast
	If SENSORLEFT is medium  and sensorright is far Then VELOCITYANGULAR is nochange and VELOCITYLINEAR is  fast
	If SENSORLEFT is medium  and sensorright is medium Then VELOCITYANGULAR is nochange and VELOCITYLINEAR is fast
	If SENSORLEFT is medium  and sensorright is near Then VELOCITYANGULAR is smallnegative and VELOCITYLINEAR is medium
	If SENSORLEFT is medium  and sensorright is vnear Then VELOCITYANGULAR is smallnegative and VELOCITYLINEAR is stop 
	
	If SENSORLEFT is near  and sensorright is vfar Then VELOCITYANGULAR is smallpositive and VELOCITYLINEAR is medium
	If SENSORLEFT is near  and sensorright is far Then VELOCITYANGULAR is smallpositive and VELOCITYLINEAR is medium
	If SENSORLEFT is near  and sensorright is medium Then VELOCITYANGULAR is smallpositive and VELOCITYLINEAR is medium
	If SENSORLEFT is near  and sensorright is near Then VELOCITYANGULAR is nochange and VELOCITYLINEAR is  medium
	If SENSORLEFT is near  and sensorright is vnear Then VELOCITYANGULAR is smallnegative	and VELOCITYLINEAR is stop

	If SENSORLEFT is vnear  and sensorright is vfar Then VELOCITYANGULAR is bigpositive and VELOCITYLINEAR is stop
	If SENSORLEFT is vnear  and sensorright is far Then VELOCITYANGULAR is positive and VELOCITYLINEAR is stop
	If SENSORLEFT is vnear  and sensorright is medium Then VELOCITYANGULAR is positive and VELOCITYLINEAR is stop
	If SENSORLEFT is vnear  and sensorright is near Then VELOCITYANGULAR is smallpositive and VELOCITYLINEAR is stop
	If SENSORLEFT is vnear  and sensorright is vnear Then VELOCITYANGULAR is nochange and VELOCITYLINEAR is reverse


	If  SENSORMIDDLE is near Then VELOCITYLINEAR is stop
	If  SENSORMIDDLE is vnear Then VELOCITYLINEAR is stop
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Crimsonwing (Malta) Ltd
Malta Malta
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions