Ejemplo n.º 1
0
def main():
    # Init loggers
    log.set_level("fine")
    log.set_sync(False)
    agent_log.set_level("fine")
    agent_log.set_sync(False)
    ure_logger().set_level("fine")
    ure_logger().set_sync(False)

    # Set main atomspace
    atomspace = AtomSpace()
    set_default_atomspace(atomspace)

    # Wrap environment
    wrapped_env = CartPoleWrapper(env, atomspace)

    # Instantiate CartPoleAgent, and tune parameters
    cpa = FixedCartPoleAgent(wrapped_env, atomspace)
    cpa.delta = 1.0e-16

    # Run control loop
    while not cpa.control_cycle():
        wrapped_env.render()
        time.sleep(0.1)
        log.info("cycle_count = {}".format(cpa.cycle_count))

    log_msg(agent_log, f"The final reward is {cpa.accumulated_reward}.")
Ejemplo n.º 2
0
def main():
    # Init loggers
    log.set_level("fine")
    log.set_sync(False)
    agent_log.set_level("fine")
    agent_log.set_sync(False)
    ure_logger().set_level("fine")
    ure_logger().set_sync(False)

    # Set main atomspace
    atomspace = AtomSpace()
    set_default_atomspace(atomspace)

    # Wrap environment
    wrapped_env = CartPoleWrapper(env)

    # Instantiate CartPoleAgent, and tune parameters
    cpa = CartPoleAgent(wrapped_env)
    cpa.delta = 1.0e-16

    # Run control loop
    while cpa.step():
        time.sleep(0.1)
        log.info("step_count = {}".format(cpa.step_count))

    print(f"The final reward is {cpa.accumulated_reward}.")
Ejemplo n.º 3
0
def run_message_passing_ure():
    res = execute_atom(atomspace, directed_message_edge_creation_rule)
    res = execute_atom(atomspace, create_initial_messages_rule)

    fc_message_sending_rule_name = DefinedSchemaNode("fc-message-sending-rule")

    DefineLink(fc_message_sending_rule_name, create_messages_rule)

    fc_message_sending_rbs = ConceptNode("fc-message-sending-rule")

    InheritanceLink(fc_message_sending_rbs, ConceptNode("URE"))

    MemberLink(fc_message_sending_rule_name, fc_message_sending_rbs)

    # Set URE maximum-iterations
    from opencog.scheme_wrapper import scheme_eval

    execute_code = \
        '''
        (use-modules (opencog rule-engine))
        (ure-set-num-parameter (ConceptNode "fc-message-sending-rbs") "URE:maximum-iterations" 30)
        '''

    scheme_eval(atomspace, execute_code)

    # chainer = ForwardChainer(atomspace,
    #                          ConceptNode("fc-message-sending-rule"),
    #                          SetLink())

    # log.set_level('FINE')
    # chainer = ForwardChainer(atomspace,
    #                          ConceptNode("fc-message-sending-rule"),
    #                          get_message(VariableNode("$V1"), VariableNode("$V2")),
    #                          VariableList(
    #                              TypedVariableLink(VariableNode("$V1"), TypeNode("ConceptNode")),
    #                              TypedVariableLink(VariableNode("$V2"), TypeNode("ConceptNode")))
    #                          )

    # chainer = BackwardChainer(atomspace,
    #                          ConceptNode("fc-message-sending-rule"),
    #                          get_message(VariableNode("$V1"), VariableNode("$V2")))

    chainer = BackwardChainer(
        atomspace, ConceptNode("fc-message-sending-rule"),
        get_message(VariableNode("$V1"), VariableNode("$V2")),
        VariableList(
            TypedVariableLink(VariableNode("$V1"), TypeNode("ConceptNode")),
            TypedVariableLink(VariableNode("$V2"), TypeNode("ConceptNode"))))

    chainer.do_chain()

    results = chainer.get_results()
    log.set_level('INFO')

    res = execute_atom(atomspace, create_node_value_rule)
Ejemplo n.º 4
0
Behavor trees are usually driven by dynamic data; however, to keep
the example simple, the below is static; it simply counts green and
red lights, and halts at the first red light.

The if-then is implemented via a matching clause with the pattern
matcher. When a match is seen, the matcher moves on to the next
clause.
"""

from opencog.atomspace import AtomSpace, TruthValue, types, get_type_name
from opencog.bindlink import satisfaction_link
from opencog.type_constructors import *
from opencog.logger import Logger, log

# Logging will be written to opencog.log in the current directory.
log.set_level('DEBUG')
log.info("Starting the stop-go demo")

# The atomspace where everything will live.
atomspace = AtomSpace()
set_type_ctor_atomspace(atomspace)

# The callback counts the number fo red and green lights.
# It returns a TruthValue of TRUE for green lights and FALSE for the
# red lights.  FALSE is interpreted as a mismatch (failure to satisfy)
# by the pattner matcher, and thus, the pattern matcher will backtrack
# and sarch for a different solution. Since the example below contains
# no variables, it will just backtrack to the start, and then report
# non-satisfiability (which is what we want, when we get a red light).
green = 0
red = 0
from opencog.ure import ForwardChainer
from opencog.ure import BackwardChainer
from opencog.bindlink import execute_atom
from opencog.type_constructors import *
from opencog.utilities import initialize_opencog

from opencog.logger import Logger, log

log.set_level('INFO')

atomspace = AtomSpace()
initialize_opencog(atomspace)

TV_TRUE = TruthValue(1.0, 1.0)

EDGE_KEY = PredicateNode('edge')
DIRECTED_EDGE_KEY = PredicateNode('directed-edge')
MESSAGE_KEY = PredicateNode('message')


def get_directed_edge(a, b):
    directed_edge = EvaluationLink(DIRECTED_EDGE_KEY, ListLink(a, b))
    directed_edge.tv = TV_TRUE
    return directed_edge


def get_message(a, b):
    return EvaluationLink(MESSAGE_KEY, ListLink(a, b))


def send_initial_message(m, v1, v2):
Ejemplo n.º 6
0
import os
import pickle
import random
from gensim.models import Word2Vec
from matplotlib import pyplot
from opencog.atomspace import AtomSpace, types
from opencog.bindlink import execute_atom
from opencog.logger import log
from opencog.scheme_wrapper import scheme_eval
from opencog.type_constructors import *
from opencog.utilities import initialize_opencog
from scipy.spatial import distance
from scipy.stats import kendalltau, pearsonr, spearmanr
from sklearn.decomposition import PCA, KernelPCA

log.set_level("ERROR")

base_datasets_dir = os.getcwd() + "/datasets/"
base_results_dir = os.getcwd() + "/results/mooc/"
if not os.path.exists(base_results_dir):
    os.makedirs(base_results_dir)

mooc_actions_tsv = base_datasets_dir + "mooc_actions.tsv"
mooc_action_labels_tsv = base_datasets_dir + "mooc_action_labels.tsv"
mooc_action_features_tsv = base_datasets_dir + "mooc_action_features.tsv"
member_links_scm = base_results_dir + "member-links.scm"
evaluation_links_scm = base_results_dir + "evaluation-links.scm"
subset_links_scm = base_results_dir + "subset-links.scm"
attraction_links_scm = base_results_dir + "attraction-links.scm"
sentences_pickle = base_results_dir + "sentences.pickle"
deepwalk_bin = base_results_dir + "deepwalk.bin"
Ejemplo n.º 7
0
    def __init__(self, env):
        # Create Action Space. The set of allowed actions an agent can take.
        # TODO take care of action parameters.
        action_space = {ExecutionLink(SchemaNode(a)) for a in env.action_list}

        # Create Goal
        pgoal = EvaluationLink(PredicateNode("Reward"), NumberNode("1"))
        ngoal = EvaluationLink(PredicateNode("Reward"), NumberNode("0"))

        # Call super ctor
        OpencogAgent.__init__(self, env, action_space, pgoal, ngoal)


if __name__ == "__main__":
    # Init loggers
    log.set_level("debug")
    log.set_sync(False)
    agent_log.set_level("fine")
    agent_log.set_sync(False)
    ure_logger().set_level("debug")
    ure_logger().set_sync(False)

    # Set main atomspace
    atomspace = AtomSpace()
    set_default_atomspace(atomspace)

    # Wrap environment
    wrapped_env = ChaseWrapper(env)

    # ChaseAgent
    ca = ChaseAgent(wrapped_env)
Ejemplo n.º 8
0
Behavor trees are usually driven by dynamic data; however, to keep
the example simple, the below is static; it simply counts green and
red lights, and halts at the first red light.

The if-then is implemented via a matching clause with the pattern
matcher. When a match is seen, the matcher moves on to the next
clause.
"""

from opencog.atomspace import AtomSpace, TruthValue, types, get_type_name
from opencog.bindlink import satisfaction_link
from opencog.type_constructors import *
from opencog.logger import Logger, log

# Logging will be written to opencog.log in the current directory.
log.set_level('DEBUG')
log.info("Starting the stop-go demo")

# The atomspace where everything will live.
atomspace = AtomSpace()
set_type_ctor_atomspace(atomspace)


# The callback counts the number fo red and green lights.
# It returns a TruthValue of TRUE for green lights and FALSE for the
# red lights.  FALSE is interpreted as a mismatch (failure to satisfy)
# by the pattner matcher, and thus, the pattern matcher will backtrack
# and sarch for a different solution. Since the example below contains
# no variables, it will just backtrack to the start, and then report
# non-satisfiability (which is what we want, when we get a red light).
green = 0
Ejemplo n.º 9
0
        # Call super ctor
        OpencogAgent.__init__(self, env, action_space, pgoal, ngoal)

        # Overwrite some OpencogAgent parameters
        self.monoaction_general_succeedent_mining = False
        self.polyaction_mining = False
        self.temporal_deduction = False


if __name__ == "__main__":
    # Set main atomspace
    atomspace = AtomSpace()
    set_default_atomspace(atomspace)

    # Init loggers
    log.set_level("info")
    # log.set_sync(True)
    agent_log.set_level("debug")
    # agent_log.set_sync(True)
    ure_logger().set_level("debug")
    # ure_logger().set_sync(True)
    miner_log = MinerLogger(atomspace)
    miner_log.set_level("debug")
    # miner_log.set_sync(True)

    # Wrap environment
    wrapped_env = ChaseWrapper(env)

    # ChaseAgent
    ca = ChaseAgent(wrapped_env)