-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
iwawomaru
committed
Oct 5, 2016
1 parent
df318ce
commit 1851355
Showing
6 changed files
with
105 additions
and
117 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,17 @@ | ||
import sys,os | ||
import sys, os | ||
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../') | ||
|
||
from noh import Circuit | ||
from noh.circuit import Planner, PropRule, TrainRule | ||
from noh.components import Random, Const | ||
from noh.environments import Pong | ||
|
||
import numpy as np | ||
|
||
n_stat = Pong.n_stat | ||
n_act = Pong.n_act | ||
|
||
component_set = [] | ||
component_set.append(Random(n_input=n_stat, n_output=n_act)) | ||
component_set.append(Const(n_input=n_stat, n_output=n_act, const_output=1)) | ||
component_set.append(Const(n_input=n_stat, n_output=n_act, const_output=2)) | ||
component_set.append(Const(n_input=n_stat, n_output=n_act, const_output=3)) | ||
|
||
|
||
class SimpleProp(PropRule): | ||
component_id_list = range(4) | ||
def __init__(self, components): | ||
super(SimpleProp, self).__init__(components) | ||
self.id = self.component_id_list.pop(0) | ||
|
||
def __call__(self, data): | ||
return self.components[self.id](data) | ||
|
||
class EmplyProp(PropRule): | ||
def __init__(self, components): | ||
super(EmplyProp, self).__init__(components) | ||
def __call__(self, **kwargs): | ||
pass | ||
|
||
class PFCPlanner(Planner): | ||
def __init__(self, components, rule_dict={}, default_prop=None, default_train=None): | ||
super(PFCPlanner, self).__init__(components, rule_dict, default_prop=None, default_train=None) | ||
self.f_go = False | ||
self.n_components = len(components) | ||
|
||
def __call__(self, data): | ||
if not self.f_go: | ||
self.prop_rule = np.random.choice(self.rules.values()) | ||
self.f_go = True | ||
|
||
""" kashikoku shitai here """ | ||
if np.random.rand() < 0.1: | ||
self.stop() | ||
|
||
return self.prop_rule(data) | ||
|
||
def train(self, data=None, label=None, epoch=None): | ||
pass | ||
|
||
def stop(self): | ||
self.f_go = False | ||
|
||
def supervised_train(self, data=None, label=None, epochs=None, **kwargs): pass | ||
def unsupervised_train(self, data=None, label=None, epochs=None, **kwargs): pass | ||
def reinforcement_train(self, data=None, label=None, epochs=None, **kwargs): pass | ||
from noh.components import SuppressionBoosting | ||
|
||
|
||
if __name__ == "__main__": | ||
prop_rules = {} | ||
for i in xrange(4): | ||
prop_rules["prop"+str(i)] = SimpleProp | ||
|
||
n_stat = Pong.n_stat | ||
n_act = Pong.n_act | ||
|
||
model = Circuit(PFCPlanner, components=component_set, rule_dict=prop_rules, | ||
default_prop=None, default_train=None) | ||
model = SuppressionBoosting.create(n_stat, n_act, n_learner=4) | ||
|
||
env = Pong(model, render=True) | ||
while True: | ||
env.execute() | ||
env.execute() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from noh.components.random_component import Random | ||
from noh.components.const_component import Const | ||
from noh.components.const_component import Const | ||
from noh.components.suppression_boosting import SuppressionBoosting |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,67 @@ | ||
from noh import Circuit | ||
from noh.circuit import PropRule | ||
from noh.components import Random, Const | ||
import numpy as np | ||
|
||
|
||
class SimpleProp(PropRule): | ||
component_id_list = range(100) | ||
|
||
def __init__(self, components): | ||
super(SimpleProp, self).__init__(components) | ||
self.id = self.component_id_list.pop(0) | ||
|
||
def __call__(self, data): | ||
return self.components[self.id](data) | ||
|
||
|
||
class LearnerSet(Circuit): | ||
|
||
def __init__(self, components, RuleClassDict): | ||
super(LearnerSet, self).__init__(components, RuleClassDict) | ||
self.n_components = len(components) | ||
self.f_go = False | ||
|
||
@classmethod | ||
def create(cls, n_stat, n_act, n_learner): | ||
component_list = [Random(n_input=n_stat, n_output=n_act)] + \ | ||
[Const(n_input=n_stat, n_output=n_act, const_output=n) for n in xrange(1, n_learner)] | ||
|
||
PropRulesDict = {"prop"+str(i): SimpleProp for i in xrange(n_learner)} | ||
|
||
return LearnerSet(component_list, PropRulesDict) | ||
|
||
|
||
class PropLearner(PropRule): | ||
|
||
name_list = [] | ||
def __init__(self, components): | ||
super(PropLearner, self).__init__(components) | ||
|
||
def __call__(self, data): | ||
if not self.components["learner_set"].f_go: | ||
self.components["learner_set"].f_go = True | ||
self.components["learner_set"].set_default_prop(name=np.random.choice(self.name_list)) | ||
|
||
res = self.components["learner_set"](data) | ||
|
||
""" kashikoku shitai here """ | ||
if np.random.rand() < 0.1: | ||
self.components["learner_set"].f_go = False | ||
return res | ||
|
||
|
||
class SuppressionBoosting(Circuit): | ||
def __init__(self, PlannerClass, components): | ||
super(SuppressionBoosting, self).__init__() | ||
def __init__(self, components, RuleClassDict): | ||
super(SuppressionBoosting, self).__init__(components, RuleClassDict, default_prop_name="prop_learner") | ||
self.f_go = False | ||
|
||
@classmethod | ||
def create(cls, n_stat, n_act, n_learner): | ||
components = {"learner_set": LearnerSet.create(n_stat, n_act, n_learner), | ||
"suppressor": None} | ||
PropLearner.name_list = components["learner_set"].rules.keys() | ||
return SuppressionBoosting(components, {"prop_learner": PropLearner}) | ||
|
||
def stop(self): | ||
self.f_go = False |