Skip to content

Commit

Permalink
Fixed bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalvar committed Jun 10, 2015
1 parent 347e604 commit d82f9ac
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 42 deletions.
9 changes: 4 additions & 5 deletions FCM/KRFuzzyCMeans.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ typedef void(^KRFuzzyCMeansEachGeneration)(NSInteger times, NSArray *clusters, N

+(instancetype)sharedFCM;
-(instancetype)init;
-(void)directCluster;
-(void)clusteringWithCompletion:(KRFuzzyCMeansClusteringCompletion)_completion eachGeneration:(KRFuzzyCMeansEachGeneration)_generation;
-(void)clusteringWithCompletion:(KRFuzzyCMeansClusteringCompletion)_completion;
-(void)clustering;
-(void)clusterWithCompletion:(KRFuzzyCMeansClusteringCompletion)_completion eachGeneration:(KRFuzzyCMeansEachGeneration)_generation;
-(void)clusterWithCompletion:(KRFuzzyCMeansClusteringCompletion)_completion;
-(void)cluster;
-(void)directClusterPatterns:(NSArray *)_directPatterns;
-(void)addCentralX:(float)_x y:(float)_y;
-(void)addPatterns:(NSArray *)_theSets;
-(void)addOnePattern:(NSArray *)_oneSets;
-(void)printResults;

#pragma --mark Blocks
Expand Down
27 changes: 12 additions & 15 deletions FCM/KRFuzzyCMeans.m
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,19 @@ -(instancetype)init
* - 不需要更新群聚形心
* - 不建議使用此方法
*/
-(void)directCluster
-(void)directClusterPatterns:(NSArray *)_directPatterns
{
//If it doesn't have sources, then directly use the original sets to be clustered results.
if( _patterns == nil )
if( _directPatterns == nil || [_directPatterns count] < 1 )
{
return;
}

if( [_centrals count] > 0 && [_patterns count] > 0 )
[self addPatterns:_directPatterns];

if( [_centrals count] > 0 )
{
//分群結果陣列的長度 < 分群中心點的長度
//分群結果陣列的組數 < 分群中心點的組數
NSInteger _resultCount = [_results count];
NSInteger _centralCount = [_centrals count];
if( _resultCount < _centralCount )
Expand All @@ -281,7 +283,7 @@ -(void)directCluster
}

_lastCenters = [_centrals copy]; //預留參數,暫無用處
for( NSArray *_xy in _patterns )
for( NSArray *_xy in _directPatterns )
{
float _maxDistance = 0.0f;
int _toIndex = 0;
Expand All @@ -307,22 +309,22 @@ -(void)directCluster
* @ FCM 進行迭代運算不斷的重新分群
*
*/
-(void)clusteringWithCompletion:(KRFuzzyCMeansClusteringCompletion)_completion eachGeneration:(KRFuzzyCMeansEachGeneration)_generation
-(void)clusterWithCompletion:(KRFuzzyCMeansClusteringCompletion)_completion eachGeneration:(KRFuzzyCMeansEachGeneration)_generation
{
_clusterCompletion = _completion;
_eachGeneration = _generation;
_currentGenerations = 0;
[self _clusterSources:_patterns compareCenters:_centrals];
}

-(void)clusteringWithCompletion:(KRFuzzyCMeansClusteringCompletion)_completion
-(void)clusterWithCompletion:(KRFuzzyCMeansClusteringCompletion)_completion
{
[self clusteringWithCompletion:_completion eachGeneration:nil];
[self clusterWithCompletion:_completion eachGeneration:nil];
}

-(void)clustering
-(void)cluster
{
[self clusteringWithCompletion:nil];
[self clusterWithCompletion:nil];
}

-(void)addCentralX:(float)_x y:(float)_y
Expand All @@ -335,11 +337,6 @@ -(void)addPatterns:(NSArray *)_theSets
[_patterns addObjectsFromArray:_theSets];
}

-(void)addOnePattern:(NSArray *)_oneSets
{
[_patterns addObject:_oneSets];
}

-(void)printResults
{
NSLog(@"=================== printResults (Start) =================\n\n\n");
Expand Down
17 changes: 7 additions & 10 deletions KRFuzzyCMeans/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ - (void)viewDidLoad {
[_krFcm addCentralX:10.0f y:10.0f]; //The center 2, cluster 2 start in here
[_krFcm addCentralX:12.0f y:14.0f]; //The center 3, cluster 3 start in here
[_krFcm addPatterns:@[@[@2, @12], @[@4, @9], @[@7, @13], @[@11, @5], @[@12, @7], @[@14, @4]]];
[_krFcm clusteringWithCompletion:^(BOOL success, NSArray *clusters, NSArray *centrals, NSInteger totalTimes)
[_krFcm clusterWithCompletion:^(BOOL success, NSArray *clusters, NSArray *centrals, NSInteger totalTimes)
{
NSLog(@"\n\n===============================================\n\n");
NSLog(@"totalTimes : %li", totalTimes);
Expand All @@ -34,17 +34,14 @@ - (void)viewDidLoad {
NSLog(@"\n\n===============================================\n\n");

//Start in verify and classify others pattern.
//When train finished, add one pattern to classify.
//[_krFcm addOnePattern:@[@2, @3]];
//If you have more patterns need to classify, use this :
[_krFcm addPatterns:@[@[@2, @3], @[@3, @3], @[@5, @9]]];

//Then, if you don't want to adjust the central groups, just wanna directly classify them, you could use :
//[_krFcm directCluster];
//[_krFcm printResults];
//If you don't want to adjust the central groups, just wanna directly classify them, you could use :
[_krFcm directClusterPatterns:@[@[@2, @3], @[@3, @3], @[@5, @9]]];
[_krFcm printResults];

//Then, if you wanna renew all groups and re-adjust the central groups, you could use :
[_krFcm clusteringWithCompletion:^(BOOL success, NSArray *clusters, NSArray *centrals, NSInteger totalTimes)
//If you have one or more patterns need to do standard classification, use this to renew all groups and re-adjust the central groups :
[_krFcm addPatterns:@[@[@2, @3], @[@3, @3], @[@5, @9]]];
[_krFcm clusterWithCompletion:^(BOOL success, NSArray *clusters, NSArray *centrals, NSInteger totalTimes)
{
[_krFcm printResults];
//... Do your next step.
Expand Down
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ KRFuzzyCMeans has implemented Fuzzy C-Means (FCM) the fuzzy (ファジー理論)

```ruby
platform :ios, '7.0'
pod "KRFuzzyCMeans", "~> 1.0"
pod "KRFuzzyCMeans", "~> 1.1"
```

## How to use
Expand All @@ -26,7 +26,7 @@ pod "KRFuzzyCMeans", "~> 1.0"
[_krFcm addCentralX:10.0f y:10.0f]; //The center 2, cluster 2 start in here
[_krFcm addCentralX:12.0f y:14.0f]; //The center 3, cluster 3 start in here
[_krFcm addPatterns:@[@[@2, @12], @[@4, @9], @[@7, @13], @[@11, @5], @[@12, @7], @[@14, @4]]];
[_krFcm clusteringWithCompletion:^(BOOL success, NSArray *clusters, NSArray *centrals, NSInteger totalTimes)
[_krFcm clusterWithCompletion:^(BOOL success, NSArray *clusters, NSArray *centrals, NSInteger totalTimes)
{
NSLog(@"\n\n===============================================\n\n");
NSLog(@"totalTimes : %li", totalTimes);
Expand All @@ -35,17 +35,14 @@ pod "KRFuzzyCMeans", "~> 1.0"
NSLog(@"\n\n===============================================\n\n");
//Start in verify and classify others pattern.
//When train finished, add one pattern to classify.
//[_krFcm addOnePattern:@[@2, @3]];
//If you have more patterns need to classify, use this :
[_krFcm addPatterns:@[@[@2, @3], @[@3, @3], @[@5, @9]]];
//Then, if you don't want to adjust the central groups, just wanna directly classify them, you could use :
//[_krFcm directCluster];
//[_krFcm printResults];
//If you don't want to adjust the central groups, just wanna directly classify them, you could use :
[_krFcm directClusterPatterns:@[@[@2, @3], @[@3, @3], @[@5, @9]]];
[_krFcm printResults];
//Then, if you wanna renew all groups and re-adjust the central groups, you could use :
[_krFcm clusteringWithCompletion:^(BOOL success, NSArray *clusters, NSArray *centrals, NSInteger totalTimes)
//If you have one or more patterns need to do standard classification, use this to renew all groups and re-adjust the central groups :
[_krFcm addPatterns:@[@[@2, @3], @[@3, @3], @[@5, @9]]];
[_krFcm clusterWithCompletion:^(BOOL success, NSArray *clusters, NSArray *centrals, NSInteger totalTimes)
{
[_krFcm printResults];
//... Do your next step.
Expand All @@ -63,7 +60,7 @@ pod "KRFuzzyCMeans", "~> 1.0"

## Version

V1.0
V1.1

## License

Expand Down

0 comments on commit d82f9ac

Please sign in to comment.