def observe_initial(self, data, labels, initial_indices): self.all_active = self.all_active.union(initial_indices) name = "{}{}{}_{}".format(self.directory, os.path.sep, self.name, self.iteration) plot_combined(self.feat_x, self.feat_y, self.domain, None, (data, labels), None, name, initial_indices, set(), self.condition)
def test_adaptive_threshold(): random.seed(888) np.random.seed(888) domain = Domain.make([], ["x", "y"], [(0, 1), (0, 1)]) x, y = domain.get_symbols(domain.variables) formula = (x <= y) & (x <= 0.5) & (y <= 0.5) & domain.get_bounds() thresholds = {"x": 0.1, "y": 0.1} data, _ = RejectionEngine(domain, formula, x * x, 100000).get_samples(50) k = 4 nearest_neighbors = [] for i in range(len(data)): nearest_neighbors.append([]) for j in range(len(data)): if i != j: distance = 1 if any(data[i, b] != data[j, b] for b, v in enumerate(domain.variables) if domain.is_bool(v))\ else max(abs(data[i, r] - data[j, r]) / (domain.var_domains[v][1] - domain.var_domains[v][0]) for r, v in enumerate(domain.variables) if domain.is_real(v)) if len(nearest_neighbors[i]) < k: nearest_neighbors[i].append((j, distance)) else: index_of_furthest = None for fi, f in enumerate(nearest_neighbors[i]): if index_of_furthest is None or f[ 1] > nearest_neighbors[i][index_of_furthest][1]: index_of_furthest = fi if distance < nearest_neighbors[i][index_of_furthest][1]: nearest_neighbors[i][index_of_furthest] = (j, distance) print(nearest_neighbors) t = [[ sum(n[1] for n in nearest_neighbors[i]) / len(nearest_neighbors[i]) * (domain.var_domains[v][1] - domain.var_domains[v][0]) for v in domain.real_vars ] for i in range(len(nearest_neighbors))] t = np.array(t) print(t) print(data) # data = uniform(domain, 400) labels = evaluate(domain, formula, data) data = data[labels == 1] labels = labels[labels == 1] data, labels = OneClassStrategy.add_negatives(domain, data, labels, t, 1000) directory = "test_output{}adaptive{}{}".format( os.path.sep, os.path.sep, time.strftime("%Y-%m-%d %Hh%Mm%Ss")) os.makedirs(directory) name = os.path.join(directory, "combined.png") plot.plot_combined("x", "y", domain, formula, (data, labels), None, name, set(), set())
def observe_iteration(self, data, labels, formula, new_active_indices, solving_time, selection_time): self.iteration += 1 learned_labels = evaluate(self.domain, formula, data) name = "{}{}{}_{}".format(self.directory, os.path.sep, self.name, self.iteration) plot_combined(self.feat_x, self.feat_y, self.domain, formula, (data, labels), learned_labels, name, self.all_active, new_active_indices, condition=self.condition) self.all_active = self.all_active.union(new_active_indices)
def experiment(): random.seed(888) np.random.seed(888) start = time.time() domain = Domain.make(["b0", "b1", "b2"], ["x0", "x1"], [(0, 1), (0, 1)]) # thresholds = {"x": 0.1, "y": 0.1} # data, _ = RejectionEngine(domain, formula, x * x, 100000).get_samples(50) filename = "/Users/samuelkolb/Downloads/input-ijcai-rh/ijcai-rh_2_3_2_100_50_4_3.problem_0.train_dataset.data" data = np.loadtxt(filename, delimiter=",", skiprows=0) k = 4 nearest_neighbors = [] for i in range(len(data)): nearest_neighbors.append([]) for j in range(len(data)): if i != j: distance = 1 if any(data[i, b] != data[j, b] for b, v in enumerate(domain.variables) if domain.is_bool(v))\ else max(abs(data[i, r] - data[j, r]) / (domain.var_domains[v][1] - domain.var_domains[v][0]) for r, v in enumerate(domain.variables) if domain.is_real(v)) if len(nearest_neighbors[i]) < k: nearest_neighbors[i].append((j, distance)) else: index_of_furthest = None for fi, f in enumerate(nearest_neighbors[i]): if index_of_furthest is None or f[ 1] > nearest_neighbors[i][index_of_furthest][1]: index_of_furthest = fi if distance < nearest_neighbors[i][index_of_furthest][1]: nearest_neighbors[i][index_of_furthest] = (j, distance) print(nearest_neighbors) t = [[ sum(n[1] for n in nearest_neighbors[i]) / len(nearest_neighbors[i]) * (domain.var_domains[v][1] - domain.var_domains[v][0]) if domain.is_real(v) else 0 for v in domain.variables ] for i in range(len(nearest_neighbors))] t = np.array(t) * 1.5 print(t) # data = uniform(domain, 400) labels = np.ones(len(data)) data, labels = OneClassStrategy.add_negatives(domain, data, labels, t, 1000) directory = "output{}lariat-synthetic{}{}".format( os.path.sep, os.path.sep, time.strftime("%Y-%m-%d %Hh%Mm%Ss")) os.makedirs(directory) name = os.path.join(directory, "combined.png") plot.plot_combined("x0", "x1", domain, None, (data, labels), None, name, set(), set()) def learn_inc(_data, _labels, _i, _k, _h): # strategy = OneClassStrategy(RandomViolationsStrategy(10), thresholds) strategy = DecisionTreeSelection() learner = KCnfSmtLearner(_k, _h, strategy, "mvn") initial_indices = LearnOptions.initial_random(20)(list( range(len(_data)))) learner.add_observer( PlottingObserver(domain, directory, "run_{}_{}_{}".format(_i, _k, _h), domain.real_vars[0], domain.real_vars[1], None, False)) return learner.learn(domain, _data, _labels, initial_indices) (new_data, new_labels, learned_formula), k, h = learn_bottom_up(data, labels, learn_inc, 1, 1, 2, 4, None, None) duration = time.time() - start Formula(domain, learned_formula).to_file( os.path.join(directory, "result_{}_{}_{}.json".format(k, h, int(duration))))