def calcSlackVector(self, element, b): """ calculates the slack vector adjustments for a particular node. @param element is the element from the node we want to send slack. @param b is the balanced vector. @return is the slack vector for the particular node. """ self.slack = VectorOps.subFrom(VectorOps.mult(b, element.weight),\ VectorOps.mult(element.drift, element.weight)) return self.slack
def calcDriftVector(self): """ calculates drift vector. Drift vector is the following vector: u(t) = e(t) + Δv(t), where Δv(t) = v(t) - v'(t) """ # calculate Δv(t) self.dv = VectorOps.subFrom(self.cm.localStatistics, \ self.cm.lastStatistics) # calculate drift vector self.cm.drift = VectorOps.addTo(self.cm.estimate,\ self.dv)
def compute_balanced_vec(self): """ computes the balanced vector from the elements that are contained in balancing group only. @return the balanced vector """ # initialize a balanced vector b self.b = [0] * self.mf.getDimension() self.total_weight = 0.0 # compute the balanced vector b for elem in self.balancing_group: self.w = elem.weight self.total_weight += self.w self.b = VectorOps.multAndAddTo(self.b, self.w, elem.drift) self.b = VectorOps.multBy(self.b, 1.0/self.total_weight) return self.b
def compute_balanced_vec(self): """ computes the balanced vector from the elements that are contained in balancing group only. @return the balanced vector """ # initialize a balanced vector b self.b = [0] * self.mf.getDimension() self.total_weight = 0.0 # compute the balanced vector b for elem in self.balancing_group: self.w = elem.weight self.total_weight += self.w self.b = VectorOps.multAndAddTo(self.b, self.w, elem.drift) self.b = VectorOps.multBy(self.b, 1.0 / self.total_weight) return self.b
def testAddTo(self): """ tests the addTo(self, a, b) method and prints the result. Final results should be [2,2,2,2,2]. """ self.true_result = [2, 2, 2, 2, 2] self.test_result = VectorOps.addTo(self.target, self.operand) self.assertEqual(self.true_result, self.test_result)
def calc_drift(self): """ calculates drift vector. Drift vector is computed as follows: u(t) = e(t) + delta-v(t), where delta-v(t) = v(t) = v'(t) """ # calculate delta-v self.dv = VectorOps.subFrom(self.cm.localStatistics,\ self.cm.lastStatistics) # calculate drift vector self.cm.drift = VectorOps.addTo(self.cm.estimate, self.dv) # now check if ball is monochromatic if not self.check_ball(self.cm.localStatistics): # if it is not, then send a REP-message to coordinator # (the algorithm here is exactly the same as in REQ_receipt()) self.REQ_receipt()
def testDeepCpy(self): """ tests if new vector is really different """ self.cpy_vector = [] self.cpy_vector = VectorOps.cpy(self.cpy_vector, self.target) # change the target vector a little self.target = [1, 2, 1, 1, 1] self.assertIsNot(self.cpy_vector, self.target)
def start_coord(self, raw_data, weight): """ starts the coordinator. @param raw_data is the data that coord-node has to process. @param weight is the weight of this node. @param ls is a list that contains all the nodes. """ # set the weight of the node to protocol self.set_weight(weight) # read all waiting, raw data and pass it to a sketch self._read_data(raw_data) # update last statistics vector to hold the localstats self._set_lastStatistics(self.ams.sketch) # update slack vector to 0 VectorOps.init_empty_vector(self.cm.slack, \ self.cm.lastStatistics) self.all_vectors[self.node_name] = \ [self.weight, self.cm.lastStatistics]
def recalcEstimateVector(self): """ calculates the estimate vector. """ # init sum of weights in the minimum permitted value # (so as to avoid division with zero) self.sumw = 0.000000000001 # for every single node for node in self.all_vectors: # get the weight and the last statistic vector self.w = float(self.all_vectors[node][0]) self.v = self.all_vectors[node][1] # compute the denominator self.sumw += self.w # and compute the numerator self.cm.estimate = VectorOps.addTo(self.cm.estimate, VectorOps.mult(self.v, self.w)) # calculate the final estimate vector self.cm.estimate = VectorOps.multBy\ (self.cm.estimate, 1.0/self.sumw)
def calc_drift(self): """ calculates drift vector. Drift vector is computed as follows: u(t) = e(t) + delta-v(t), where delta-v(t) = v(t) = v'(t) """ # calculate delta-v self.dv = VectorOps.subFrom(self.cm.localStatistics,\ self.cm.lastStatistics) # calculate drift vector self.cm.drift = VectorOps.addTo(self.cm.estimate, self.dv) # now check if ball is monochromatic if not self.check_ball(self.cm.localStatistics): # if it is not, then initiate a balancing process, self.coord_elem = self.createBalancingElement(self.node_name,\ self.cm.localStatistics, self.cm.drift, self.weight) # initiating the balancing group with this very element self.bp.addToBalancing(self.coord_elem)
def computeBalancedVector(self): """ computes the balanced_vector. b = sum(w(i) * u(i))/sum(w(i)), where node i ε balancing_group. """ # initialize a balanced vector b self.b = [0] * self.mf.getDimension() self.total_weight = 0.0 # compute the balanced vector b for elem in self.balancing_group: self.w = elem.weight self.total_weight += self.w self.b = VectorOps.multAndAddTo(self.b, self.w, elem.drift) self.b = VectorOps.multBy(self.b, 1.0/self.total_weight) # check if ball B(e(t), b) is monochromatic (step 1 of coord) if self.mf.isMonochromatic(self.coord.cm.estimate, self.b): self.successfulBalancing(self.b) else: self.unsuccessfulBalancing()
def computeBalancedVector(self): """ computes the balanced_vector. b = sum(w(i) * u(i))/sum(w(i)), where node i ε balancing_group. """ # initialize a balanced vector b self.b = [0] * self.mf.getDimension() self.total_weight = 0.0 # compute the balanced vector b for elem in self.balancing_group: self.w = elem.weight self.total_weight += self.w self.b = VectorOps.multAndAddTo(self.b, self.w, elem.drift) self.b = VectorOps.multBy(self.b, 1.0 / self.total_weight) # check if ball B(e(t), b) is monochromatic (step 1 of coord) if self.mf.isMonochromatic(self.coord.cm.estimate, self.b): self.successfulBalancing(self.b) else: self.unsuccessfulBalancing()
def calcNodeDriftVector(self): """ calculates the drift vector of a node. """ self.weightEstimate = 1.0 self.ratioW = self.weightEstimate / self.weight # compute statistics delta vector. # delta = (weight*localStatistics - weightEstimate*localEstimate - # (weight - weightEstimate)*globalEstimate) / weight self.cm.delta = VectorOps.multAndAdd(self.cm.localStatistics, \ self.weight, self.localEstimate) VectorOps.multAndAddTo(self.cm.delta, self.ratioW-self.weight,\ self.cm.globalV) # update drift vector VectorOps.cpy(self.cm.drift, self.cm.globalV) VectorOps.addTo(self.cm.drift, self.cm.delta) VectorOps.multAndAddTo(self.cm.drift, 1.0 / self.weight, self.cm.slack)
def calcNodeDriftVector(self): """ calculates the drift vector of a node. """ self.weightEstimate = 1.0 self.ratioW = self.weightEstimate/self.weight # compute statistics delta vector. # delta = (weight*localStatistics - weightEstimate*localEstimate - # (weight - weightEstimate)*globalEstimate) / weight self.cm.delta = VectorOps.multAndAdd(self.cm.localStatistics, \ self.weight, self.localEstimate) VectorOps.multAndAddTo(self.cm.delta, self.ratioW-self.weight,\ self.cm.globalV) # update drift vector VectorOps.cpy(self.cm.drift, self.cm.globalV) VectorOps.addTo(self.cm.drift, self.cm.delta) VectorOps.multAndAddTo(self.cm.drift, 1.0/self.weight, self.cm.slack)
def start(self, raw_data, weight): """ starts the protocol. Initialization phase of the coordinator-based algorithm is implemented, exactly as it is presented in the paper [lift2006]. @param raw_data is a list that contains items of unprocessed data. """ # first of all, set the weight of the node to protocol self.set_weight(weight) # read all waiting, raw data and pass it to a sketch self._read_data(raw_data) # update last statistics vector to hold the localstats self._set_lastStatistics(self.ams.sketch) # update slack vector to 0 VectorOps.init_empty_vector(self.cm.slack, \ self.cm.lastStatistics) # create an init message self.init_msg = self.create_init_msg() # send an 'INIT'-type message to coordinator self.sendMessage(self.init_msg) # finally set change state self.set_state('UNSAFE')
def initialization(self): """ inits the centralized protocol. """ # first of all set the weight self.weight = self.getNodeWeight() # hold the initial statistics vector to last statistics self.cm.lastStatistics = self.getDataFromNode() # set slack vector to 0 self.cm.slack = VectorOps.init_empty_vector(self.cm.slack, \ self.cm.lastStatistics) # create then init message self.init_msg = INIT(self.node_name, \ self.COORD, \ self.cm.lastStatistics,\ self.weight) # send message to network self.sendMessage(self.init_msg)
def testSubFrom(self): """ tests the subFrom(self, a, b) method """ self.true_result = [0, 0, 0, 0, 0] self.test_result = VectorOps.subFrom(self.target, self.operand) self.assertEqual(self.true_result, self.test_result)
def ADJ_SLK_receipt(self, message): """ Upon receipt of an ADJ_SLK message, add the value specified in the message to the value of the slack vector (δ <- δ + Δδ). """ self.cm.slack = VectorOps.addTo(self.cm.slack, message.content) self.set_state('SAFE')
def ADJ_SLK_receipt(self, message): """ Upon receipt of an ADJ_SLK message, add the value specified in the message to the value of the slack vector (δ <- δ + Δδ). """ self.cm.slack = VectorOps.addTo(self.cm.slack, message.content)
def testMultBy(self): """ tests the multBy(self, a b) method """ self.true_result = [1, 1, 1, 1, 1] self.test_result = VectorOps.multBy(self.target, self.operand) self.assertEquals(self.true_result, self.test_result)
def testCpy(self): """ tests if new vector has some value inside it. """ self.cpy_vector = [] self.cpy_vector = VectorOps.cpy(self.cpy_vector, self.target) self.assertTrue(self.cpy_vector)