def setUp(self):
        '''
        For MAP = 20 on both dimensions.
        
        The expected sequence of generated random numbers with seed 20 is:
        random.seed(20)
        random.uniform(0, 20) = 18.112793523490414
                                   --> MAP_MARKET VALUE is assumed to be 20.
        random.uniform(0, 20) = 13.725083140534052
                                   --> MAP_TECH VALUE is assumed to be 20.
        random.uniform(0.0, 5.0) = 3.832546281813221
                                   --> MIN_CUM_KNOWLEDGE is assumed to be 0.0
                                   and MAX_CUM_KNOWLEDGE is assumed to be 5.0
        
        The expected sequence of generated random numbers with seed 30 is:
        random.seed(30)
        random.uniform(0, 20) = 10.781631292116211
                                --> MAP_MARKET VALUE is assumed to be 20.
        random.uniform(0, 20) = 5.7839288727944105
                                --> MAP_TECH VALUE is assumed to be 20.
        random.uniform(0.0, 5.0) = 0.15018454275563531
                                --> MIN_CUM_KNOWLEDGE is assumed to be 0.0
                                and MAX_CUM_KNOWLEDGE is assumed to be 5.0
        '''


        self.sigma = Sigma()
        # 20 is given as seed. 
        random.seed(20)
        self.agent1 = Agent(0, 0, self.sigma)
        # 30 is given as seed.
        random.seed(30)
        self.agent2 = Agent(1, 1, self.sigma)
Exemple #2
0
    def test_double_item_pass(self):
        """Tests if the agent can handle more than a single item correctly"""
        class LocalDataStore(self.FakeDataStore):
            def __init__(self):
                self.item1_history = [Decimal(500)]
                self.item2_history = [Decimal(1000)]

            def get_item_ids(self):
                return ["item1.html", "item2.html"]

            def get_item_history(self, id):
                if id == "item1.html":
                    return self.item1_history
                elif id == "item2.html":
                    return self.item2_history
                else:
                    return super().get_item_history(id)

            def set_item_history(self, key, history):
                print("setting price key = '" + key + "', value = '" +
                      str(history) + "'")
                self.modified = True
                if key == "item1.html":
                    self.item1_history = history
                elif key == "item2.html":
                    self.item2_history = history
                else:
                    raise KeyError("Item '" + key + "' not mocked")

        class LocalEngine(self.TestEngine):
            def __init__(self):
                pass

            def fetch(self, server_url, item_id):
                if item_id == "item1.html":
                    return Decimal(385)
                elif item_id == "item2.html":
                    return Decimal(999)
                assert False

        notifier = self.TestNotifier()
        engine = LocalEngine()
        data_store = LocalDataStore()
        agent = Agent(data_store)

        agent.execute(notifier, engine)

        history1 = data_store.get_item_history("item1.html")
        history2 = data_store.get_item_history("item2.html")

        self.assertTrue(len(history1) == 2)
        self.assertTrue(history1[-1] == Decimal(385))
        self.assertTrue(len(history2) == 2)
        self.assertTrue(history2[-1] == Decimal(999))
        self.assertTrue(data_store.modified)
        self.assertTrue(notifier.notified)
        self.assertTrue(len(notifier.messages) == 2)
Exemple #3
0
def exec():
    data_stores = load_config("config.json", deserializer)

    for ds in data_stores:
        notifier = find_notifier(ds.task_id)
        engine = find_engine(ds.task_id)
        agent = Agent(ds)
        agent.execute(notifier, engine)

    save_config(data_stores, "config.json", serializer)
Exemple #4
0
    def test_invalid_history_length(self):
        """Tests what happens if the agent is given an invalid history length"""
        class LocalDataStore(self.FakeDataStore):
            def get_config_value(self, key):
                if key == "max_history_length":
                    return 0
                else:
                    return super().get_config_value(key)

        notifier = self.TestNotifier()
        engine = self.TestEngine()
        data_store = LocalDataStore()
        agent = Agent(data_store)

        with self.assertRaises(RuntimeError):
            agent.execute(notifier, engine)
Exemple #5
0
    def test_single_item_pass(self):
        """Tests if the agent can handle a single item normally that passes on"""

        notifier = self.TestNotifier()
        engine = self.TestEngine()
        data_store = self.FakeDataStore()
        agent = Agent(data_store)

        agent.execute(notifier, engine)

        history = data_store.get_item_history("item1.html")

        self.assertTrue(len(history) == 2)
        self.assertTrue(history[-1] == Decimal(385)
                        and history[-2] == Decimal(500))
        self.assertTrue(data_store.modified)
        self.assertTrue(notifier.notified)
        self.assertTrue(len(notifier.messages) == 1)
    def test_execution(self):
        """This end to end test starts a mock server, requests an item from it
        and checks if a mock data store is updated properly."""
        # create mock data store
        data_store = self.FakeDataStore("TEST_E2E1")

        notifier = self.TestNotifier()

        # create and execute the agent
        agent = Agent(data_store)
        agent.execute(notifier, self.TestEngine())

        # verify that the new cost is correct
        history = data_store.get_item_history("item1.html")
        self.assertTrue(len(history) == 2)
        self.assertAlmostEqual(history[1], 370.00)

        # assert that notifier works properly
        self.assertEqual(len(notifier.messages), 1)
        self.assertTrue(notifier.alerted)
Exemple #7
0
    def test_history_grows_too_large(self):
        """Tests the agent's behavior when the history grows larger than the limit"""
        class LocalDataStore(self.FakeDataStore):
            def __init__(self):
                self.item_history = [Decimal(500), Decimal(650)]

        notifier = self.TestNotifier()
        engine = self.TestEngine()
        data_store = LocalDataStore()
        agent = Agent(data_store)

        agent.execute(notifier, engine)

        history = data_store.get_item_history("item1.html")

        self.assertTrue(len(history) == 2)
        self.assertTrue(history[-2] == Decimal(650)
                        and history[-1] == Decimal(385))
        self.assertTrue(data_store.modified)
        self.assertTrue(notifier.notified)
        self.assertTrue(len(notifier.messages) == 1)
Exemple #8
0
    def test_single_item_fail(self):
        """Tests the agent's behavior when a single item fails on comparison"""
        class LocalEngine(self.TestEngine):
            def __init__(self):
                pass

            def fetch(self, server_url, item_id):
                return Decimal(685)

        notifier = self.TestNotifier()
        engine = LocalEngine()
        data_store = self.FakeDataStore()
        agent = Agent(data_store)

        agent.execute(notifier, engine)

        history = data_store.get_item_history("item1.html")

        self.assertTrue(len(history) == 2)
        self.assertTrue(history[-1] == Decimal(685))
        self.assertTrue(data_store.modified)
        self.assertTrue(notifier.notified)
        self.assertTrue(len(notifier.messages) == 0)
def train(trialMax=5000, delay=4000, policy=1):
    agent = Agent.StandardAgent()
    env = Environment.UnimodalEnvironment(delay=delay)
    env.reset()
    state = env.state
    rewardsFound = 0

    while rewardsFound != 120:
        print('Finding reward # ', rewardsFound + 1, end='\r')
        action = agent.act(state, env.trialTime)
        state, reward = env.step(action)
        agent.learn(state, reward, env.delay)

        if reward == 1:
            rewardsFound += 1
            print('\nFound reward: ', rewardsFound)
            print('Time to reward: ', env.trialTime)
            agent.learn(state, reward, env.delay)
            agent.policy = 1
            env.reset()
            state = env.state
class TestAgent(unittest.TestCase):

    def setUp(self):
        '''
        For MAP = 20 on both dimensions.
        
        The expected sequence of generated random numbers with seed 20 is:
        random.seed(20)
        random.uniform(0, 20) = 18.112793523490414
                                   --> MAP_MARKET VALUE is assumed to be 20.
        random.uniform(0, 20) = 13.725083140534052
                                   --> MAP_TECH VALUE is assumed to be 20.
        random.uniform(0.0, 5.0) = 3.832546281813221
                                   --> MIN_CUM_KNOWLEDGE is assumed to be 0.0
                                   and MAX_CUM_KNOWLEDGE is assumed to be 5.0
        
        The expected sequence of generated random numbers with seed 30 is:
        random.seed(30)
        random.uniform(0, 20) = 10.781631292116211
                                --> MAP_MARKET VALUE is assumed to be 20.
        random.uniform(0, 20) = 5.7839288727944105
                                --> MAP_TECH VALUE is assumed to be 20.
        random.uniform(0.0, 5.0) = 0.15018454275563531
                                --> MIN_CUM_KNOWLEDGE is assumed to be 0.0
                                and MAX_CUM_KNOWLEDGE is assumed to be 5.0
        '''


        self.sigma = Sigma()
        # 20 is given as seed. 
        random.seed(20)
        self.agent1 = Agent(0, 0, self.sigma)
        # 30 is given as seed.
        random.seed(30)
        self.agent2 = Agent(1, 1, self.sigma)

                
    def tearDown(self):
        '''
        Releases the used sources for the tests.
        t        '''
        self.sigma = None
        self.agent1 = None
        self.agent2 = None

    def test_constructor(self):
        '''
        Tests the constructor of Agent class.
        '''
        # Tests the values for agent1 with seed 20.
        self.assertEqual(0, self.agent1.agent_id)
        self.assertEqual(0, self.agent1.entry_cycle)
        self.assertEqual(1, self.agent1.is_active)
        self.assertEqual(0.06790318043362997, self.agent1.sigma_m)
        self.assertEqual(1.3511240781010534, self.agent1.sigma_k)
        self.assertEqual(18.112793523490414, self.agent1.map_market)
        self.assertEqual(13.725083140534052, self.agent1.map_knowledge)
        self.assertEqual(3.832546281813221, self.agent1.cum_knowledge)
        self.assertEqual(0, self.agent1.cycle_realized_learning)
        self.assertEqual(0, self.agent1.next_map_market)
        self.assertEqual(0, self.agent1.next_map_knowledge)
        self.assertEqual(None, self.agent1.alliance)

        # Tests the values for agent2 with seed 30.
        self.assertEqual(1, self.agent2.agent_id)
        self.assertEqual(1, self.agent2.entry_cycle)
        self.assertEqual(1, self.agent2.is_active)
        self.assertEqual(0.09231151831518464, self.agent2.sigma_m)
        self.assertEqual(2.1445491468857547, self.agent2.sigma_k)
        self.assertEqual(10.781631292116211, self.agent2.map_market)
        self.assertEqual(5.7839288727944105, self.agent2.map_knowledge)
        self.assertEqual(0.15018454275563531, self.agent2.cum_knowledge)
        self.assertEqual(0, self.agent2.cycle_realized_learning)
        self.assertEqual(0, self.agent2.next_map_market)
        self.assertEqual(0, self.agent2.next_map_knowledge)
        self.assertEqual(None, self.agent2.alliance)

    def test_reset(self):
        '''
        Tests the reset method of the Agent class.
        '''
        self.agent1.make_alliance(self.agent2)
        self.agent1.cycle_realized_learning = 10
        self.agent1.next_map_market = 0.3
        self.agent1.next_map_knowledge = 0.5
        
        self.agent1.reset()
        
        self.assertEqual(0, self.agent1.cycle_realized_learning)
        self.assertEqual(0, self.agent1.next_map_market)
        self.assertEqual(0, self.agent1.next_map_knowledge)
        self.assertEqual(None, self.agent1.alliance)
    
    def test_exit(self):
        '''
        Tests the exit method of Agent class.
        '''
        self.agent1.exit()
        
        self.assertEqual(0, self.agent1.is_active)
        
    def test_in_learning_margin(self):
        '''
        Tests the in_learning_margin method of Agent class.
        '''
        pass # since learning margin = 0 always returns 1.

    def test_in_learning_margin_exception(self):
        '''
        Tests the in_learning_margin method of Agent class for invalid input.
        '''
        self.assertRaises(InputError, Agent.in_learning_margin, self.agent1, -5)
        
    def test_make_alliance(self):
        '''
        Tests the make_alliance method of Agent class.
        '''
        self.agent1.make_alliance(self.agent2)
        self.assertEqual(self.agent2, self.agent1.alliance)
        self.assertEqual(self.agent1, self.agent2.alliance)

    def test_make_alliance_exception(self):
        '''
        Tests the make_alliance method of Agent class for invalid input.
        '''
        self.assertRaises(InputError, Agent.make_alliance, self.agent1, \
                                                           None)
        self.assertRaises(InputError, Agent.make_alliance, self.agent1, \
                                                           self.agent1)
        
    def test_calculate_next_position(self):
        '''
        Tests the calculate_next_position method of Agent class.
        MAP = 20.0, BETA = 6.0 (MAX_TIP = 9.0), ALPHA = 0.2
        
        Agent_1 (map_market, map_knowledge) =
                (18.112793523490414, 13.725083140534052)
        Agent_2 (map_market, map_knowledge) =
                (10.781631292116211, 5.7839288727944105)
        '''
        
        self.agent1.calculate_next_position()
        self.assertEqual(0, self.agent1.next_map_market)
        self.assertEqual(0, self.agent1.next_map_knowledge)
        
        self.agent1.make_alliance(self.agent2)
        self.agent1.calculate_next_position()
        self.agent2.calculate_next_position()

        # learning of the agent_1 is null so no movement for it
        self.assertAlmostEqual(18.112793523490414, self.agent1.next_map_market)
        self.assertAlmostEqual(13.725083140534052, self.agent1.next_map_knowledge)

        # learning of the agent_2 in market dimension is null
        self.assertAlmostEqual(10.781631292116211, self.agent2.next_map_market)
        self.assertAlmostEqual(5.991987079, self.agent2.next_map_knowledge)

    def test_move_next_position(self):
        '''
        Tests the move_next_position method of Agent class.
        '''
        self.agent1.make_alliance(self.agent2)
        self.agent1.calculate_next_position()
        self.agent2.calculate_next_position()
        self.agent1.move_next_position()
        self.agent2.move_next_position()
            
        self.assertAlmostEqual(18.112793523490414, self.agent1.map_market)
        self.assertAlmostEqual(13.725083140534052, self.agent1.map_knowledge)

        self.assertAlmostEqual(10.781631292116211, self.agent2.map_market)
        self.assertAlmostEqual(5.991987079, self.agent2.map_knowledge)
    def test_execution(self):
        """Tests the execution of the system by reading a configuration file, fetching
        requests from a dummy server, checking if the notification component is
        used properly, and if the persistence component works as expected."""
        # load data stores from configuration file
        def deserialize(task, data_str):
            if task == "TEST1":
                return Decimal(data_str)
            elif task == "TEST2":
                return Decimal(data_str)
            else:
                raise RuntimeError("Task not implemented")

        input_data_stores = load_config("./test/input_config.json", deserialize)

        # load the engines and notifiers
        notifiers = [find_notifier(input_data_stores[0].task_id), find_notifier(input_data_stores[1].task_id)]
        engines = [find_engine(input_data_stores[0].task_id), find_engine(input_data_stores[1].task_id)]

        for i in range(len(input_data_stores)):

            # check that we only have one price in history (in our config)
            before_items = input_data_stores[i].get_item_ids()

            for it in before_items:
                assert(len(input_data_stores[i].get_item_history(it)) == 1)

            # create and execute the agent
            agent = Agent(input_data_stores[i])
            agent.execute(notifiers[i], engines[i])

            # check if we fetched an additional price
            after_items = input_data_stores[i].get_item_ids()

            assert before_items == after_items

            for it in after_items:
                assert (len(input_data_stores[i].get_item_history(it)) == 2)

            # assert that notifier works properly
            if input_data_stores[i].task_id == "TASK1":
                self.assertEqual(len(notifiers[i].messages), 2)
            elif input_data_stores[i].task_id == "TASK2":
                self.assertEqual(len(notifiers[i].messages), 2)
            self.assertTrue(notifiers[i].alerted)

        # check that persistence works fine
        def serialize(task, data_point):
            if task == "TEST1":
                return str(data_point)
            elif task == "TEST2":
                return str(data_point)
            else:
                raise RuntimeError("Task not implemented")

        save_config(input_data_stores, "./test/output_config.json", serialize)

        output_data_stores = load_config("./test/output_config.json", deserialize)

        assert input_data_stores == output_data_stores

        os.remove("./test/output_config.json")