本文整理汇总了Python中pln.chainers.Chainer类的典型用法代码示例。如果您正苦于以下问题:Python Chainer类的具体用法?Python Chainer怎么用?Python Chainer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Chainer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DeceptionAgent
class DeceptionAgent(MindAgent):
def __init__(self):
self.chainer = None
print "Initialized DeceptionAgent."
def create_chainer(self, atomspace, stimulate_atoms=False):
self.chainer = Chainer(atomspace,
agent=self,
stimulateAtoms=stimulate_atoms,
allow_output_with_variables=False,
delete_temporary_variables=True)
self.chainer.add_rule(
ModusPonensRule(self.chainer, types.ImplicationLink))
def run(self, atomspace):
if self.chainer is None:
self.create_chainer(atomspace)
return
result = self.chainer.forward_step()
# if __VERBOSE__:
# print result
return result
def get_trails(self):
return self.chainer.trails
def get_history(self):
return self.chainer.history.get_history()
示例2: InferenceAgent
class InferenceAgent(MindAgent):
def __init__(self):
self.chainer = None
def create_chainer(self, atomspace, stimulate_atoms=True):
self.chainer = Chainer(atomspace,
agent=self,
stimulateAtoms=stimulate_atoms,
allow_output_with_variables=False,
preferAttentionalFocus=True,
delete_temporary_variables=True)
# ModusPonens:
# Implication smokes(x) cancer(X)
# smokes(Anna)
# |= cancer(Anna)
self.chainer.add_rule(
ModusPonensRule(self.chainer, types.ImplicationLink))
def run(self, atomspace):
if self.chainer is None:
self.create_chainer(atomspace)
return
if not check_result(atomspace):
result = self.chainer.forward_step()
return result
示例3: InferenceAgent
class InferenceAgent(MindAgent):
def __init__(self):
self.chainer = None
def create_chainer(self, atomspace):
self.chainer = Chainer(atomspace, stimulateAtoms=False)
# Note:
# The API for adding Rules and Links to a PLN Chainer is
# hard to use. It could be redesigned so that you would
# just pass a list of rule types and link types when you
# instantiate the Chainer, without having to be aware of
# the different sets of rules that require different
# argument sets.
for rule in RULES_CHAINER_LINKTYPE:
for link_type in LINK_TYPES:
print rule, link_type
self.chainer.add_rule(rule(self.chainer, link_type))
for rule in RULES_CHAINER:
self.chainer.add_rule(rule(self.chainer))
def run(self, atomspace):
if self.chainer is None:
self.create_chainer(atomspace)
return
result = self.chainer.forward_step()
if __VERBOSE__:
print result
return result
示例4: get_predicate_arguments
def get_predicate_arguments(atomspace, predicate_name):
"""
Finds the unique EvaluationLink for a predicate and returns its list
of arguments.
(Adapted from Alex's PLNUnitTester class)
:param atomspace: the atomspace where the predicate should be looked for
:param predicate_name: the name of the predicate
:return: a list of the predicate arguments
"""
chainer = Chainer(atomspace)
var = chainer.new_variable()
predicate = chainer.atomspace.add_node(types.PredicateNode, predicate_name)
template = chainer.link(types.EvaluationLink,
[predicate, var])
queries = chainer.lookup_atoms(template, "")
# The template needs to be removed from the results
queries.remove(template)
if len(queries) != 1:
if predicate_name == "undesired_outputs":
return None
else:
raise ValueError("Predicate {0} must have 1 EvaluationLink"
.format(predicate_name))
return queries[0].out[1].out
示例5: InferenceAgent
class InferenceAgent(MindAgent):
def __init__(self):
self.chainer = None
print "Initializing InferenceAgent."
def create_chainer(self, atomspace, stimulate_atoms=True):
self.chainer = Chainer(atomspace,
agent=self,
stimulateAtoms=stimulate_atoms,
allow_output_with_variables=False,
preferAttentionalFocus=True,
delete_temporary_variables=True)
# ModusPonens:
# Implication smokes(x) cancer(X)
# smokes(Anna)
# |= cancer(Anna)
self.chainer.add_rule(
ModusPonensRule(self.chainer, types.ImplicationLink))
def run(self, atomspace):
if self.chainer is None:
self.create_chainer(atomspace)
print "PLN Chainer created."
return
print "PLN continuing."
target = atomspace[scheme_eval_h(atomspace, "query")]
if not check_result(atomspace):
result = self.chainer.forward_step()
self.chainer._give_stimulus(target, TARGET_STIMULUS)
return result
示例6: DeductionAgent
class DeductionAgent(MindAgent):
def __init__(self):
self.chainer = None
def create_chainer(self, atomspace):
self.chainer = Chainer(atomspace, stimulateAtoms=False)
link_types = [types.InheritanceLink]
for link_type in link_types:
self.chainer.add_rule(DeductionRule(self.chainer, link_type))
def run(self, atomspace):
# Run is invoked on every cogserver cognitive cycle
# If it is the first time it has been invoked, then the chainer
# needs to be created
if self.chainer is None:
self.create_chainer(atomspace)
return
result = self.chainer.forward_step()
if __VERBOSE__:
print result
return result
def get_trails(self):
return self.chainer.trails
def get_history(self):
return self.chainer.history.get_history()
示例7: run_pln_example
def run_pln_example(self, filename):
self.atomspace.clear()
assert len(self.atomspace) == 0
# Todo: The variable 'tmp' is not used
tmp = open(filename, 'r')
if not opencog.scheme_wrapper.load_scm(self.atomspace, filename):
print 'unable to load file', filename
return
print filename
chainer = Chainer(self.atomspace,
stimulateAtoms=False,
agent=self,
learnRuleFrequencies=False,
allow_output_with_variables=True,
allow_backchaining_with_variables=True,
check_cycles=True)
try:
queries = chainer.get_predicate_arguments('query')
except ValueError, e:
try:
queries = chainer.get_predicate_arguments('queries')
except ValueError, e:
print e
return
示例8: InferenceAgent
class InferenceAgent(MindAgent):
def __init__(self):
self.chainer = None
def create_chainer(self, atomspace):
self.chainer = Chainer(atomspace, stimulateAtoms=False)
# EvaluationToMember, MemberToInheritance
self.chainer.add_rule(EvaluationToMemberRule(self.chainer))
self.chainer.add_rule(MemberToEvaluationRule(self.chainer))
self.chainer.add_rule(MemberToInheritanceRule(self.chainer))
self.chainer.add_rule(InheritanceToMemberRule(self.chainer))
# For predicates with 2 arguments,
# with the 0th argument made into a variable
self.chainer.add_rule(
GeneralEvaluationToMemberRule(self.chainer, 0, 2))
# ModusPonens:
# Implication smokes(x) cancer(X)
# smokes(Anna)
# |= cancer(Anna)
self.chainer.add_rule(
ModusPonensRule(self.chainer, types.ImplicationLink))
def run(self, atomspace):
if self.chainer is None:
self.create_chainer(atomspace)
return
result = self.chainer.forward_step()
return result
示例9: InferenceAgent
class InferenceAgent(MindAgent):
def __init__(self):
self.chainer = None
self.query = None
print "Initializing InferenceAgent."
def create_chainer(self, atomspace, stimulate_atoms=True):
"""
Creates the chainer for the "smokes" example. Optionally, you can
define the target query before calling this method, by defining a
Scheme expression named "query". For example, you can issue the
Scheme expression "(define query hasCancer)" in reference to the
predicate defined in smokes.scm before loading this agent. Once
defined, the target query will receive stimulus at every time step.
Stimulus requires the agent to be running in a CogServer.
For a complete example that incorporates this behavior, see
example.py here:
https://github.com/opencog/external-tools/tree/master/attention
"""
self.chainer = Chainer(atomspace,
agent=self,
stimulateAtoms=stimulate_atoms,
allow_output_with_variables=False,
preferAttentionalFocus=True,
delete_temporary_variables=True)
# ModusPonens:
# Implication smokes(x) cancer(X)
# smokes(Anna)
# |= cancer(Anna)
self.chainer.add_rule(
ModusPonensRule(self.chainer, types.ImplicationLink))
# stimulateAtoms is only enabled when the agent is ran inside the
# CogServer, since the functionality requires a CogServer and
# attention allocation
if self.chainer._stimulateAtoms:
self.query = scheme_eval_h(atomspace, "query")
def run(self, atomspace):
if self.chainer is None:
self.create_chainer(atomspace)
print "PLN Chainer created."
return
print "PLN continuing."
if not check_result(atomspace):
result = self.chainer.forward_step()
if self.query is not None:
self.chainer._give_stimulus(atomspace[self.query],
TARGET_STIMULUS)
return result
示例10: BackwardInferenceAgent
class BackwardInferenceAgent(MindAgent):
def __init__(self):
self.chainer = None
def create_chainer(self, atomspace):
self.chainer = Chainer(atomspace, stimulateAtoms = False, agent = self)
deduction_link_types = [types.InheritanceLink]
# types.SubsetLink, types.IntensionalInheritanceLink]
for link_type in deduction_link_types:
self.chainer.add_rule(rules.InversionRule(self.chainer, link_type))
self.chainer.add_rule(rules.DeductionRule(self.chainer, link_type))
# self.chainer.add_rule(rules.NotCreationRule(self.chainer))
# self.chainer.add_rule(rules.NotEliminationRule(self.chainer))
# for rule in rules.create_and_or_rules(self.chainer, 1, 5):
# self.chainer.add_rule(rule)
# self.chainer.add_rule(EvaluationToMemberRule(self.chainer))
# self.chainer.add_rule(MemberToInheritanceRule(self.chainer))
def run(self, atomspace):
# incredibly exciting futuristic display!
#import os
#os.system('cls' if os.name=='nt' else 'clear')
# try:
if self.chainer is None:
self.create_chainer(atomspace)
result = self.chainer.backward_step()
if result:
(outputs, inputs) = result
if False:
print '==== Inference ===='
print show_atoms(outputs), '<=', show_atoms(inputs)
print
print '==== Attentional Focus ===='
for atom in get_attentional_focus(atomspace)[0:30]:
print str(atom), atom.av
#print '==== Result ===='
#print output
#print '==== Trail ===='
#print_atoms( self.chainer.trails[output] )
if self.chainer._all_nonzero_tvs(inputs):
raw_input()
else:
print 'Invalid inference attempted'
示例11: SyllogismAgent
class SyllogismAgent(MindAgent):
def __init__(self):
self.chainer = None
def create_chainer(self, local_atomspace):
self.chainer = Chainer(
local_atomspace,
agent=self,
stimulateAtoms=False,
# can be set to True to use AA on syllogisms
preferAttentionalFocus=False,
allow_output_with_variables=True,
delete_temporary_variables=True,
)
# add rules here
# self.chainer.add_rule()
def run(self, local_atomspace):
if self.chainer is None:
self.create_chainer(local_atomspace)
print ("PLN Chainer created.")
return
print ("PLN continuing.")
result = self.chainer.forward_step()
return result
示例12: create_chainer
def create_chainer(self, atomspace):
self.chainer = Chainer(atomspace, stimulateAtoms = False, agent = self, learnRuleFrequencies=True)
# ImplicationLink is MixedImplicationLink, you could also have Extensional and Intensional Implication. etc. but that's a bit much.
similarity_types = [types.SimilarityLink, types.ExtensionalSimilarityLink, types.IntensionalSimilarityLink]
# types.EquivalenceLink]
conditional_probability_types = [types.InheritanceLink, types.SubsetLink, types.IntensionalInheritanceLink,
types.ImplicationLink]
for link_type in conditional_probability_types:
self.chainer.add_rule(rules.InversionRule(self.chainer, link_type))
self.chainer.add_rule(rules.DeductionRule(self.chainer, link_type))
self.chainer.add_rule(rules.ModusPonensRule(self.chainer, link_type))
self.chainer.add_rule(rules.InheritanceRule(self.chainer))
self.chainer.add_rule(rules.SimilarityRule(self.chainer))
self.chainer.add_rule(rules.NotCreationRule(self.chainer))
self.chainer.add_rule(rules.NotEliminationRule(self.chainer))
for rule in rules.create_and_or_rules(self.chainer, 1, 5):
self.chainer.add_rule(rule)
self.chainer.add_rule(rules.SubsetEvaluationRule(self.chainer))
self.chainer.add_rule(rules.InheritanceEvaluationRule(self.chainer))
self.chainer.add_rule(rules.ExtensionalSimilarityEvaluationRule(self.chainer))
self.chainer.add_rule(rules.EvaluationToMemberRule(self.chainer))
self.chainer.add_rule(rules.MemberToInheritanceRule(self.chainer))
self.chainer.add_rule(rules.MemberToSubsetRule(self.chainer))
self.chainer.add_rule(rules.AttractionRule(self.chainer))
示例13: run
def run(self, atomspace):
if self.chainer is None:
self.chainer = Chainer(atomspace, stimulateAtoms = True, agent = self)
self.chainer.add_rule(rules.InversionRule(self.chainer))
self.chainer.add_rule(rules.DeductionRule(self.chainer))
# incredibly exciting futuristic display!
#import os
#os.system('cls' if os.name=='nt' else 'clear')
try:
result = self.chainer.forward_step()
if result:
(output, inputs) = result
print '==== Inference ===='
print output,str(output.av),'<=',' '.join(str(i)+str(output.av) for i in inputs)
print
print '==== Attentional Focus ===='
for atom in get_attentional_focus(atomspace)[0:30]:
print str(atom), atom.av
#print '==== Result ===='
#print output
#print '==== Trail ===='
#print_atoms( self.chainer.trails[output] )
else:
print 'Invalid inference attempted'
except Exception, e:
print e
示例14: create_chainer
def create_chainer(self, atomspace, stimulate_atoms=False):
# if mode == 1 it is deception inference mode else it is deception
# behavior mode
if self.mode == 1:
enable_variable_outputs = True
else:
enable_variable_outputs = False
self.chainer = Chainer(atomspace,
agent=self,
stimulateAtoms=False,
preferAttentionalFocus=False,
allow_output_with_variables=enable_variable_outputs,
delete_temporary_variables=True)
# Transformation rules
# This rules are invoked randomly and are run in the background because,
# they imply the creation of AndLinks between different sets of Links so
# as to represent the existance of a state in the world or of state of knowledge
# being simulated. In the ure this wouldn't be required as AndLink is used to denote
# the existance of the required set of Links without the need of creating
# the AndLink. If one wants to match AndLinked set of Links then the QuoteLink
# can be used.(This is based on the assumption that ure doesn't make a fundamental
# change on how the pattern matcher works presently)
if self.mode == 1:
self.chainer.add_rule(ImplicationAndRule(self.chainer))
else:
self.chainer.add_rule(AndCreationRule(self.chainer, 2))
self.chainer.add_rule(AndCreationRule(self.chainer, 2, types.EvaluationLink))
self.chainer.add_rule(AndCreationRule(self.chainer, 3, types.EvaluationLink))
self.chainer.add_rule(ModusPonensRule(self.chainer, types.ImplicationLink))
示例15: create_chainer
def create_chainer(atomspace, rule_concept_nodes):
"""
:param atomspace: the atomspace to be used by the chainer
:param concept_nodes: list of ConceptNodes where the name is the name of a
rule optionally amended by a link_type separated with ":", e.g.
[ConceptNode "ModusPonensRule:ImplicationLink"]
:return: the chainer that has been created
"""
chainer = Chainer(atomspace,
stimulateAtoms=False,
allow_output_with_variables=True,
delete_temporary_variables=True)
for rule_concept_node in rule_concept_nodes:
rule_name, link_type = rule_concept_node.name.split(":")
chainer.add_rule(rules[rule_name](chainer, link_types[link_type]))
return chainer