📄 fuzzydecision.cs
字号:
using System;
using System.Xml;
using System.Collections;
namespace Fuzzy_Logic_Library
{
/// <summary>
/// This is an experimental class for use in the Pathfinder project
/// The idea is that the class is developed as a helper to make decisions
/// Though I'm not sure at the moment how much use it will actually be.
/// The hope is that as the decisions for the Pathfinder program get more
/// complex then this class will evolve along with the program
/// </summary>
public class FuzzyDecision : FuzzyBasic
{
/// <summary>
/// a FuzzyDecision automatically starts as invalid until an
/// assignment or decrement has been made on it
/// </summary>
private bool bIsValidNumber;
public bool IsValid
{
get
{
return bIsValidNumber;
}
set
{
bIsValidNumber = value;
}
}
/// <summary>
/// basic constructor
/// </summary>
public FuzzyDecision() : base()
{
//
// TODO: Add constructor logic here
//
bIsValidNumber = false;
}
/// <summary>
/// copy constructor
/// </summary>
/// <param name="decision"></param>
private FuzzyDecision( FuzzyDecision decision )
{
this.Number = decision.Number;
this.Name = decision.Name;
this.bIsValidNumber = decision.IsValid;
}
/// <summary>
/// Constructor taking just the decision name
/// </summary>
/// <param name="name"></param>
public FuzzyDecision( string name )
{
bIsValidNumber = false;
this.Name = name;
}
/// <summary>
/// increment this decision
/// </summary>
public void IncrementDecision()
{
this.Number++;
}
/// <summary>
/// decrement this decision
/// </summary>
public void DecrementDecision()
{
this.Number--;
}
/// <summary>
/// add the passed in value to this decision
/// </summary>
/// <param name="number"></param>
public void AddToDecision( int number )
{
this.Number += number;
}
/// <summary>
/// subtract the passed in value from this decision
/// </summary>
/// <param name="number"></param>
public void SubtractFromDecision( int number )
{
this.Number -= number;
}
/// <summary>
/// set this decision value to zero
/// </summary>
public void ZeroNumber()
{
this.Number = 0;
}
/// <summary>
/// compare this decision with another returning the winner
/// </summary>
/// <param name="decision"></param>
/// <returns></returns>
public FuzzyDecision Compare( FuzzyDecision decision )
{
if( this.IsValid == false )
return new FuzzyDecision( decision );
if( decision == null || decision.IsValid == false )
return new FuzzyDecision( this );
if( this.Number > decision.Number )
return new FuzzyDecision( this );
else if( this.Number < decision.Number )
return new FuzzyDecision( decision );
else if( this.Number == decision.Number )
{
Random rand = new Random();
switch( rand.Next( 3 ) )
{
case 1: return new FuzzyDecision( this );
case 2: return new FuzzyDecision( decision );
default : return new FuzzyDecision( decision );
}
}
else
return new FuzzyDecision( this );
}
/// <summary>
/// Save this decision
/// </summary>
/// <param name="xmlWriter"></param>
public override void Save( XmlWriter xmlWriter )
{
xmlWriter.WriteStartElement( "FuzzyDecision" );
xmlWriter.WriteElementString( "IsValid", IsValid.ToString() );
base.Save( xmlWriter );
xmlWriter.WriteEndElement();
}
/// <summary>
/// load the values for this decision from an xml file
/// </summary>
/// <param name="xmlReader"></param>
public override void Load( XmlReader xmlReader )
{
while( xmlReader.Name != "IsValid" )
{
xmlReader.Read();
if( xmlReader.EOF == true )
return;
}
xmlReader.Read();
if( xmlReader.Value == "True" )
IsValid = true;
else
IsValid = false;
base.Load( xmlReader );
}
/// Comparison stuff
public static bool operator ==( FuzzyDecision fuzzyDecisionOne, FuzzyDecision fuzzyDecisionTwo )
{
bool bOneIsNull = false;
bool bBothAreNull = false;
try
{
double nTest = fuzzyDecisionOne.ID;
}
catch( NullReferenceException nullRefExp )
{
string strTemp = nullRefExp.Message;
bOneIsNull = true;
}
try
{
double nTest = fuzzyDecisionTwo.ID;
}
catch( NullReferenceException nullRefExp )
{
string strTemp = nullRefExp.Message;
if( bOneIsNull == true )
bBothAreNull = true;
else
bOneIsNull = true;
}
if( bOneIsNull == true && bBothAreNull == false )
return false;
if( bBothAreNull == true )
return true;
if( fuzzyDecisionOne.IsValid == fuzzyDecisionTwo.IsValid
&& fuzzyDecisionOne.ID == fuzzyDecisionTwo.ID
&& fuzzyDecisionOne.Maximum == fuzzyDecisionTwo.Maximum
&& fuzzyDecisionOne.Membership == fuzzyDecisionTwo.Membership
&& fuzzyDecisionOne.Minimum == fuzzyDecisionTwo.Minimum
&& fuzzyDecisionOne.Name == fuzzyDecisionTwo.Name
&& fuzzyDecisionOne.Number == fuzzyDecisionTwo.Number )
return true;
else
return false;
}
public static bool operator !=( FuzzyDecision fuzzyDecisionOne, FuzzyDecision fuzzyDecisionTwo )
{
bool bOneIsNull = false;
bool bBothAreNull = false;
try
{
double nTest = fuzzyDecisionOne.ID;
}
catch( NullReferenceException nullRefExp )
{
string strString = nullRefExp.Message;
bOneIsNull = true;
}
try
{
double nTest = fuzzyDecisionTwo.ID;
}
catch( NullReferenceException nullRefExp )
{
string strTemp = nullRefExp.Message;
if( bOneIsNull == true )
bBothAreNull = true;
else
bOneIsNull = true;
}
if( bOneIsNull == true && bBothAreNull == false )
return true;
if( bBothAreNull == true )
return false;
if( fuzzyDecisionOne.IsValid != fuzzyDecisionTwo.IsValid
|| fuzzyDecisionOne.ID != fuzzyDecisionTwo.ID
|| fuzzyDecisionOne.Maximum != fuzzyDecisionTwo.Maximum
|| fuzzyDecisionOne.Membership != fuzzyDecisionTwo.Membership
|| fuzzyDecisionOne.Minimum != fuzzyDecisionTwo.Minimum
|| fuzzyDecisionOne.Name != fuzzyDecisionTwo.Name
|| fuzzyDecisionOne.Number != fuzzyDecisionTwo.Number )
return true;
else
return false;
}
/// required overrides due to the comparison operators
///
public override bool Equals(object obj)
{
if( obj == null || GetType() != obj.GetType() )
return false;
FuzzyDecision temp = ( FuzzyDecision )obj;
return this == temp;
}
public override int GetHashCode()
{
return ID.GetHashCode() ^ Maximum.GetHashCode() ^ Membership.GetHashCode() ^ Minimum.GetHashCode() ^ Name.GetHashCode() ^ Number.GetHashCode();
}
}
/// <summary>
/// use a decision set to store the decisions
/// </summary>
public class FuzzyDecisionSet : FuzzySet
{
/// <summary>
/// standard constructor
/// </summary>
public FuzzyDecisionSet()
{
}
/// <summary>
///
/// </summary>
/// <param name="strName"></param>
public FuzzyDecisionSet( string strName ) : base()
{
Name = strName;
}
public string GetName( int decision )
{
if( decision < this.FuzzyArray.Count )
{
return ( ( FuzzyDecision )this.FuzzyArray[ decision ] ).Name;
}
return null;
}
/// <summary>
/// return the strongest Decision
/// </summary>
/// <returns></returns>
public FuzzyDecision WinningDecision()
{
int nWinningNumber = 0;
for( int i=0; i<this.FuzzyArray.Count; i++ )
{
if( i+1 < this.FuzzyArray.Count )
{
if( ( ( FuzzyDecision )this.FuzzyArray[ i ] ).Number > ( ( FuzzyDecision )this.FuzzyArray[ i+1 ] ).Number )
{
nWinningNumber = i;
}
}
}
return ( FuzzyDecision )this.FuzzyArray[ nWinningNumber ];
}
/// <summary>
/// return the weakest decision
/// </summary>
/// <returns></returns>
public FuzzyDecision LosingDecision()
{
int nLosingNumber = 0;
for( int i=0; i<this.FuzzyArray.Count; i++ )
{
if( i+1 < this.FuzzyArray.Count )
{
if( ( ( FuzzyDecision )this.FuzzyArray[ i ] ).Number < ( ( FuzzyDecision )this.FuzzyArray[ i+1 ] ).Number )
{
nLosingNumber = i;
}
}
}
return ( FuzzyDecision )this.FuzzyArray[ nLosingNumber ];
}
/// <summary>
/// Add an unnamed Fuzzy Decision to the set
/// </summary>
public void AddDecision()
{
this.FuzzyArray.Add( new FuzzyDecision() );
}
/// <summary>
/// Add a named Fuzzy Decision to the set
/// </summary>
/// <param name="name">name of the new decision</param>
public void AddDecision( string name )
{
this.FuzzyArray.Add( new FuzzyDecision( name ) );
}
/// <summary>
/// Add a named Fuzzy Decision to the set and indicate if it is valid
/// </summary>
/// <param name="name">name of the new decision</param>
/// <param name="isValid">is the decision valid?</param>
public void AddDecision( string name, bool isValid )
{
FuzzyDecision temp = new FuzzyDecision( name );
temp.IsValid = isValid;
this.FuzzyArray.Add( temp );
}
/// <summary>
/// Add a new Fuzzy Decision
/// </summary>
/// <param name="decision">Fuzzy Decision to be added</param>
public void AddDecision( FuzzyDecision decision )
{
FuzzyArray.Add( decision );
}
/// <summary>
/// Increment the Fuzzy Decision at the given index
/// </summary>
/// <param name="index">index of the decision in the array</param>
/// <returns>true if done ( probably never checked )</returns>
public bool IncrementAt( int index )
{
if( index < this.FuzzyArray.Count )
{
( ( FuzzyDecision )this.FuzzyArray[ index ] ).Number++;
return true;
}
else
return false;
}
/// <summary>
/// Increment the Fuzzy Decision with the given name
/// </summary>
/// <param name="name">name of the decision to be incremented</param>
/// <returns>true if done</returns>
public bool IncrementByName( string name )
{
bool bDone = false;
for( int i=0; i<this.FuzzyArray.Count; i++ )
{
if( name == ( ( FuzzyDecision )this.FuzzyArray[ i ] ).Name )
{
( ( FuzzyDecision )this.FuzzyArray[ i ] ).Number++;
bDone = true;
i=this.FuzzyArray.Count;
}
}
return bDone;
}
/// <summary>
/// decrement the Fuzzy Decision at the given index
/// </summary>
/// <param name="index">the index for the decision</param>
/// <returns>true if done</returns>
public bool DecrementAt( int index )
{
if( index < this.FuzzyArray.Count )
{
( ( FuzzyDecision )this.FuzzyArray[ index ] ).Number--;
return true;
}
else
return false;
}
/// <summary>
/// decrement the Fuzzy decision with the given name
/// </summary>
/// <param name="name">name of the decision</param>
/// <returns>true if done</returns>
public bool DecrementByName( string name )
{
bool bDone = false;
for( int i=0; i<this.FuzzyArray.Count; i++ )
{
if( name == ( ( FuzzyDecision )this.FuzzyArray[ i ] ).Name )
{
( ( FuzzyDecision )this.FuzzyArray[ i ] ).Number--;
bDone = true;
i=this.FuzzyArray.Count;
}
}
return bDone;
}
/// <summary>
/// Add the number to the Fuzzy Decision at the index
/// </summary>
/// <param name="index">index of the decision</param>
/// <param name="number">number to be added</param>
/// <returns>true if done</returns>
public bool AddAt( int index, int number )
{
if( index < this.FuzzyArray.Count )
{
( ( FuzzyDecision )this.FuzzyArray[ index ] ).Number += number;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -