Exemple #1
0
 def setUp(self):
     self.world = World()
     self.world.add_currency(Currency('American Dolar', 'USD', 0))
     self.world.add_stock(Stock('CocaCola', 0, 0))
     self.world.set_start_date('2016.05.09')
     self.world.add_currency_rate(0, '2016.05.09', 150.0)
     self.world.add_currency_rate(0, '2016.05.10', 50.0)
     self.world.add_stock_price(0, '2016.05.09', 50.0)
     self.world.add_stock_price(0, '2016.05.10', 55.0)
class VisionWrapper:
    world = None
    blue = Array("f", range(3))
    yellow = Array("f", range(3))
    ball = Array("f", range(2))
    dims = Array("i", range(2))
    sim = None

    def __init__(self, sim):
        self.world = World()
        self.sim = sim
        self.done = sim.done
        self._run_subprocess()

    def _run_subprocess(self):
        self._subp = Process(target=self.mainLoop)
        self._subp.start()

    def mainLoop(self):
        dims = self.world.getDimensions()
        self.dims[0] = dims[0]
        self.dims[1] = dims[1]
        while not self.done.value:
            self.world.updateAll()
            self.keyframe()

    def keyframe(self):
        w = self.world
        keyframe = {
            "blue": {"position": w.getBluePosition(), "angle": w.getBlueAngle(), "velocity": w.getBlueVelocity()},
            "yellow": {
                "position": w.getYellowPosition(),
                "angle": w.getYellowAngle(),
                "velocity": w.getYellowVelocity(),
            },
            "ball": {"position": w.getBallPosition(), "velocity": w.getBallVelocity(), "angle": 0},
        }
        self.sim.keyFrame(keyframe, external=True)

    def getBlueBot(self):
        return self.sim.getBlueBot()

    def getBlueRobot(self):
        return self.getBlueBot()

    def getYellowBot(self):
        return self.sim.getYellowBot()

    def getYellowRobot(self):
        return self.getYellowBot()

    def getBall(self):
        return self.sim.getBall()

    def getDimensions(self):
        return tuple(self.dims)
Exemple #3
0
class WorldTests(unittest.TestCase):
    def setUp(self):
        self.world = World()
        self.world.add_currency(Currency('American Dolar', 'USD', 0))
        self.world.add_stock(Stock('CocaCola', 0, 0))
        self.world.set_start_date('2016.05.09')
        self.world.add_currency_rate(0, '2016.05.09', 150.0)
        self.world.add_currency_rate(0, '2016.05.10', 250.0)
        self.world.add_currency_rate(0, '2016.05.11', 255.0)
        self.world.add_currency_rate(0, '2016.05.12', 275.0)
        self.world.add_stock_price(0, '2016.05.09', 50.0)
        self.world.add_stock_price(0, '2016.05.10', 55.0)
        self.investor = Investor()
        self.investor.add_currency(0, 500)
        self.investor.add_stock(0, 300)

    def test_comparisonCond(self):
        self.world.next_day()
        cond = ComparisonCondition(False)
        cond.chosen_oper = cond.comparison_opers['<']
        cond.arg1_access_method = self.world.get_currency_rate
        cond.arg1_method_symbol_id = 0
        cond.arg1_method_date_str = '2016.05.09'
        cond.arg2_access_method = self.world.get_currency_rate_now
        cond.arg2_method_symbol_id = 0
        result = cond.eval()
        self.assertTrue(result)

    def test_comparisonCond2(self):
        self.world.next_day()
        cond = ComparisonCondition(False)
        cond.chosen_oper = cond.comparison_opers['<']
        cond.arg1 = 15
        cond.arg2_access_method = self.world.get_currency_rate_now
        cond.arg2_method_symbol_id = 0
        result = cond.eval()
        self.assertTrue(result)

    def test_masterCond(self):
        self.world.next_day()
        cond1 = ComparisonCondition(False)
        cond1.chosen_oper = cond1.comparison_opers['<']
        cond1.arg1_access_method = self.world.get_currency_rate
        cond1.arg1_method_symbol_id = 0
        cond1.arg1_method_date_str = '2016.05.09'
        cond1.arg2_access_method = self.world.get_currency_rate_now
        cond1.arg2_method_symbol_id = 0

        cond = ComparisonCondition(False)
        cond.chosen_oper = cond.comparison_opers['>']
        cond.arg1_access_method = self.world.get_currency_rate
        cond.arg1_method_symbol_id = 0
        cond.arg1_method_date_str = '2016.05.09'
        cond.arg2_access_method = self.world.get_currency_rate_now
        cond.arg2_method_symbol_id = 0

        master = MasterCondition()
        master.conditions = [cond1, cond]
        master.operators = ['&&']
        result = master.eval()
        self.assertFalse(result)
        master.operators = ['||']
        result = master.eval()
        self.assertTrue(result)

    def test_trendCond(self):
        self.world.next_day()
        self.world.next_day()
        self.world.next_day()
        cond = TrendCondition(self.world)
        cond.accessor_method = self.world.get_currency_rate
        cond.symbol_id = 0
        cond.number_of_days = 2
        cond.is_inc = True
        cond.growth_percent = 5
        res = cond.eval()
        self.assertTrue(res)
 def __init__(self, sim):
     self.world = World()
     self.sim = sim
     self.done = sim.done
     self._run_subprocess()
Exemple #5
0
class WorldTests(unittest.TestCase):
    def setUp(self):
        self.world = World()
        self.world.add_currency(Currency('American Dolar', 'USD', 0))
        self.world.add_stock(Stock('CocaCola', 0, 0))
        self.world.set_start_date('2016.05.09')
        self.world.add_currency_rate(0, '2016.05.09', 150.0)
        self.world.add_currency_rate(0, '2016.05.10', 50.0)
        self.world.add_stock_price(0, '2016.05.09', 50.0)
        self.world.add_stock_price(0, '2016.05.10', 55.0)

    def tearDown(self):
        self.world = None

    def test_getCorrectCurrency(self):
        self.assertEqual(self.world.get_currency_rate_now(0), 150.0)
        self.assertEqual(self.world.get_currency_rate(0, '2016.05.09'), 150.0)

    def test_getNonExistingCurrency(self):
        try:
            self.world.get_currency_rate_now(1)
            self.fail()
        except ValueError:
            self.assertEqual(1, 1)

    def test_nextDay(self):
        self.world.next_day()
        self.assertEqual(self.world.get_currency_rate_now(0), 50.0)
        self.assertEqual(self.world.get_currency_rate(0, '2016.05.09'), 150.0)
        self.assertEqual(self.world.get_currency_rate(0, '2016.05.10'), 50.0)

    def test_getCorrectStock(self):
        self.assertEqual(self.world.get_stock_price_now(0), 50.0)
        self.assertEqual(self.world.get_stock_price(0, '2016.05.09'), 50.0)

    def test_getNonExistingStock(self):
        try:
            self.world.get_stock_price_now(1)
            self.fail()
        except ValueError:
            self.assertEqual(1, 1)

    def test_nextDayStock(self):
        self.world.next_day()
        self.assertEqual(self.world.get_stock_price_now(0), 55.0)
        self.assertEqual(self.world.get_stock_price(0, '2016.05.09'), 50.0)
        self.assertEqual(self.world.get_stock_price(0, '2016.05.10'), 55.0)
class StrategyWrapper:
    world = None
    blue = Array("f", range(3))
    yellow = Array("f", range(3))
    ball = Array("f", range(2))
    dims = Array("i", range(2))
    sim = None

    def __init__(self, sim):
        self.world = World()
        self.sim = sim
        self.done = sim.done
        self._run_subprocess()

    def _run_subprocess(self):
        self._subp = Process(target=self.mainLoop)
        self._subp.start()

    def mainLoop(self):
        dims = self.world.getDimensions()
        self.dims[0] = dims[0]
        self.dims[1] = dims[1]
        while not self.done.value:
            self.world.updateAll()
            self.simpleStrategy()

    def simpleStrategy(self):
        """ Simple strategy used for simulation ahead - just follows the ball"""
        # simulates simple Follow-the-Ball strategy for both robots

        w = self.world
        blue = self.sim.objects["blue"]
        yellow = self.sim.objects["yellow"]
        bpos = w.getBluePosition()
        borient = w.getBlueAngle()
        ypos = w.getYellowPosition()
        yorient = w.getYellowAngle()

        gpos = w.getBallPosition()
        ym = getWheelSpeeds(ypos, yorient, gpos)
        bm = getWheelSpeeds(bpos, borient, gpos)
        blue.move(*bm)
        yellow.move(*ym)

    def getBlueBot(self):
        return self.sim.getBlueBot()

    def getBlueRobot(self):
        return self.getBlueBot()

    def getYellowBot(self):
        return self.sim.getYellowBot()

    def getYellowRobot(self):
        return self.getYellowBot()

    def getBall(self):
        return self.sim.getBall()

    def getDimensions(self):
        return tuple(self.dims)
Exemple #7
0
 def __init__(self):
     self.world = World()
     self.investor = Investor()
     self.event_world_adapter = EventWorldAdapter(self.world)
     self.start_investor_adapter = StartInvestorAdapter(self.investor)
     self.rules = {}
Exemple #8
0
class RealityController:
    def __init__(self):
        self.world = World()
        self.investor = Investor()
        self.event_world_adapter = EventWorldAdapter(self.world)
        self.start_investor_adapter = StartInvestorAdapter(self.investor)
        self.rules = {}

    def next_day(self):
        self.world.next_day()
        for k, v in self.rules.items():
            v.executed = False

    def add_start_cond(self, start_cond):
        self.start_investor_adapter.add_start_cond(start_cond)

    def add_event(self, event):
        self.event_world_adapter.add_event(event)

    def add_start_condition(self, start_cond):
        self.start_investor_adapter.add_start_cond(start_cond)

    def add_currency(self, currency):
        self.world.add_currency(currency)

    def add_stock(self, stock):
        self.world.add_stock(stock)

    def sell_stock_for_currency(self, stock_id, curr_amount, currency_id=None):
        stock_amount = self.investor.has_stock(stock_id)
        stock_amount_min = math.ceil(curr_amount / self.world.get_stock_price_now(stock_id))
        if stock_amount < stock_amount_min:
            return False
        else:
            self.investor.rem_stock(stock_id, stock_amount_min)
            stock_currency_id = self.world.get_stock_currency_id(stock_id)
            currency_added = stock_amount_min * self.world.get_stock_price_now(stock_id)
            self.investor.add_currency(stock_currency_id, currency_added)
            return True

    def sell_stock_amount(self, stock_id, stock_amount, currency_id=None):
        stock_amount_owned = self.investor.has_stock(stock_id)
        if stock_amount == 'ALL':
            stock_amount = stock_amount_owned
        if stock_amount_owned < stock_amount:
            return False
        else:
            self.investor.rem_stock(stock_id, stock_amount)
            stock_currency_id = self.world.get_stock_currency_id(stock_id)
            currency_added = stock_amount * self.world.get_stock_price_now(stock_id)
            self.investor.add_currency(stock_currency_id, currency_added)
            return True

    def sell_stock_part(self, stock_id, stock_part, third_arg=None):
        stock_amount_owned = self.investor.has_stock(stock_id)
        stock_part = math.ceil(stock_amount_owned * (stock_part / 100))
        self.investor.rem_stock(stock_id, stock_part)
        stock_currency_id = self.world.get_stock_currency_id(stock_id)
        currency_added = stock_part * self.world.get_stock_price_now(stock_id)
        self.investor.add_currency(stock_currency_id, currency_added)
        return True

    def buy_currency_amount(self, currency_bought_id, amount_bought, currency_sold_id):
        if amount_bought == 'MAX' and currency_sold_id != 'ANY':
            amount_bought = self.max_currency_to_buy_for_currency(currency_bought_id, currency_sold_id)
        elif amount_bought == 'MAX' and currency_sold_id == 'ANY':
            currency_sold_id = self.world.get_random_currency_id()
            amount_bought = self.max_currency_to_buy_for_currency(currency_bought_id, currency_sold_id)
        elif amount_bought != 'MAX' and currency_sold_id == 'ANY':
            currency_sold_id = self.get_currency_id_to_buy_currency_amount(amount_bought, currency_bought_id)
        fictional_amount_bought = amount_bought / self.world.get_currency_rate_now(currency_bought_id)
        currency_sold_amount = round(fictional_amount_bought * self.world.get_currency_rate_now(currency_sold_id), 2)
        currency_sold_owned = self.investor.has_currency(currency_sold_id)
        if currency_sold_amount > currency_sold_owned:
            return False
        else:
            self.investor.rem_currency(currency_sold_id, currency_sold_amount)
            self.investor.add_currency(currency_bought_id, amount_bought)
            return True

    def buy_stock_amount(self, stock_id, stock_amount, currency_id):
        if currency_id == 'OWN':
            currency_id = self.world.get_stock_currency_id(stock_id)
        if stock_amount == 'MAX' and currency_id != 'ANY':
            stock_amount = self.max_stock_to_buy_for_currency(stock_id, currency_id)
        elif stock_amount == 'MAX' and currency_id == 'ANY':
            stock_amount = self.max_stock_to_buy_for_currency(stock_id, self.world.get_random_currency_id())
        elif stock_amount != 'MAX' and currency_id == 'ANY':
            currency_id = self.get_currency_id_to_buy_stock_amount(stock_id, stock_amount)
        currency_owned = self.investor.has_currency(currency_id)
        currency_needed = round(self.world.get_stock_price_now(stock_id) * stock_amount, 2)
        if currency_id != self.world.get_stock_currency_id(stock_id):
            stock_currency_id = self.world.get_stock_currency_id(stock_id)
            currency_needed = round((currency_needed / self.world.get_currency_rate_now(
                stock_currency_id)) * self.world.get_currency_rate_now(currency_id), 2)
        if currency_owned < currency_needed:
            return False
        else:
            self.investor.rem_currency(currency_id, currency_needed)
            self.investor.add_stock(stock_id, stock_amount)

    def max_stock_to_buy_for_currency(self, stock_bought_id, currency_id):
        if currency_id == -1:
            currency_id = self.world.get_random_currency_id()
        stock_currency_id = self.world.get_stock_currency_id(stock_bought_id)
        stock_price_in_owned_currency = (self.world.get_stock_price_now(
            stock_bought_id) / self.world.get_currency_rate_now(stock_currency_id)) * self.world.get_currency_rate_now(
            currency_id)
        owned_currency_amount = self.investor.has_currency(currency_id)
        return math.floor(owned_currency_amount / stock_price_in_owned_currency)

    def max_currency_to_buy_for_currency(self, currency_bought_id, currency_sold_id):
        if currency_sold_id == -1:
            currency_sold_id = self.world.get_random_currency_id()
        currency_owned_amount = self.investor.has_currency(currency_sold_id)
        return (currency_owned_amount / self.world.get_currency_rate_now(
            currency_sold_id)) * self.world.get_currency_rate_now(currency_bought_id)

    def get_currency_id_to_buy_stock_amount(self, stock_id, stock_amount):
        for k, v in self.world.get_currencies().items():
            stocks = self.max_stock_to_buy_for_currency(stock_id, k)
            if stocks > stock_amount:
                return k

    def get_currency_id_to_buy_currency_amount(self, currency_amount, currency_id):
        for k, v in self.world.get_currencies().items():
            if k == currency_id:
                continue
            currency_amount_buyable = self.max_currency_to_buy_for_currency(currency_id, k)
            if currency_amount_buyable > currency_amount:
                return k

    def run_reality(self, date_start, date_stop):
        rules_list = list(self.rules.values())
        rules_list.sort(key=lambda x: x.priority, reverse=True)
        self.world.set_start_date(date_start)
        while True:
            for rule in rules_list:
                rule.execute()
            if dC.to_str(self.world.current_day) == date_stop:
                break
            else:
                self.next_day()