Skip to content

Commit

Permalink
Added GM0N model.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalvar committed Oct 21, 2015
1 parent 3dc7756 commit 67c39a0
Show file tree
Hide file tree
Showing 17 changed files with 362 additions and 62 deletions.
19 changes: 18 additions & 1 deletion GreyTheory.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
8E6349BD1BD7DC8E002F601C /* KRGreyGM0N.m in Sources */ = {isa = PBXBuildFile; fileRef = 8E6349BC1BD7DC8E002F601C /* KRGreyGM0N.m */; settings = {ASSET_TAGS = (); }; };
8E6349C01BD7DCB3002F601C /* KRGreyLib.m in Sources */ = {isa = PBXBuildFile; fileRef = 8E6349BF1BD7DCB3002F601C /* KRGreyLib.m */; settings = {ASSET_TAGS = (); }; };
8EE869021BACFF0A00802684 /* KRMathLib.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EE868FF1BACFF0A00802684 /* KRMathLib.m */; };
8EE869031BACFF0A00802684 /* NSMutableArray+KRMatrix.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EE869011BACFF0A00802684 /* NSMutableArray+KRMatrix.m */; };
8EE8690C1BACFF7000802684 /* KRGreyGM1N.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EE8690B1BACFF7000802684 /* KRGreyGM1N.m */; };
Expand All @@ -32,6 +34,10 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
8E6349BB1BD7DC8E002F601C /* KRGreyGM0N.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KRGreyGM0N.h; sourceTree = "<group>"; };
8E6349BC1BD7DC8E002F601C /* KRGreyGM0N.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KRGreyGM0N.m; sourceTree = "<group>"; };
8E6349BE1BD7DCA4002F601C /* KRGreyLib.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KRGreyLib.h; sourceTree = "<group>"; };
8E6349BF1BD7DCB3002F601C /* KRGreyLib.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KRGreyLib.m; sourceTree = "<group>"; };
8EE868FE1BACFF0A00802684 /* KRMathLib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KRMathLib.h; sourceTree = "<group>"; };
8EE868FF1BACFF0A00802684 /* KRMathLib.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KRMathLib.m; sourceTree = "<group>"; };
8EE869001BACFF0A00802684 /* NSMutableArray+KRMatrix.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSMutableArray+KRMatrix.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -90,6 +96,10 @@
8EE869091BACFF7000802684 /* KRGreyModels */ = {
isa = PBXGroup;
children = (
8E6349BE1BD7DCA4002F601C /* KRGreyLib.h */,
8E6349BF1BD7DCB3002F601C /* KRGreyLib.m */,
8E6349BB1BD7DC8E002F601C /* KRGreyGM0N.h */,
8E6349BC1BD7DC8E002F601C /* KRGreyGM0N.m */,
8EE8690A1BACFF7000802684 /* KRGreyGM1N.h */,
8EE8690B1BACFF7000802684 /* KRGreyGM1N.m */,
);
Expand Down Expand Up @@ -221,7 +231,7 @@
8EF4E7001B987A770096433A /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0640;
LastUpgradeCheck = 0700;
ORGANIZATIONNAME = "Kalvar Lin";
TargetAttributes = {
8EF4E7071B987A770096433A = {
Expand Down Expand Up @@ -279,9 +289,11 @@
files = (
8EE8690C1BACFF7000802684 /* KRGreyGM1N.m in Sources */,
8EF4E7331B987E180096433A /* KRGreyTheory.m in Sources */,
8E6349C01BD7DCB3002F601C /* KRGreyLib.m in Sources */,
8EF4E7141B987A770096433A /* ViewController.m in Sources */,
8EE869021BACFF0A00802684 /* KRMathLib.m in Sources */,
8EF4E7111B987A770096433A /* AppDelegate.m in Sources */,
8E6349BD1BD7DC8E002F601C /* KRGreyGM0N.m in Sources */,
8EF4E70E1B987A770096433A /* main.m in Sources */,
8EE869031BACFF0A00802684 /* NSMutableArray+KRMatrix.m in Sources */,
);
Expand Down Expand Up @@ -346,6 +358,7 @@
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand Down Expand Up @@ -412,6 +425,7 @@
INFOPLIST_FILE = GreyTheory/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.kalvar.grey.theory.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
Expand All @@ -423,6 +437,7 @@
INFOPLIST_FILE = GreyTheory/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.kalvar.grey.theory.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand All @@ -441,6 +456,7 @@
);
INFOPLIST_FILE = GreyTheoryTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.kalvar.grey.theory.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/GreyTheory.app/GreyTheory";
};
Expand All @@ -456,6 +472,7 @@
);
INFOPLIST_FILE = GreyTheoryTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.kalvar.grey.theory.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/GreyTheory.app/GreyTheory";
};
Expand Down
2 changes: 1 addition & 1 deletion GreyTheory/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.kalvar.grey.theory.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
28 changes: 24 additions & 4 deletions GreyTheory/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ @interface ViewController ()

@implementation ViewController

- (void)viewDidLoad
-(void)gm1n
{
[super viewDidLoad];

// Using GM1N model
KRGreyGM1N *gm1n = [[KRGreyTheory sharedTheory] useGM1N];
[gm1n addPatterns:@[@2.0f, @11.0f, @1.5f, @2.0f, @2.2f, @3.0f] patternKey:@"x1"];
[gm1n addPatterns:@[@3.0f, @13.5f, @1.0f, @3.0f, @3.0f, @4.0f] patternKey:@"x2"];
Expand All @@ -28,6 +25,29 @@ - (void)viewDidLoad
[gm1n addPatterns:@[@1.0f, @10.0f, @5.0f, @2.0f, @1.0f, @1.0f] patternKey:@"x5"];
[gm1n analyze];
[gm1n print];
}

-(void)gm0n
{
KRGreyGM0N *gm0n = [[KRGreyTheory sharedTheory] useGM0N];
[gm0n addPatterns:@[@1.0f, @1.0f, @1.0f, @1.0f, @1.0f, @1.0f] patternKey:@"x1"];
[gm0n addPatterns:@[@0.75f, @1.22f, @0.2f, @1.0f, @1.0f, @1.0f] patternKey:@"x2"];
[gm0n addPatterns:@[@0.5f, @1.0f, @0.7f, @0.66f, @1.0f, @0.5f] patternKey:@"x3"];
[gm0n addPatterns:@[@1.0f, @1.09f, @0.4f, @0.33f, @0.66f, @0.25f] patternKey:@"x4"];
[gm0n addPatterns:@[@0.25f, @0.99f, @1.0f, @0.66f, @0.33f, @0.25f] patternKey:@"x5"];
[gm0n analyze];
[gm0n print];
}

- (void)viewDidLoad
{
[super viewDidLoad];

// Using GM1N model
[self gm1n];

// Using GM0N model
[self gm0n];

}

Expand Down
2 changes: 1 addition & 1 deletion GreyTheoryTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.kalvar.grey.theory.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
2 changes: 1 addition & 1 deletion KRGreyTheory.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "KRGreyTheory"
s.version = "1.0.0"
s.version = "1.1.0"
s.summary = "Grey Theory on Machine Learning and Data Analysis."
s.description = <<-DESC
Machine Learning (マシンラーニング) in this project, it implemented the Grey Theory. This theory could use in big data analysis (データ分析), user behavior analysis (ユーザーの行動分析) and data mining (データマイニング) as well, especially find out what sub-factories impact on the real results via big data.
Expand Down
12 changes: 6 additions & 6 deletions ML/KRGrey+Definition/KRGrey+Definition.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// Copyright (c) 2015年 Kalvar Lin. All rights reserved.
//

static NSString *_kKRGreyGm1nEquationFactoryPrefixName = @"b";
static NSString *_kKRGreyGmEquationFactoryPrefixName = @"b";

static NSString *kKRGreyGm1nEquatioinResultName = @"a";
static NSString *kKRGreyGm1nEquationName = @"equation";
static NSString *kKRGreyGm1nEquationAbsValue = @"absValue";
static NSString *kKRGreyGm1nEquationPatternIndex = @"patternIndex";
static NSString *kKRGreyGm1nEquationRanking = @"ranking";
static NSString *kKRGreyGmEquatioinResultName = @"a";
static NSString *kKRGreyGmEquationName = @"equation";
static NSString *kKRGreyGmEquationAbsValue = @"absValue";
static NSString *kKRGreyGmEquationPatternIndex = @"patternIndex";
static NSString *kKRGreyGmEquationRanking = @"ranking";

30 changes: 30 additions & 0 deletions ML/KRGreyModels/KRGreyGM0N.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// KRGreyGM0N.h
// GreyTheory
//
// Created by Kalvar Lin on 2015/9/3.
// Copyright (c) 2015年 Kalvar Lin. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "KRGreyLib.h"

@interface KRGreyGM0N : NSObject

@property (nonatomic, strong) NSMutableArray *patterns;
@property (nonatomic, strong) NSMutableArray *keys;
@property (nonatomic, strong) NSMutableArray *analyzedResults;
@property (nonatomic, strong) NSMutableArray *influenceDegrees;
@property (nonatomic, strong) NSMutableDictionary *mappingResults; // This will map analyzedResults and pattern-keys

+(instancetype)sharedTheory;
-(instancetype)init;

-(void)addOutputs:(NSArray *)_someOutputs patternKey:(NSString *)_patternKey;
-(void)addPatterns:(NSArray *)_somePatterns patternKey:(NSString *)_patternKey;
-(void)analyze;
-(void)clean;
-(void)print;

@end

148 changes: 148 additions & 0 deletions ML/KRGreyModels/KRGreyGM0N.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//
// KRGreyGM0N.m
// GreyTheory
//
// Created by Kalvar Lin on 2015/9/3.
// Copyright (c) 2015年 Kalvar Lin. All rights reserved.
//

#import "KRGreyGM0N.h"
#import "NSMutableArray+KRMatrix.h"

@implementation KRGreyGM0N (fixFactories)

-(NSString *)_makeEquationNameByNumber:(NSInteger)_number
{
return [NSString stringWithFormat:@"%@%li", _kKRGreyGmEquationFactoryPrefixName, _number];
}

@end

@implementation KRGreyGM0N

+(instancetype)sharedTheory
{
static dispatch_once_t pred;
static KRGreyGM0N *_object = nil;
dispatch_once(&pred, ^{
_object = [[KRGreyGM0N alloc] init];
});
return _object;
}

-(instancetype)init
{
self = [super init];
if( self )
{
_patterns = [NSMutableArray new];
_keys = [NSMutableArray new];
_analyzedResults = [[NSMutableArray alloc] initWithCapacity:0];
_influenceDegrees = [NSMutableArray new];
_mappingResults = [NSMutableDictionary new];
}
return self;
}

#pragma --mark Public Methods
// The outputs are the results of all patterns
-(void)addOutputs:(NSArray *)_someOutputs patternKey:(NSString *)_patternKey
{
[_patterns insertObject:[_someOutputs copy] atIndex:0];
[_keys addObject:[_patternKey copy]];
}

-(void)addPatterns:(NSArray *)_somePatterns patternKey:(NSString *)_patternKey
{
[_patterns addObject:[_somePatterns copy]];
[_keys addObject:[_patternKey copy]];
}

-(void)analyze
{
NSArray *_ago = [[KRGreyLib sharedLib] ago:_patterns];
NSArray *_agoBoxes = [[_ago firstObject] copy];
NSArray *_zBoxes = [[_ago lastObject] copy];
_ago = nil;
NSInteger _zCount = [_zBoxes count];
if( _zCount > 1 )
{
// 依公式取出要用的 x2[1] 以後的 AGO matrixes,並且轉置矩陣
NSInteger _xDimension = [_agoBoxes count];
int _startIndex = 1;
NSMutableArray *_allFactors = [NSMutableArray new];
for( int _i = 0; _i < _zCount; ++_i )
{
NSMutableArray *_xT = [NSMutableArray new];
// Start from x(2)[1] to x(n)[m]
for( int _k = _startIndex; _k < _xDimension; ++_k )
{
// Hence, that _i needs to +1 to start in (1) to (n)
[_xT addObject:[[_agoBoxes objectAtIndex:_k] objectAtIndex:_i+1]];
}
[_allFactors addObject:_xT];
}

NSMutableArray *_solvedEquations = [_allFactors solveEquationsWithVector:_zBoxes];
//NSLog(@"_solvedEquations : %@", _solvedEquations);

// Desc sorting the abs() equation values
NSMutableArray *_sorts = [NSMutableArray new];
NSInteger _count = [_solvedEquations count];
for( NSInteger _i=0; _i<_count; _i++ )
{
CGFloat _equationValue = fabsf([[_solvedEquations objectAtIndex:_i] floatValue]);
NSMutableDictionary *_factoryInfo = [NSMutableDictionary new];
NSString *_equationName = [self _makeEquationNameByNumber:(_i + 2)];
[_factoryInfo setObject:_equationName forKey:kKRGreyGmEquationName];
[_factoryInfo setObject:[NSNumber numberWithFloat:_equationValue] forKey:kKRGreyGmEquationAbsValue];
[_factoryInfo setObject:[NSNumber numberWithInteger:(_i + 1)] forKey:kKRGreyGmEquationPatternIndex];
[_factoryInfo setObject:@0 forKey:kKRGreyGmEquationRanking];
[_sorts addObject:_factoryInfo];
}

// Asc sortting by _sorts
[_analyzedResults addObjectsFromArray:[_sorts sortByKey:kKRGreyGmEquationAbsValue ascending:NO]];

// Reset the ranking for all factories
NSInteger _ranking = 0;
for( NSMutableDictionary *_factoryInfo in _analyzedResults )
{
// Ranking number means the influence degree
[_factoryInfo setValue:[NSNumber numberWithInteger:_ranking] forKey:kKRGreyGmEquationRanking];
// Reset to pattern key-name that already customized by user
NSInteger _patternIndex = [[_factoryInfo objectForKey:kKRGreyGmEquationPatternIndex] integerValue];
NSString *_originalEquationName = [_keys objectAtIndex:_patternIndex];
[_factoryInfo setValue:_originalEquationName forKey:kKRGreyGmEquationName];

// To setup mappingResults dictionary to quickly search
[_mappingResults setObject:[_factoryInfo copy] forKey:_originalEquationName];

// To setup influenceDegrees
[_influenceDegrees addObject:_originalEquationName];

++_ranking;
}

}

}

-(void)clean
{
[_patterns removeAllObjects];
[_keys removeAllObjects];
[_analyzedResults removeAllObjects];
[_influenceDegrees removeAllObjects];
[_mappingResults removeAllObjects];
}

-(void)print
{
NSLog(@"influenceDegrees : %@", [_influenceDegrees componentsJoinedByString:@" > "]);
NSLog(@"analyzedResults : %@", [_analyzedResults description]);
NSLog(@"mappingResults : %@", [_mappingResults description]);
}


@end
2 changes: 1 addition & 1 deletion ML/KRGreyModels/KRGreyGM1N.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import <Foundation/Foundation.h>
#import "KRGrey+Definition.h"
#import "KRGreyLib.h"

@interface KRGreyGM1N : NSObject

Expand Down
Loading

0 comments on commit 67c39a0

Please sign in to comment.