def __init__(self, scenarioData, environmentData, actionTable, updateStrategy): self._timeLines = [ TimeLine(environmentData, timeLineData, actionTable) for timeLineData in scenarioData.timeline ] self._updateStrategy = updateStrategy
class World: def __init__(self, map): self.map = map self.timeLine = TimeLine(map) #self.order((1,2), (4,1)) def locationInWorld(self, location): x,y = location return (1 <= x <= self.map.width) and (1 <= y <= self.map.height) def order(self, startLocation, finishLocation): #checks both locations are not the same if startLocation == finishLocation: print "ordered to same location" return # checks first startLocation has a soldier if startLocation not in self.timeLine.getCurrentState()[0]: print "no soldier at %s" % str(startLocation) return # checks both locations are within the world boundary if not self.locationInWorld(startLocation) or not self.locationInWorld(finishLocation): print "location not in world" return time = self.timeLine.getCurrentTimeState() soldier = time.getSoldierByLocation(*startLocation) time.addOrder(Order(soldier, startLocation, finishLocation)) def update(self): self.timeLine.goToTime(self.timeLine.getTimeNumeric()+1) def changeTimeTo(self, time): self.timeLine.goToTime(time) def getSoldiers(self): """returns a mapping from location to soldier""" return self.timeLine.getCurrentState()[0] def getOrders(self): """returns a mapping from soldier id to order""" return self.timeLine.getCurrentState()[1]
def test10(self): tl = [ [1, 100], [20, 90], [30, 70], [40, 60], [50, 55], ] result = TimeLine.intersect(tl) self.assertEqual(result, [[20, 90]])
class World: def __init__(self, map): self.map = map self.timeLine = TimeLine(map) #self.order((1,2), (4,1)) def locationInWorld(self, location): x, y = location return (1 <= x <= self.map.width) and (1 <= y <= self.map.height) def order(self, startLocation, finishLocation): #checks both locations are not the same if startLocation == finishLocation: print "ordered to same location" return # checks first startLocation has a soldier if startLocation not in self.timeLine.getCurrentState()[0]: print "no soldier at %s" % str(startLocation) return # checks both locations are within the world boundary if not self.locationInWorld(startLocation) or not self.locationInWorld( finishLocation): print "location not in world" return time = self.timeLine.getCurrentTimeState() soldier = time.getSoldierByLocation(*startLocation) time.addOrder(Order(soldier, startLocation, finishLocation)) def update(self): self.timeLine.goToTime(self.timeLine.getTimeNumeric() + 1) def changeTimeTo(self, time): self.timeLine.goToTime(time) def getSoldiers(self): """returns a mapping from location to soldier""" return self.timeLine.getCurrentState()[0] def getOrders(self): """returns a mapping from soldier id to order""" return self.timeLine.getCurrentState()[1]
def __init__(self, backend, parent=None, id=-1, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, name="PlayControls"): wx.Panel.__init__(self, parent, id, pos, size, style, name) self.backend = backend self.time = self.backend.getTime() self.Bind(wx.EVT_IDLE, self.OnIdle) self.playButton = wx.Button(self, -1, ">") self.Bind(wx.EVT_BUTTON, self.Play, self.playButton) self.toStartButton = wx.Button(self, -1, "|<") self.Bind(wx.EVT_BUTTON, self.ToStart, self.toStartButton) self.toEndButton = wx.Button(self, -1, ">|") self.Bind(wx.EVT_BUTTON, self.ToEnd, self.toEndButton) self.timeBox = wx.TextCtrl(self, -1, "0", style=wx.TE_PROCESS_ENTER) self.Bind(wx.EVT_TEXT_ENTER, self.setTime, self.timeBox) self.resetRangeButton = wx.Button(self, -1, "<>") self.Bind(wx.EVT_BUTTON, self.ResetRange, self.resetRangeButton) self.children = [] #size = self.GetClientSize() self.timeline = TimeLine( self.backend, self) #, size=(size.width-3*self.buttonwidth, size.height)) self.children.append(self.timeline) sizer = wx.BoxSizer(wx.HORIZONTAL) sizer.Add(self.toStartButton, 0, wx.EXPAND) sizer.Add(self.playButton, 0, wx.EXPAND) sizer.Add(self.toEndButton, 0, wx.EXPAND) sizer.Add(self.timeline, 1, wx.EXPAND) sizer.Add(self.timeBox, 0, wx.EXPAND) sizer.Add(self.resetRangeButton, 0, wx.EXPAND) self.SetSizer(sizer) sizer.Fit(self)
import sys sys.path.append('./TimeProcess') from TimeLine import TimeLine if __name__ == '__main__': di = {} f = open('time', 'r') for line in f.readlines(): line = line.strip() line = line.split(' ') key = line[0] # 将开始和结束时间点加入相应key对应的值中 if key not in di.keys(): start = int(line[1]) end = int(line[2]) di[key] = [[start, end]] else: start = int(line[1]) end = int(line[2]) di[key].append([start, end]) print(di) for key in di.keys(): time_list = di[key] # 计算并集 # result = union(time_list) # print(result) result = TimeLine.intersect(time_list) print(key, result) # print(line)
def test13(self): tl = [[1, 100], [10, 50], [40, 70]] result = TimeLine.intersect(tl) self.assertEqual(result, [[10, 50], [50, 70]])
def test14(self): tl = [[1, 3], [2, 4], [3, 5], [4, 6], [5, 7], [6, 8]] result = TimeLine.intersect(tl) self.assertEqual(result, [[2, 3], [3, 4], [4, 5], [5, 6], [6, 7]])
def test11(self): tl = [[1, 100], [20, 110], [0, 70]] result = TimeLine.intersect(tl) self.assertEqual(result, [[1, 70], [70, 100]])
def test1(self): tl = [] result = TimeLine.intersect(tl) self.assertEqual(result, [])
if self.TIMELINE.lastDAY != self.TIMELINE.DAY: self.TIMELINE.newDay = True if self.TIMELINE.newDay and self.TIMELINE.inTrade: self.__open_market() self.TIMELINE.newDay = False def update_price(self, mid_price): #应该用索引就可以! #接下去修改orderbook 加入中间价的计算! self.latest_price = mid_price self.price_stream.append(mid_price) if __name__ == '__main__': logging.basicConfig(filename='ham.log', filemode='w+', level=logging.INFO) timeline = TimeLine() fv = fvalue(timeline) CONTEXT = context(timeline, fv) orderbook = OrderBook(CONTEXT) agentpool = AgentPool(5000, CONTEXT) # count = 0 # for i in range(): # agentpool.select_an_agent() # if CONTEXT.latest_quote: # print(i) # orderbook.scan_quote() agentidlist = list(range(5000)) for rd in range(1, 5000): if CONTEXT.TIMELINE.inTrade: np.random.shuffle(agentidlist)
def test13(self): tl = [[2, 2], [2, 2]] result = TimeLine.union(tl) self.assertEqual(result, [[2, 2]])
def test7(self): tl = [[1, 10], [1, 9]] result = TimeLine.union(tl) self.assertEqual(result, [[1, 10]])
def test3(self): tl = [[1, 10], [20, 100]] result = TimeLine.intersect(tl) self.assertEqual(result, [])
def __init__(self, map): self.map = map self.timeLine = TimeLine(map)
def test15(self): tl = [[1, 3], [4, 6], [7, 9], [10, 12], [13, 15], [16, 18]] result = TimeLine.intersect(tl) self.assertEqual(result, [])
def test10(self): tl = [[1, 2], [3, 4], [5, 6], [7, 8]] result = TimeLine.union(tl) self.assertEqual(result, [[1, 2], [3, 4], [5, 6], [7, 8]])
return forward_FVlist def new_shock(self, shock_size=None, shock_ratio=None): if shock_size and not shock_ratio: self.FV += shock_size self.mu += shock_size elif shock_ratio: self.FV *= 1 + shock_ratio self.mu *= 1 + shock_ratio self.__snapshot() if __name__ == '__main__': from TimeLine import TimeLine fv = fvalue(TimeLine()) plt.plot(fv.forward_fv()) # plt.figure(figsize=(15,3)) # fv = fvalue(100,0.1,0.5) # for i in range(500): ## if i == 20: ## fv.new_shock(5) ## else: # fv.new_fv() # plt.plot(fv.timestream,fv.fvstream) #def brownian_path(N): # Δt_sqrt = math.sqrt(1 / N) # Z = np.random.randn(N) # Z[0] = 0
def test1(self): tl = [] result = TimeLine.union(tl) self.assertEqual(result, [])
def test9(self): tl = [[1, 10], [2, 9], [3, 8]] result = TimeLine.union(tl) self.assertEqual(result, [[1, 10]])
def test9(self): tl = [[1, 10], [5, 10]] result = TimeLine.intersect(tl) self.assertEqual(result, [[5, 10]])
def test10(self): tl = [[1, 3], [2, 4], [3, 5], [4, 6]] result = TimeLine.union(tl) self.assertEqual(result, [[1, 6]])
def test16(self): tl = [[1, 3], [3, 6], [6, 9], [9, 12], [12, 15], [15, 18]] result = TimeLine.intersect(tl) self.assertEqual(result, [[3, 3], [6, 6], [9, 9], [12, 12], [15, 15]])
def test11(self): tl = [[1, 3], [3, 5], [5, 7], [7, 9]] result = TimeLine.union(tl) self.assertEqual(result, [[1, 9]])