def call(self, interpreter, arguments, parentheses): if not isinstance(arguments[0], PseudoList): raise RuntimeError(parentheses, "Can only append to a list.") pseudoList = DeepCopy(arguments[0]) #Deep copy is inefficient pseudoList.getValues().append(arguments[1]) return PseudoList(pseudoList.getValues())
def call(self, interpreter, arguments, parentheses): if not isinstance(arguments[0], PseudoList): raise RuntimeError(parentheses, "Can only remove elements from a list.") pseudoList = DeepCopy(arguments[0]) pseudoList._checkIndex(parentheses, arguments[1]) #bad, using a private method but CBA del pseudoList.getValues()[int(arguments[1])] return PseudoList(pseudoList.getValues())
def call(self, interpreter, arguments, parentheses): if not isinstance(arguments[0], PseudoList): raise RuntimeError(parentheses, "Can only slice a list.") if not isinstance(arguments[1], float) and not isinstance(arguments[2], float): raise RuntimeError(parentheses, "Start and End indices must be numbers.") pseudoList = DeepCopy(arguments[0]) pseudoList._checkIndex(parentheses, arguments[1]) pseudoList._checkIndex(parentheses, arguments[2]) return PseudoList(pseudoList.getValues()[int(arguments[1]) : int(arguments[2])])
def integrate_verlet(cls): d = 1.0 g = 0.2 p, o = DP(cls.pos), DP(cls.oldpos) ox, oy = o.x, o.y # integrate p.x = p.x + (d * p.x) - (d * ox) p.y = p.y + (d * p.y) - (d * oy) + g cls.oldpos = DP(cls.pos) cls.pos = p
def addMountain(valuesVert, valuesEdge, mountA, mountB, mountH, function, end1, end2, desiredAmplitude, nSteps, nSegs): ''' adds the interface which hopefully looks like some small mountains ''' # If function is bad :: if the function is constant, or reaches a discontinuity try: function(0) except: # Give the interface a tag of 10 valuesEdgeP = [t if t != (mountA, mountB, 0) else (mountA, mountB, interfaceTag) for t in valuesEdge] return valuesVert, valuesEdgeP #remove line valuesEdgeP = [t for t in valuesEdge if t != (mountA,mountB,0) ] if function == None: return valuesVert, valuesEdgeP n = int(round(float(nSteps) / nSegs)) heights = [function(end1 + float(end2 - end1) * x / n) for x in range(n + 1)] amplitude = max(heights) - min(heights) heights = [mountH + x * float(desiredAmplitude) / amplitude for x in heights] # heights = heights[::-1] + heights heights *= nSegs H = len(heights) segLength = float(sizeX) / (H - 1) values = [(i * segLength, heights[i], interfaceTag) for i in range(H)] valuesVertP = COPY(valuesVert) valuesVertP = insertAndDelete(mountA, values[0], valuesVertP) valuesVertP = insertAndDelete(mountB, values[-1], valuesVertP) values = values[1:-1] V = len(values) #change valuesVert + valuesEdge L = len(valuesVertP) valuesVertP.extend( values ) valuesEdgeP.extend( [(mountA, L+1, interfaceTag), (L+V, mountB, interfaceTag)] ) valuesEdgeP.extend( (L+i, L+i+1, interfaceTag) for i in range(1, V) ) return valuesVertP, valuesEdgeP
def crossover(self, size=1, strategy='RAND'): # Simple set union strategy for crossing over. Takes the union of the # two schedules. UNTESTED if(strategy == 'UNION'): new_sched = {} for team, time in self.parent.schedule.items(): for team2, time2 in self.parent_two.schedule.items(): if(team == team2 and time2 == time): new_sched[team] = time self.times[time].append(team) else: new_sched[team] = time self.times[time].append(team) new_sched[team2] = time2 self.times[time2].append(team2) self.schedule = new_sched self.rebuild_times_from_sched() # Random strategy. Gives the first parent preference elif(strategy == 'RAND'): # Need a deep copy of the parents schedule to avoid modifying the # parent. new_sched = DeepCopy(self.parent.schedule) #Choose N(size) of from second parent to put into first parent for i in range(size): rand_key_one = choice(list(new_sched.keys())) rand_key_two = choice(list(self.parent_two.schedule.keys())) val_one = new_sched[rand_key_one] val_two = self.parent_two.schedule[rand_key_two] new_sched[rand_key_two] = val_one new_sched[rand_key_one] = val_two # Sets the schedule and rebuild the times matrix. self.schedule = new_sched self.rebuild_times_from_sched()
def densify(targetNodes, factor): for i, curr in enumerate(targetNodes): if (i == len(targetNodes) - 1): break next = targetNodes[i + 1] parent = curr.getparent() # Obtain data from both current and next nodes nodes = {'curr': curr, 'next': next} for key, node in nodes.items(): nodes[key] = { # 'time': DateTime.strptime(node[0].text, '%Y-%m-%dT%H:%M:%SZ'), 'lat': float(node.attrib['lat']), 'lon': float(node.attrib['lon']), 'node': node, } # using the geodata, create a virtual line to get information. line = Geo([nodes['curr']['lat'], nodes['curr']['lon']], [nodes['next']['lat'], nodes['next']['lon']]) # subdivide the line into corresponding chunks and add the to the tree after curr for j in range(factor): pointC = line.point(line.distance * (1 / (j + 1))) node = DeepCopy(curr) node.attrib['lat'] = '%.7f' % round(pointC[0], 7) node.attrib['lon'] = '%.7f' % round(pointC[1], 7) curr.addnext(node)
def report_collision_vs_world(cls, px, py, dx, dy): p = DP(cls.pos) o = DP(cls.oldpos) # calc velocity vx = p.x - o.x vy = p.y - o.y # find component of velocity parallel to collision normal dp = vx * dx + vy * dy # project velocity onto collision normal # nx, ny is normal velocity nx = dp * dx ny = dp * dy # px, py is tangent velocity tx = vx - nx ty = vy - ny # we only want to apply collision response forces # if the object is travelling into, and not out of, the collision if dp < 0: # f = cls.settings.get('FRICTION') f = 0.05 fx = tx * f fy = ty * f # self bounce constant should be else:where, i.e inside the object/tile/etc.. # b = 1 + cls.settings.get('BOUNCE') b = 1 + 0.3 bx = nx * b by = ny * b else: # moving out of collision, do not apply forces bx = by = fx = fy = 0 # project object out of collision p.x = p.x + px p.y = p.y + py # apply bounce+friction impulses which alter velocity o.x = o.x + px + bx + fx o.y = o.y + py + by + fy cls.pos = DP(p) cls.oldpos = DP(o)
def getPolygon(valuesVert, neighbors, prev, curr): ''' helper method for "polygonize" method ''' visited = [] while not curr in visited: visited.append(curr) neigh = COPY(neighbors[curr]) neigh.remove(prev) if len(neigh) == 1: prev, curr = curr, neigh.pop() elif len(neigh) == 2: nA, nB = neigh.pop(), neigh.pop() if getAngle(valuesVert, prev, curr, nA) > getAngle(valuesVert, prev, curr, nB): prev, curr = curr, nA else: prev, curr = curr, nB return (len(visited),) + tuple(map(lambda i: i+1, visited))
def getPolygon(valuesVert, neighbors, prev, curr): ''' helper method for "polygonize" method ''' visited = [] while not curr in visited: visited.append(curr) neigh = COPY(neighbors[curr]) neigh.remove(prev) if len(neigh) == 1: prev, curr = curr, neigh.pop() elif len(neigh) == 2: nA, nB = neigh.pop(), neigh.pop() if getAngle(valuesVert, prev, curr, nA) > getAngle( valuesVert, prev, curr, nB): prev, curr = curr, nA else: prev, curr = curr, nB return (len(visited), ) + tuple(map(lambda i: i + 1, visited))
prev_char_stack.append(prev_char1) key=':'.join(prev_ichar_stack) val=[DC(state), prob, chosen_p1] beam[key] = val if '<EOS>' == ivocab[index1]: #print(ivocab[index1], end="\n") break else: #print(ivocab[index1], end="") pass #print('\n'.join(beam.keys())) #print(beam) for k in range(1, 14): beam_buff = DC(beam) for key, val in beam.items(): if '<EOS>' in key: #print(key) continue prev_ichar_stack = key.split(':') state, prob, chosen_p = val probability = cuda.to_cpu(prob.data)[0].astype(np.float64) probability /= np.sum(probability) prob_with_index = [] for e, p in enumerate(probability): prob_with_index.append( [e, p, ivocab[e] ] ) prob_with_index.sort(key=lambda x:-1 * x[1] ) improval = False for l in range(3):