def test_full2(self): initial_sigmas = np.array([10, 12, 12, 12, 14], dtype=np.int32) alpha = utils.borda(5) * 2 # kind-of-Borda weights = np.array([2, 2]) m = len(initial_sigmas) assert isinstance(initial_sigmas, np.ndarray) gaps = utils.sigmas_to_gaps(initial_sigmas, np.max(initial_sigmas)) logger.info('gaps={}'.format(gaps)) init_gaps = gaps t1 = current_milli_time() fractional_makespan, clp_res = clp_R_alpha_WCM.find_strategy( initial_sigmas, alpha, weights, mode='per_cand') t2 = current_milli_time() logger.warning('Took time {}ms, fractional_makespan={}'.format( t2 - t1, fractional_makespan)) # fractional_makespan = utils.weighted_makespan(frac_res, alpha, weights, initial_sigmas) clp_makespan = utils.weighted_makespan(clp_res, alpha, weights, initial_sigmas) logger.info('weights={} m={}frac={} CLP={}'.format( weights, m, fractional_makespan, clp_makespan)) self.assertLessEqual(fractional_makespan, clp_makespan)
def find_strategy(initial_sigmas, alpha, weights): if len(initial_sigmas) != len(alpha): raise ValueError(len(initial_sigmas) != len(alpha)) m = len(initial_sigmas) k = len(weights) # order voter according to weights, non-increasing ordered_voters = sorted(np.arange(k), key=lambda v: weights[v], reverse=True) current_awarded = initial_sigmas.copy() config_mat = np.zeros((m, k), dtype=int) for ell in ordered_voters: # now sort the candidates according to the current awarded score, high to low: candidates_sorted = sorted(utils.borda(m), key=lambda i: current_awarded[i], reverse=True) # now achieve inverse-sort: every candidate gets his rank (high-to-low) in sort ballot = np.zeros(m, dtype=int) for rank, cand in enumerate(candidates_sorted): ballot[cand] = rank config_mat[:, ell] = ballot current_awarded += weights[ell] * alpha[ballot] return config_mat
def test_calculate_awarded(self): m = 3 config_mat = np.array([[0, 1], [2, 1], [2, 0]]) initil_sigmas = np.array([10, 11, 12]) weights = np.array([1, 2]) alpha = utils.borda(m) # borda awarded = utils.weighted_calculate_awarded(config_mat, alpha, weights, initil_sigmas) self.assertEquals(awarded.tolist(), [12, 15, 14])
def test_find_strategy(self): initial_sigmas = np.array([5, 6, 6, 6, 7], dtype=np.int32) weights = np.array([1, 1]) k = len(weights) m = len(initial_sigmas) alpha = utils.borda(m) res = reverse_algorithm.find_strategy(initial_sigmas, alpha, weights) self.assertListEqual(np.ravel(res).tolist(), [4, 0, 1, 3, 2, 2, 3, 1, 0, 4])
def test_lp_solve(self): initial_sigmas = np.array([4, 6, 6, 8], dtype=np.int32) alpha = utils.borda(4) # Borda weights = np.array([2, 2]) m = len(initial_sigmas) target = 12 result = clp_R_alpha_WCM.lp_solve(m, alpha, weights, initial_sigmas, target) tol = 0.001 makespan = utils.weighted_fractional_makespan(initial_sigmas, result, alpha, weights) self.assertLessEqual(makespan, target + tol)
def test_fix_rounding_result_weighted(self): k = 2 m = 3 config_mat = np.array([[0, 1], [2, 2], [2, 0]]) initial_sigmas = np.array([0, 1, 1]) weights = np.array([1, 1]) alpha = utils.borda(m) # borda res_config_mat = clp_R_alpha_WCM.fix_rounding_result_weighted( config_mat, alpha, weights, initial_sigmas) self.assertListEqual( np.ravel(res_config_mat).tolist(), [0, 1, 1, 2, 2, 0])
def experiments(): for m in range(10, 110, 10): for k in [10]: for trial in range(trials): n = k * 2 ceil_log_n = int(np.ceil(np.log(n))) yield delayed(run)( n, utils.borda(m), utils.draw_zipf_weights(owners=k, W=k * ceil_log_n), m, trial, utils.draw_uniform_weighted( m, k * 2, utils.draw_zipf_weights(owners=n, W=n * ceil_log_n, return_len_k=True)))
def test_full(self): initial_sigmas = np.array([5, 6, 6, 6, 7], dtype=np.int32) k = 2 m = len(initial_sigmas) alpha = utils.borda(m) # borda assert isinstance(initial_sigmas, np.ndarray) gaps = utils.sigmas_to_gaps(initial_sigmas, np.max(initial_sigmas)) logger.info('gaps={}'.format(gaps)) init_gaps = gaps t1 = current_milli_time() fractional_makespan, clp_res = clp_R_alpha_UCM.find_strategy( initial_sigmas, alpha, k, mode='per_cand') t2 = current_milli_time() logger.warning('Took time {}ms, fractional_makespan={}'.format( t2 - t1, fractional_makespan)) # fractional_makespan = utils.makespan(initial_sigmas, frac_res, alpha=alpha) clp_makespan = utils.makespan(initial_sigmas, clp_res, alpha=alpha) gaps = utils.sigmas_to_gaps(initial_sigmas, clp_makespan) af_res = average_fit.solve_one_gaps(k, gaps, verbose=False) af_makespan = utils.makespan(initial_sigmas, af_res, alpha=alpha) logger.info('k={} m={} frac={} CLP={} AF={}'.format( k, m, fractional_makespan, clp_makespan, af_makespan)) # self.assertListEqual(clp_res.tolist(), # [[0, 0, 1, 1, 0], # [0, 1, 0, 1, 0], # [1, 0, 0, 0, 1], # [1, 0, 0, 0, 1], # [0, 1, 1, 0, 0]] # ) self.assertAlmostEqual(fractional_makespan, 10.0, delta=1e-3) self.assertEquals(clp_makespan, 10) self.assertEquals(af_makespan, 12)
def solve_one_gaps(k, init_gaps, verbose=False): m = len(init_gaps) score2mult = np.array([k] * m) init_gaps = np.array(init_gaps, dtype=float) gaps = init_gaps.copy() left = np.array([k] * m) potential = gaps / left num_scores = m * k config_mat = np.zeros((m, m), dtype=int) if verbose: strs = [ '{}/{}={}'.format(g, l, p) for g, l, p in zip(gaps, left, potential) ] print('\t\t'.join(strs)) while num_scores > 0: # choose cand with highest potential cand = np.argmax(potential) # choose which score to give does_fit = utils.borda(m) <= gaps[cand] still_left = score2mult > 0 does_fit_and_still_left = np.logical_and(does_fit, still_left) score_to_give = None if np.any(does_fit_and_still_left): score_to_give = does_fit_and_still_left.nonzero()[0][-1] else: score_to_give = still_left.nonzero()[0][-1] # does_fit = np.array([ j <= gaps[cand] for j in range(m)]) # still_left = np.array([score2mult[j] > 0 for j in range(m)]) # if we have still score of this type to give, and it doesn't cause the candidate to overflow score2mult[score_to_give] -= 1 config_mat[cand, score_to_give] += 1 gaps[cand] -= score_to_give left[cand] -= 1 # re-calculate potential if left[cand] > 0: potential[cand] = gaps[cand] / left[cand] else: potential[cand] = -1 num_scores -= 1 if verbose: strs = [ '{}/{}={}'.format(g, l, p) for g, l, p in zip(gaps, left, potential) ] strs[cand] = utils.bcolors.OKBLUE + str( score_to_give ) + '->' + utils.bcolors.WARNING + strs[cand] + utils.bcolors.ENDC print('\t\t'.join(strs)) logging.debug('final configs:') logging.debug(config_mat) return config_mat