Skip to content

Commit

Permalink
get working: conclusion parameters, basic templates
Browse files Browse the repository at this point in the history
  • Loading branch information
ldevesine committed Oct 6, 2015
1 parent e3bdb9b commit 109e658
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 23 deletions.
5 changes: 5 additions & 0 deletions src/ai_testing.py
Expand Up @@ -19,4 +19,9 @@
core['all']['longitude'] = -42.32
env = environment.Environment(core)
result = engine.build_argument(conclusions.Conclusion('no snow melt'), env)
print result

print '---------'

result = engine.build_argument(conclusions.Conclusion('no annual signal', (1, 10)), env)
print result
2 changes: 1 addition & 1 deletion src/calvin/reasoning/calculations.py
Expand Up @@ -117,7 +117,7 @@ def synth_gaussian(core, mean, variation):
return GaussianThreshold(mean, variation)


def past_avg_temp(core):
def past_avg_temp(core):
return 'cake'


16 changes: 7 additions & 9 deletions src/calvin/reasoning/conclusions.py
Expand Up @@ -35,10 +35,10 @@ class Conclusion(object):
Also represents the same thing but with arguments filled in
(like outlier 2).
"""
def __init__(self, name, params=()):
def __init__(self, name, *params):
self.name = name
#TODO: make sure params is always hashable.
self.params = params
self.params = tuple(params)

def canfill(self, other):
"""
Expand All @@ -61,16 +61,14 @@ def __repr__(self):
st += ': ' + ', '.join([str(param) for param in self.params])
return st

def update_env(self, working_env):
#TODO: filling conclusions...hmmmmm
def update_env(self, working_env, filler):
"""
updates a working environment with the conclusion-fillers in fill
"""
#if len(fill.params) != len(self.params):
# raise ValueError("Attempt to use a rule with incorrect number of "
# "conclusion parameters")
if len(filler.params) != len(self.params):
raise ValueError("Attempt to use a rule with incorrect number of "
"conclusion parameters")

working_env.new_rule(self)
#envdict.update(dict(zip(self.params, fill.params)))
working_env.new_rule(self, filler)
return working_env

2 changes: 1 addition & 1 deletion src/calvin/reasoning/confidence.py
Expand Up @@ -291,7 +291,7 @@ class Template(object):
confidence unification and performs said unification for said rules
"""

def __init__(self, increment=0, flip=False, priority=True):
def __init__(self, flip=False, priority=True, increment=0):
"""
Constructor takes the following parameters:
Expand Down
9 changes: 6 additions & 3 deletions src/calvin/reasoning/environment.py
Expand Up @@ -16,9 +16,12 @@ def new_scope(self):
def leave_scope(self):
self.variables.pop()

def new_rule(self, conclusion):
def new_rule(self, conclusion, filler):
self.conclusions.append(conclusion)

for param, fill in zip(conclusion.params, filler.params):
self.setvar(param, fill)
#TODO: do we unset this param when we leave the rule?

def leave_rule(self):
self.conclusions.pop()

Expand Down Expand Up @@ -77,7 +80,7 @@ def do_lookup(env):
return None
return do_lookup

def metadata(varname):
def metadata(varname, *args):
def do_lookup(env):
return env.core['all'][varname]
return do_lookup
Expand Down
11 changes: 9 additions & 2 deletions src/calvin/reasoning/rule_list.py
Expand Up @@ -6,6 +6,9 @@
obs, arg, sim = Observation, Argument, Simulation
r = make_rule

NOT = (True, True, 0)
OR = (False, False, 0)

"""
#ice comes from greenland or antarctica
Expand Down Expand Up @@ -94,6 +97,11 @@
<with not-great conf, can try both ways and poll user with results>
"""
r('smooth accumulation rate',
obs('<', 'max accumulation angle', 20), sound)



r('no snow melt',
arg('current temperature rarely above freezing'), sound)
r('no snow melt',
Expand Down Expand Up @@ -137,8 +145,7 @@


r(('no annual signal', 'depth interval'),
obs('within %', ('counted years', 'depth interval'), ('known timescale', 'depth interval'), .15), probable,
NOT) #TODO: make conf template work
obs('within %', ('counted years', 'depth interval'), ('known timescale', 'depth interval'), .15), probable, NOT)
define(('counted years', 'depth interval'),
'run straticounter on the depth interval!!!')
define(('known timescale', 'depth interval'),
Expand Down
12 changes: 8 additions & 4 deletions src/calvin/reasoning/rules.py
Expand Up @@ -37,11 +37,15 @@

all_rules = []

def make_rule(conc_name, rhs_list, validity=None):
def make_rule(conc, rhs_list, validity, template=()):
if isinstance(conc, basestring):
conc = [conc]
if not hasattr(rhs_list, '__iter__'):
rhs_list = [rhs_list]
if not isinstance(template, confidence.Template):
template = confidence.Template(*template)
#TODO - old conclusions needed other params sometimes; do I still?
all_rules.append(Rule(conclusions.Conclusion(conc_name), rhs_list, validity))
all_rules.append(Rule(conclusions.Conclusion(*conc), rhs_list, validity, template))
#TODO: guards; templates?

def get_rules(conclusion):
Expand Down Expand Up @@ -145,15 +149,15 @@ class Rule(object):
combination information.
"""

def __init__(self, conclusion, rhs_list, quality, guard=None, confTemplate=confidence.Template()):
def __init__(self, conclusion, rhs_list, quality, confTemplate, guard=None):
self.conclusion = conclusion
self.guard = guard
self.rhs_list = rhs_list
self.quality = quality
self.template = confTemplate

def run(self, conclusion, env):
working_env = self.conclusion.update_env(env)
working_env = self.conclusion.update_env(env, conclusion)
if self.guard and not self.guard.passed(working_env):
working_env.leave_rule()
return None
Expand Down
6 changes: 3 additions & 3 deletions src/cscience/components/baconplugin.py
Expand Up @@ -225,11 +225,11 @@ def find_mem_params(self, core):
core['all'].setdefault('accumulation memory mean', .7)
core['all'].setdefault('accumulation memory strength', 4)

str = core['all']['accumulation memory strength']
stren = core['all']['accumulation memory strength']
mean = core['all']['accumulation memory mean']

memorya = str * mean
memoryb = str * (1-mean)
memorya = stren * mean
memoryb = stren * (1-mean)

return (memorya, memoryb)

Expand Down

0 comments on commit 109e658

Please sign in to comment.