基本的に、あなたが説明しているのはFuzzy Logicです。
あなたがやりたいことを処理するファジー論理規則クラスを書きました。
特徴:
- 私が提供した方法を使用して、独自のカスタム ルールを簡単に追加できます。
- 単一の値をチェックして
method
、文字列の結果を取得できます (またはnil
、ルールに一致しない場合)。
- ルールを使用するため、任意の間隔期間を定義できます。
新しいルールを追加します:
[logic addRule:@"2.0" forLowerCondition:14.5 forUpperCondition:17.0];
出力例 (以下のコードから):
Result for 15.20 is: 2.0
これがコードの実装です.....
あなたのメインで:
FuzzyLogic *logic = [[FuzzyLogic alloc] initWithRule:@"1.0" forLowerCondition:10.0 forUpperCondition:14.5];
[logic addRule:@"2.0" forLowerCondition:14.5 forUpperCondition:17.0];
[logic addRule:@"2.5" forLowerCondition:17.0 forUpperCondition:23.0];
double input1 = 15.2f;
NSLog(@"Result for %.2lf is: %@", input1, [logic fuzzyResultForValue:input1]);
[logic release];
FuzzyLogic.h:
#import <Foundation/Foundation.h>
@interface FuzzyLogic : NSObject {
NSMutableArray *conditions;
}
- (id) initWithRule:(NSString*)result forLowerCondition:(double)lower forUpperCondition:(double)upper;
- (void) addRule:(NSString*)result forLowerCondition:(double)lower forUpperCondition:(double)upper;
- (NSString*) fuzzyResultForValue:(double)input;
@end
FuzzyLogic.m:
#import "FuzzyLogic.h"
@implementation FuzzyLogic
enum {
lowerIndex = 0,
upperIndex,
resultIndex
};
- (id) initWithRule:(NSString*)result forLowerCondition:(double)lower forUpperCondition:(double)upper {
self = [super init];
if (self != nil) {
[self addRule:result forLowerCondition:lower forUpperCondition:upper];
}
return self;
}
- (void) addRule:(NSString*)result forLowerCondition:(double)lower forUpperCondition:(double)upper {
NSArray *rule = [[NSArray alloc] initWithObjects:[NSString stringWithFormat:@"%lf",lower],[NSString stringWithFormat:@"%lf",upper],result,nil];
if (conditions == nil) {
conditions = [[NSMutableArray alloc] initWithObjects:rule,nil];
} else {
[conditions addObject:rule];
}
}
- (NSString*) fuzzyResultForValue:(double)input
{
NSString *returnable = nil;
// Find the result
for (NSArray *cond in conditions) {
double lower = [[cond objectAtIndex:lowerIndex] doubleValue];
double upper = [[cond objectAtIndex:upperIndex] doubleValue];
if ( (input >= lower && input < upper) ) {
returnable = [cond objectAtIndex:resultIndex];
break;
}
}
if (returnable == nil)
{ NSLog(@"Error: Input met no conditions!");}
return returnable;
}
@end