def write(args, out): theme = DefaultTheme() prices = theme.getOilPrices() oil_min = prices._minPrice oil_max = prices._maxPrice for i in xrange(args.weeks): price = theme.getOilPrices().next() if args.normalize: price = normalize(price, oil_min, oil_max) out.write("%s\n" % price)
def testSneakyDrilling(self): # start a game, survey once by each player service = GameService(DefaultTheme()) name1 = "alice" well1 = name1[0].upper() # create the game, join it gameId = service.new(10, 10, 1) handle1 = service.join(gameId, name1, well1) game, player1 = service._readHandle(handle1) self.assertEquals(False, service.isStarted(handle1)) # start the game service.start(handle1) self.assertEquals(True, service.isStarted(handle1)) x, y = 0, 0 # survey site = Site.deserialize(service.survey(handle1, x, y)) site = Site.deserialize(service.erect(handle1, x, y)) well = Well.deserialize(service.drill(handle1, x, y)) self.assertRaises(WildcattingException, service.drill, handle1, x, y + 1)
def write(self, out): if not self.args.no_headers: site_ct = (self.args.width * self.args.height / self.args.reduce ** 2) if self.args.partition: site_ct /= self.args.partition ** 2 self.write_headers(site_ct, out) sim = Simulator(DefaultTheme()) for i in xrange(self.args.num): field = sim.field(self.args.width, self.args.height) val_funcs = self.ins + self.outs region = Region.map(field, val_funcs) if self.args.partition: regions = region.partition(field, self.args.partition, val_funcs) else: regions = [region] if self.args.reduce != 1: regions = [Region.reduce(r, self.args.reduce) for r in regions] for region in regions: self.write_values(region, self.ins, out) self.write_values(region, self.outs, out) out.write('\n')
def test_fill_flat(self): theme = DefaultTheme() for scale, width, height in [(8, 80,24), (4, 40, 12), (2, 20, 6)]: field = OilField(width, height) FlatFiller().fill(field) map = Region.map(field, val_funcs) region = Region.reduce(map, scale) self.assertEquals(len(region.sites), 30) for i in xrange(30): self.assertEquals(region.sites[i]['prob'], 10.0)
def test_choose(self): theme = DefaultTheme() field = OilField(80, 24) OilFiller(theme).fill(field) DrillCostFiller(theme).fill(field) PotentialOilDepthFiller(theme).fill(field) ReservoirFiller(theme).fill(field) surveying = Surveying.load(dir) coords = surveying.choose(field)
def test_visualize(self): theme = DefaultTheme() for scale, width, height in [(4, 40, 12), (2, 20, 6)]: field = OilField(width, height) OilFiller(theme).fill(field) DrillCostFiller(theme).fill(field) PotentialOilDepthFiller(theme).fill(field) ReservoirFiller(theme).fill(field) map = Region.map(field, val_funcs) print map region = Region.reduce(map, scale) self.assertEquals(len(region.sites), 30) print region
def testGameStart(self): service = GameService(DefaultTheme()) name1 = "alice" well1 = name1[0].upper() gameId = service.new(10, 10, 13) handle1 = service.join(gameId, name1, well1) game, player1 = service._readHandle(handle1) self.assertEquals(False, service.isStarted(handle1)) service.start(handle1) self.assertEquals(True, service.isStarted(handle1))
def testFreshField(self): rows = cols = 10 field = OilField(rows, cols) filler = OilFiller(DefaultTheme()) filler.fill(field) for row in xrange(field.getHeight()): for col in xrange(field.getWidth()): site = field.getSite(row, col) self.assertNotEqual(site, None) self.assert_(site.getProbability() >= 0) self.assert_(site.getProbability() <= 100)
def run(args): comp = components[args.component].load(args.agent) theme = DefaultTheme() sim = Simulator(theme) field = sim.field(args.width, args.height) site_ct = args.width * args.height vfs = [ OilProbability(theme, site_ct=site_ct, normalize=True), DrillCost(theme, site_ct, True) ] region = Region.map(field, val_funcs=vfs) if args.visualize: print region col, row = comp.choose(field) site = field.getSite(row, col) print(col, row), '->', site.getProbability(), site.getDrillCost()
def testGameWithoutSurvey(self): service = GameService(DefaultTheme()) name1 = "alice" well1 = name1[0].upper() name2 = "bob" well2 = name2[0].upper() name3 = "carol" well3 = name3[0].upper() # create the game, join it gameId = service.new(10, 10, 1) handle1 = service.join(gameId, name1, well1) handle2 = service.join(gameId, name2, well2) handle3 = service.join(gameId, name3, well3) game, player1 = service._readHandle(handle1) game, player2 = service._readHandle(handle2) game, player3 = service._readHandle(handle3) service.start(handle1) self.assertEquals(name1, service.getPlayersTurn(handle1)) self.assertEquals(name1, service.getPlayersTurn(handle2)) self.assertEquals(name1, service.getPlayersTurn(handle3)) site1 = Site.deserialize(service.survey(handle1, 0, 0)) service.endTurn(handle1) self.assertEquals(name2, service.getPlayersTurn(handle1)) self.assertEquals(name2, service.getPlayersTurn(handle2)) self.assertEquals(name2, service.getPlayersTurn(handle3)) service.endTurn(handle2) self.assertEquals(name3, service.getPlayersTurn(handle1)) self.assertEquals(name3, service.getPlayersTurn(handle2)) self.assertEquals(name3, service.getPlayersTurn(handle3)) service.endTurn(handle3) self.assertEquals(name1, service.getPlayersTurn(handle1)) self.assertEquals(name1, service.getPlayersTurn(handle2)) self.assertEquals(name1, service.getPlayersTurn(handle3))
def run(args): theme = DefaultTheme() ins = [] for i in args.inputs: ins.append(FieldCommand.val_map[i](theme, args.width * args.height, args.normalize)) outs = [] for o in args.outputs: outs.append(FieldCommand.val_map[o](theme, args.width * args.height, args.normalize)) fw = FieldWriter(args, theme, ins, outs) if args.file: with open(args.file, 'w') as f: fw.write(f) else: fw.write(sys.stdout)
def testSneakyErecting(self): # start a game, survey once by each player service = GameService(DefaultTheme()) name1 = "alice" well1 = name1[0].upper() # create the game, join it gameId = service.new(10, 10, 1) handle1 = service.join(gameId, name1, well1) game, player1 = service._readHandle(handle1) self.assertEquals(False, service.isStarted(handle1)) # start the game service.start(handle1) self.assertEquals(True, service.isStarted(handle1)) # survey site1 = Site.deserialize(service.survey(handle1, 0, 0)) # make sure we can't erect elsewhere self.assertRaises(WildcattingException, service.survey, handle1, 0, 1)
def testShortGame(self): # start a game, survey once by each player service = GameService(DefaultTheme()) name1 = "alice" well1 = name1[0].upper() name2 = "bob" well2 = name2[0].upper() # create the game, join it gameId = service.new(10, 10, 1) handle1 = service.join(gameId, name1, well1) handle2 = service.join(gameId, name2, well2) game, player1 = service._readHandle(handle1) game, player2 = service._readHandle(handle2) self.assertEquals(False, service.isStarted(handle1)) self.assertEquals(False, service.isStarted(handle2)) # make sure player 2 isn't the game master self.assertRaises(WildcattingException, service.start, handle2) # start the game service.start(handle1) # make sure both players see the game started self.assertEquals(True, service.isStarted(handle1)) self.assertEquals(True, service.isStarted(handle2)) # make sure it's week 1 game, player = service._readHandle(handle1) self.assertEquals(1, game.getWeek().getWeekNum()) # it's player 1's turn - make sure player 2 can't survey self.assertRaises(WildcattingException, service.survey, handle2, 0, 0) # survey as player 1 site1 = Site.deserialize(service.survey(handle1, 0, 0)) # make sure player 1 can't survey twice in one turn self.assertRaises(WildcattingException, service.survey, handle1, 0, 0) # end player 1's turn service.endTurn(handle1) # make sure player 1 can't survey anymore self.assertRaises(WildcattingException, service.survey, handle1, 0, 0) # make sure it's still week 1 game, player = service._readHandle(handle1) self.assertEquals(1, game.getWeek().getWeekNum()) # survey as player 2 site2 = Site.deserialize(service.survey(handle2, 0, 1)) # end player 2's turn service.endTurn(handle2) # make sure player 2 can't survey anymore self.assertRaises(WildcattingException, service.survey, handle2, 0, 0) # make sure week is 2 game, player = service._readHandle(handle1) self.assertEquals(2, game.getWeek().getWeekNum()) # make sure both players see the game ended self.assertEquals(True, service.isFinished(handle1)) self.assertEquals(True, service.isFinished(handle2))
import os import random import numpy as np import neurolab as nl from os.path import join, exists from wildcatting.theme import DefaultTheme from .data import OilProbability, DrillCost, Region theme = DefaultTheme() # Components are decision making entities which are currently all backed by # neural networks. The Component object provides a common interface for # initializing, saving, loading and training these components. While the RL # components (Surveying, Report, Drilling, and Sales) are intended to be # trained via Q-learning, they all support supervised learning based on # training data files. The purpose of this is threefold: testing, proof of # concept, and bootstrap. # # In the bootstrap case, an NN may even be trained on a smaller set of inputs # than will be provided at gameplay time. Additional initially zero-weighted # inputs may be inserted with the weights for those inputs learned only # through RL. class Component: @classmethod def init(cls, agent): comp = cls(join(agent, cls.name))