def huffmanRebuildTree(nod): global nodeTree global bitIndex bitIndex += 1 if inputBinary[bitIndex] == "0": nod.left = node(None, None, None, None) huffmanRebuildTree(nod.left) nod.right = node(None, None, None, None) huffmanRebuildTree(nod.right) else: wordBinary = inputBinary[bitIndex + 1:bitIndex + 9] wordDec = int(wordBinary, 2) wordHex = format(wordDec, "02x") nod.word = wordHex bitIndex += 8
def huffmanDecompress(input): global output output = "" inputBinary = "" for char in input: inputBinary = inputBinary + format(int(char, 16), "04b") global nodeTree nodeTree = node(None, None, None, None) global bitIndex bitIndex = 1 def huffmanRebuildTree(nod): global nodeTree global bitIndex bitIndex += 1 if inputBinary[bitIndex] == "0": nod.left = node(None, None, None, None) huffmanRebuildTree(nod.left) nod.right = node(None, None, None, None) huffmanRebuildTree(nod.right) else: wordBinary = inputBinary[bitIndex + 1:bitIndex + 9] wordDec = int(wordBinary, 2) wordHex = format(wordDec, "02x") nod.word = wordHex bitIndex += 8 huffmanRebuildTree(nodeTree) def huffmanDecompressRecursion(nod): global nodeTree global bitIndex global output if nod.word != None: output = output + nod.word else: bitIndex += 1 if inputBinary[bitIndex] == "0": huffmanDecompressRecursion(nod.left) else: huffmanDecompressRecursion(nod.right) padLengthBinary = inputBinary[:2] padLength = int(padLengthBinary, 2) while bitIndex < len(inputBinary) - padLength - 1: huffmanDecompressRecursion(nodeTree) return output
def treePolicy(self, v, iter): while v.depth < self.maxDepth: if len(v.D) < self.branchingFactor: theta = self.getNextTheta(v, iter) traj, derivX, derivY = self.generateTrajectory(v, theta) self.xTraj = np.hstack( (self.xTraj, traj[0, :].reshape(self.nTrajPoints, 1))) self.yTraj = np.hstack( (self.yTraj, traj[1, :].reshape(self.nTrajPoints, 1))) r, o = self.evaluateTrajectory(v, traj, theta) v.D.append((theta, r)) # Update GP mapping from theta to r: v.GP.update(theta, r) # Create new node: if self.par.cbtsNodeBelief == 'noUpdates': vNewGMRF = v.gmrf # pass reference (since no updates) elif self.par.cbtsNodeBelief == 'sampledGMRF' and v.depth == 0: vNewGMRF = functions.sampleGMRF(v.gmrf) else: vNewGMRF = copy.deepcopy( v.gmrf) # copy GMRF (since copy will be updated) vNew = node(self.par, vNewGMRF, v.auv) vNew.rewardToNode = v.rewardToNode + self.discountFactor**v.depth * r vNew.totalReward = vNew.rewardToNode vNew.parent = v vNew.depth = v.depth + 1 vNew.actionToNode = theta v.children.append(vNew) # simulate GP update of belief for i in range(len(o)): vNew.auv.x = traj[0, i + 1] vNew.auv.y = traj[1, i + 1] if self.par.cbtsNodeBelief != 'noUpdates': Phi = functions.mapConDis(vNew.gmrf, vNew.auv.x, vNew.auv.y) vNew.gmrf.seqBayesianUpdate(o[i], Phi) vNew.auv.derivX = derivX vNew.auv.derivY = derivY gc.collect() return vNew else: v = self.bestChild(v)
def getNewTraj(self, auv, gmrf, iter): # Get gmrf with less grid points v0 = node(self.par, copy.deepcopy(gmrf), auv) # create node with belief b and total reward 0 self.xTraj = np.zeros((self.nTrajPoints, 1)) self.yTraj = np.zeros((self.nTrajPoints, 1)) for i in range(self.nIterations): vl = self.treePolicy(v0, iter) # get next node if vl is None: continue # max depth and branching reached # rollout futureReward = self.exploreNode(vl) vl.accReward = vl.rewardToNode + futureReward self.backUp(v0, vl, vl.accReward) bestTraj, derivX, derivY = self.getBestTheta(v0) for ix in range(self.nTrajPoints): if bestTraj[0, ix] < gmrf.xMin: bestTraj[0, ix] = gmrf.xMin angle = np.sign(derivY) * 0.99 * math.pi / 2 derivX = math.cos(angle) derivY = math.sin(angle) elif bestTraj[0, ix] > gmrf.xMax: bestTraj[0, ix] = gmrf.xMax angle = np.sign(derivY) * 1.01 * math.pi / 2 derivX = math.cos(angle) derivY = math.sin(angle) for iy in range(self.nTrajPoints): if bestTraj[1, iy] < gmrf.yMin: bestTraj[1, iy] = gmrf.yMin angle = math.pi / 2 - np.sign(derivX) * 0.99 * math.pi / 2 derivX = math.cos(angle) derivY = math.sin(angle) elif bestTraj[1, iy] > gmrf.yMax: bestTraj[1, iy] = gmrf.yMax angle = math.pi / 2 - np.sign(derivX) * 1.01 * math.pi / 2 derivX = math.cos(angle) derivY = math.sin(angle) return bestTraj, derivX, derivY
def create_tree(exp): stack = ["("] exp = exp[1:] root = node('') while stack and exp: curr = exp[0] exp = exp[1:] if (curr.isalpha()): op = stack[-1] if (op == "!" and root.left == None): root.left = node("!", node(curr)) elif (root.left == None): root.left = node(curr) elif (op in "+."): stack.pop() root.right = node(curr) temp = root root.value = op root = node('') root.left = temp elif (op == "!"): stack.pop() root.value = stack.pop() root.right = node("!", node(curr)) temp = root root = node('') root.left = temp elif curr in ".+!": stack.append(curr) elif (curr == "("): op = stack[-1] count, ind = 1, 0 for i in range(0, len(exp)): if (exp[i] == "("): count += 1 elif (exp[i] == ")"): count -= 1 if (count == 0): ind = i break new = "(" + exp[0:ind + 1] exp = exp[ind + 1:] if (op == "!" and root.left == None): root.left = node("!", create_tree(new)) elif (root.left == None): root.left = create_tree(new) elif (op in "+-"): root.right = create_tree(new) temp = root root.value = stack.pop() root = node('') root.left = temp elif (op == "!"): stack.pop() root.right = node("!", create_tree(new)) temp = root root.value = stack.pop() root = node('') root.left = temp elif (curr == ")"): stack.pop() if (root.value == ''): return root.left return root
from classes import gmrf import matplotlib.pyplot as plt import matplotlib import numpy as np import math matplotlib.use('TkAgg') N = 3 depth = 3 par = par('seqBayes', 'cbts', 'noUpdates', 'random', False, False, 1000, 0, 0, 0, 0, 0, 6, 3, 50, 50, 0.05, 0.5, 0.9) cbts = CBTS(par) auv = agent(par, par.x0, par.y0, par.alpha0) gmrf1 = gmrf(par, par.nGridX, par.nGridY, par.nEdge) v = node(par, gmrf, auv) derivXStart = auv.derivX derivYStart = auv.derivY fig = plt.figure() for i in range(N): v.auv.x = par.x0 v.auv.y = par.y0 v.auv.derivX = derivXStart v.auv.derivY = derivYStart # Get first trajectory tau = np.zeros((2, (par.nTrajPoints - 1) * depth + 1)) theta = (-1 + 2 * np.random.rand()) * np.eye(1) #theta = [-1] tau[:, 0:par.
def huffmanCompress(input): output = "" freqTable = [] for i in range(0, 256): freqTable.append([i, 0]) for i in range(0, len(input), 2): byteInHex = input[i:i + 2] byteInDec = int(byteInHex, 16) freqTable[byteInDec][1] += 1 freqTable = sorted(freqTable, key=lambda l: l[1], reverse=True) nodeTable = [] for charFrequency in freqTable: if charFrequency[1] > 0: nodeTable.append( node(charFrequency[1], charFrequency[0], None, None)) while len(nodeTable) > 1: left = nodeTable.pop() right = nodeTable.pop() nodeTable.append(node(left.freq + right.freq, None, left, right)) nodeTable = sorted(nodeTable, key=lambda node: node.freq, reverse=True) codeTable = [] for i in range(0, 256): codeTable.append(None) global outputBinary outputBinary = "" def huffmanCompressRecursion(node, bits): global outputBinary if node.word == None: outputBinary = outputBinary + "0" huffmanCompressRecursion(node.left, bits + "0") huffmanCompressRecursion(node.right, bits + "1") else: outputBinary = outputBinary + "1" + format(node.word, "08b") codeTable[node.word] = bits huffmanCompressRecursion(nodeTable[0], "") messageBinary = "" for i in range(0, len(input), 2): byteInHex = input[i:i + 2] byteInDec = int(byteInHex, 16) messageBinary = messageBinary + codeTable[byteInDec] outputBinary = outputBinary + messageBinary padLength = 4 - (len(outputBinary) + 2) % 4 if padLength == 4: padLength = 0 padString = format(padLength, "02b") outputBinary = padString + outputBinary while len(outputBinary) % 4 != 0: outputBinary = outputBinary + "0" for i in range(0, len(outputBinary), 4): binarySlice = outputBinary[i:i + 4] nibbleValue = int(binarySlice, 2) nibbleHex = format(nibbleValue, "x") output = output + nibbleHex return output
fluxbcNode = [] fluxbcNodes = {} aline = [] # read node geometry file totalNode = 0 totalElement = 0 totalHfluxElement = 0 for line in nodeFile: aline = line.splitlines() nodeinfo = aline[0].split() inode = nodeinfo[0] x = nodeinfo[1] y = nodeinfo[2] z = nodeinfo[3] nodes[int(inode)] = node(inode, x, y, z) totalNode += 1 totalNode = inode print("total node=" + str(inode)) # read element geometry file # readElement() # read flux boundary condition file i = int(0) totalPowerDeposit = 0. totalArea = 0. maxPowerDensity = 0.