def adjust_alpha(self, alpha_i): """If alpha_i is very close to 0 or C, then just assign 0 or C to it. (computational errors, thank you) """ C = self.C if h.in_range(alpha_i, -self.eps, self.eps): return 0 if h.in_range(alpha_i, C - self.eps, C + self.eps): return C return alpha_i
def compute_F(self): """Compute target function F(alpha)""" # 1. Check constraints assert s.all(self.alpha >= 0) assert s.all(self.alpha <= self.C) alpha_t = self.alpha * self.T assert h.in_range(alpha_t.sum(), -self.eps, self.eps) # 2. Compute F sum1 = alpha_t.dot(self.K).dot(alpha_t) sum2 = self.alpha.sum() F = 0.5 * sum1 - sum2 assert F.shape == (1,1) F = F.tolist()[0][0] return F
def load_blocks_gareth(): response = requests.get( "https://codaexplorer.garethtdavies.com/api/blocks?limit=20000").json( ) blocks = response["blocks"]["nodes"] # HACK: Filter out blocks created that aren't within 2months from start time of Winter Special (12/10/19) two_months = datetime.timedelta(days=60) return [ block for block in blocks if in_range(block["dateTime"], [( datetime.datetime(year=2019, month=12, day=10, hour=14, tzinfo=pytz.timezone('America/Los_Angeles')), two_months)]) ]
def create_points_report(window_times=[], windowed_metrics={}, global_metrics={}, pk_mapping={}): logger = logging.Logger(__name__) blocks = load_blocks_gareth() logger.debug(f"Loaded {len(blocks)} Blocks") windows = [] for i, window_time in enumerate(window_times): window = [ block for block in blocks if in_range(block["dateTime"], [window_time]) ] windowed_metrics_modified = dict( map(lambda key: (key + str(i + 1), windowed_metrics[key]), windowed_metrics.keys())) windows.append((window, windowed_metrics_modified)) logger.debug(f"Filtered out {len(window)} Windowed Blocks") global_metrics = collect_metrics(blocks, global_metrics) windowed_metrics = [ collect_metrics(window, metrics) for (window, metrics) in windows ] metrics = [global_metrics] + windowed_metrics print(metrics) public_keys = set( chain.from_iterable([metric.keys() for metric in metrics])) logger.debug(f"Observed {len(public_keys)} Public Keys") report = defaultdict(dict) for public_key in public_keys: for metric in metrics: report[public_key].update(metric.get(public_key, {})) for public_key, user in pk_mapping.items(): if public_key in report: report[user] = report[public_key] del (report[public_key]) else: logger.debug(f"Did not find user={user} public_key={public_key}") return report
def create_points_report(window_times=[], windowed_metrics={}, global_metrics={}, pk_mapping={}): logger = logging.Logger(__name__) blocks = load_blocks() logger.debug(f"Loaded {len(blocks)} Blocks") windowed_blocks = [ block for block in blocks if in_range( block["protocolState"]["blockchainState"]["date"], window_times) ] logger.debug(f"Filtered out {len(windowed_blocks)} Windowed Blocks") global_metrics = collect_metrics(blocks, global_metrics) windowed_metrics = collect_metrics(windowed_blocks, windowed_metrics) metrics = [global_metrics, windowed_metrics] public_keys = set( chain.from_iterable(metrics.keys() for metrics in metrics)) logger.debug(f"Observed {len(public_keys)} Public Keys") report = defaultdict(dict) for public_key in public_keys: for metric in metrics: report[public_key].update(metric.get(public_key, {})) #print(json.dumps(report, indent=2)) for public_key, user in pk_mapping.items(): if public_key in report: report[user] = report[public_key] del (report[public_key]) else: logger.debug(f"Did not find user={user} public_key={public_key}") return report
def is_in_I_minus(self, i): assert i in range(self.n), "Invalid index i" return (self.T[i] == -1 and h.in_range(self.alpha[i], -self.eps, 0)) or \ (self.T[i] == 1 and h.in_range(self.alpha[i], self.C, self.C + self.eps))