Example #1
0
def convex_limit(struct_points: np.ndarray) -> int:
    """Given points in order from best to worst,
    Returns the length of the maximum initial segment of points such that quasiconvexity is verified."""
    points: tp.List[float] = []
    d = len(struct_points[0])
    if len(struct_points) < 2 * d + 2:
        return len(struct_points) // 2
    for i in range(0, min(2 * d + 2, len(struct_points)), 2):
        points += [struct_points[i]]
    hull = ConvexHull(points[:(d + 1)], incremental=True)
    num_points = len(hull.vertices)
    k = len(points) - 1
    for i in range(num_points, len(points)):
        # We add the ith point.
        hull.add_points(points[i:(i + 1)])
        num_points += 1
        if len(hull.vertices) != num_points:
            return num_points - 1
        for j in range(i + 1, len(points)):
            # We check that the jth point is not in the convex hull;
            # this is ok if the jth point, added in the hull, becomes a vertex.
            hull_copy = copy.deepcopy(hull)
            hull_copy.add_points(points[j:j + 1])
            if len(hull_copy.vertices) != num_points + 1:
                return num_points - 1
    return k
Example #2
0
 def free_reward(self, obs):
     ch = ConvexHull(np.array(self.points), incremental=True)
     area = ch.area
     direction = np.array([math.cos(self.theta), math.sin(self.theta)])
     velocity = direction * 50
     newp = self.point + velocity
     ch.add_points(np.array([newp]))
     narea = ch.area
     return narea - area, newp
Example #3
0
def convex_limit(points: np.ndarray) -> int:
    """Given points in order from best to worst, 
    Returns the length of the maximum initial segment of points such that quasiconvexity is verified."""
    d = len(points[0])
    hull = ConvexHull(points[:d + 1], incremental=True)
    k = min(d, len(points) // 4)
    for i in range(d + 1, len(points)):
        hull.add_points(points[i:(i + 1)])
        if i not in hull.vertices:
            k = i - 1
            break
    return k
    def greedy_alg(self):
        end = False
        oracle_calls = 0

        # take an arbitrary element out of F
        random.shuffle(self.F)
        # label vector of the elements (binary classification {-1, 1}
        labels = -1 * np.ones(shape=(self.size_E, 1))

        # E labels of initial labelled data
        for i in self.C_A:
            labels[i] = 1
        for i in self.C_B:
            labels[i] = 0

        if not intersect(self.C_A, self.C_B):
            while len(self.F) > 0 and end == False:
                # print(len(self.F))
                next_point = self.F.pop()
                new_hull_C_A = ConvexHull(self.E[self.C_A], 1)
                new_hull_C_A.add_points([self.get_point(next_point)], 1)
                new_C_A, added = get_points_inside_convex_hull(self.E, new_hull_C_A, self.C_A, self.F + self.C_B,
                                                               next_point)
                oracle_calls += 1

                if not intersect(new_C_A, self.C_B):
                    self.hull_C_A = new_hull_C_A
                    self.C_A = new_C_A
                    self.F = list(set(self.F) - set(added))

                    for x in added:
                        labels[x] = 1
                        self.colors_E[x] = "orange"

                else:
                    new_hull_C_B = ConvexHull(self.E[self.C_B], 1)
                    new_hull_C_B.add_points([self.get_point(next_point)], 1)
                    new_C_B, added = get_points_inside_convex_hull(self.E, new_hull_C_B, self.C_B, self.F + self.C_A,
                                                                   next_point)
                    oracle_calls += 1

                    if not intersect(self.C_A, new_C_B):
                        self.hull_C_B = new_hull_C_B
                        self.C_B = new_C_B
                        self.F = list(set(self.F) - set(added))
                        for x in added:
                            labels[x] = 0
                            self.colors_E[x] = "violet"
        else:
            return [], [], False

        return oracle_calls, labels, True
Example #5
0
    def squash_down_to_convex_hull(self, M, sim_hull_pts=None):
        from scipy.spatial import ConvexHull

        ndim = M[0][1].shape[0]
        pts = np.empty((len(M) * (2 ** (ndim)), ndim))
        i = 0
        for (input_range, output_range) in M:
            for pt in product(*output_range):
                pts[i, :] = pt
                i += 1
        hull = ConvexHull(pts, incremental=True)
        if sim_hull_pts is not None:
            hull.add_points(sim_hull_pts)
        return hull
Example #6
0
def is_stable(sculpture: np.ndarray) -> bool:
    """Given a 'sculpted' NDarray, where number values represent densities and
    NaN values represent the areas carved away, determine if, in the orientation
    given, whether it will sit stably upon its base.

    :param sculpture: NDarray representing a sculpture of variable density material.
    """
    # TODO: Complete this function.
    # TODO: Add a few good, working Doctests

    ## 假設sculpture已經是ndarray (scuplture = np.array().reshape())
    ## if value is nAn -> needs to replace 0 so that center of mass works
    sculpture = carve_sculpture_from_density_block(shape_1, marble_block_1)
    sculpture = np.nan_to_num(sculpture, 0)  # convert nan to zero to make center_of_mass work
    center = center_of_mass(sculpture)
    if center > 2:
        center = center[1:]

    ## slice the bottom layer ( last item in the array)
    sculpture_base = sculpture[-1]  # 3d -> 2d -> result a sqare
    # nan arrays
    sculpture_base = np.argwhere(sculpture_base > 0) # return array of points
    # np.transpose(sculpture_base) (?)

    # use convexhull 看center of mass有沒有在base裡面
    # get ch1
    ch1 = ConvexHull(points=sculpture_base, incremental=True)
    # summarize_hull_properties ( ch1 )
    ch2 = ch1.add_points(center)
    # summarize_hull_properties ( ch1 )
    # compare ch1 and ch2, if ch1 == ch2 return True, else False
    if ch1 == ch2:
        return True
Example #7
0
def convex_limit(struct_points: np.ndarray) -> int:
    """Given points in order from best to worst,
    Returns the length of the maximum initial segment of points such that quasiconvexity is verified."""
    points: tp.List[float] = []
    d = len(struct_points[0])
    if len(struct_points) < 2 * d + 2:
        return len(struct_points) // 2
    for i in range(0, min(2 * d + 2, len(struct_points)), 2):
        points += [struct_points[i]]
    hull = ConvexHull(points, incremental=True)
    k = len(points)
    for i in range(d + 1, len(points)):
        hull.add_points(points[i:(i + 1)])
        if i not in hull.vertices:
            k = i - 1
            break
    return k
    def optimal_alg(self):
        time_point = time.time()
        oracle_calls = 0
        counter = 0
        labels = -1 * np.ones(shape=(self.size_E, 1))
        outside_points_1 = [x for x in self.convex_A_distances.keys()] + self.C_B
        outside_points_2 = [x for x in self.convex_B_distances.keys()] + self.C_A

        # add labels
        for i in self.C_A:
            labels[i] = 1
        for i in self.C_B:
            labels[i] = 0

        if not intersect(self.C_A, self.C_B):
            while (len(self.convex_A_distances) > 0 or len(self.convex_B_distances) > 0):
                # print(len(self.Set1Distances), len(self.Set2Distances))
                added = []

                # First set is nearer to nearest not classified point
                if self.decide_nearest():
                    time_point = time_step("Find Neighbour:", time_point)
                    if len(self.convex_A_distances) > 0:
                        next_point = self.convex_A_distances.popitem()[0]
                        new_hull_C_A = ConvexHull(self.E[self.C_A], 1)
                        new_hull_C_A.add_points([self.get_point(next_point)], 1)
                        time_point = time_step("Adding Convex Hull E 1:", time_point)
                        new_C_A, added = get_points_inside_convex_hull(self.E, new_hull_C_A, self.C_A,
                                                                       outside_points_1)
                        oracle_calls += 1
                        time_point = time_step("Getting inside E:", time_point)

                        # if there is no intersection the point can be added to the first convex set
                        if not intersect(new_C_A, self.C_B):
                            time_point = time_step("Intersection Test:", time_point)
                            self.C_A = new_C_A
                            self.hull_C_A = new_hull_C_A
                            for x in added:
                                # add to labels
                                labels[x] = 1
                                self.colors_E[x] = "orange"
                                if x in self.convex_A_distances.keys():
                                    del self.convex_A_distances[x]
                                if x in self.convex_B_distances.keys():
                                    del self.convex_B_distances[x]
                            outside_points_1 = list(set(outside_points_1) - set(added))
                            time_point = time_step("Update arrays:", time_point)


                        # if there is an intersection we have to check if it can be added to the second set
                        else:
                            # Test second half space
                            new_hull_C_B = ConvexHull(self.E[self.C_B], 1)
                            new_hull_C_B.add_points([self.get_point(next_point)], 1)
                            new_C_B, added = get_points_inside_convex_hull(self.E, new_hull_C_B, self.C_B,
                                                                           outside_points_2)
                            oracle_calls += 1

                            # the point can be added to the second set,
                            # if we reach this point the first time all the other E which are classified did not change the optimal margin
                            if not intersect(self.C_A, new_C_B):
                                self.C_B = new_C_B
                                self.hull_C_B = new_hull_C_B
                                for x in added:
                                    # add to labels
                                    labels[x] = 0
                                    self.colors_E[x] = "violet"
                                    if x in self.convex_A_distances.keys():
                                        del self.convex_A_distances[x]
                                    if x in self.convex_B_distances.keys():
                                        del self.convex_B_distances[x]
                                outside_points_2 = list(set(outside_points_2) - set(added))
                            # the point cannot be added to any set
                            else:
                                if next_point in outside_points_1:
                                    outside_points_1.remove(next_point)
                                if next_point in outside_points_2:
                                    outside_points_2.remove(next_point)

                    time_point = time_step("Point add Hull:", time_point)

                # Second set is nearer to nearest not classified point
                else:
                    time_point = time_step("Find Neighbour:", time_point)
                    if len(self.convex_B_distances) > 0:
                        next_point = self.convex_B_distances.popitem()[0]
                        new_hull_C_B = ConvexHull(self.E[self.C_B], 1)
                        new_hull_C_B.add_points([self.get_point(next_point)], 1)
                        new_C_B, added = get_points_inside_convex_hull(self.E, new_hull_C_B, self.C_B,
                                                                       outside_points_2)
                        oracle_calls += 1

                        # we can add the new point to the second, the nearer set
                        if not intersect(new_C_B, self.C_A):
                            self.C_B = new_C_B
                            self.hull_C_B = new_hull_C_B
                            for x in added:
                                # add to labels
                                labels[x] = 0
                                self.colors_E[x] = "violet"
                                if x in self.convex_A_distances.keys():
                                    del self.convex_A_distances[x]
                                if x in self.convex_B_distances.keys():
                                    del self.convex_B_distances[x]
                            outside_points_2 = list(set(outside_points_2) - set(added))

                        # we check if we can add the point to the first set
                        # if we reach this point the first time all the other E which are classified did not change the optimal margin
                        else:
                            # Test first half space
                            new_hull_C_A = ConvexHull(self.E[self.C_A], 1)
                            new_hull_C_A.add_points([self.get_point(next_point)], 1)
                            new_C_A, added = get_points_inside_convex_hull(self.E, new_hull_C_A, self.C_A,
                                                                           outside_points_1)
                            oracle_calls += 1

                            # the point can be classified to the second set
                            if not intersect(self.C_B, new_C_A):
                                self.hull_C_A = new_hull_C_A
                                self.C_A = new_C_A
                                for x in added:
                                    # add to labels
                                    labels[x] = 1
                                    self.colors_E[x] = "orange"
                                    if x in self.convex_A_distances.keys():
                                        del self.convex_A_distances[x]
                                    if x in self.convex_B_distances.keys():
                                        del self.convex_B_distances[x]
                                outside_points_1 = list(set(outside_points_1) - set(added))

                            # we cannot classify the point
                            else:
                                if next_point in outside_points_1:
                                    outside_points_1.remove(next_point)
                                if next_point in outside_points_2:
                                    outside_points_2.remove(next_point)

                time_point = time_step("Point add Hull:", time_point)
        else:
            return [], [], False

        return oracle_calls, labels, True
Example #9
0
    def r(self, c, gpos, mpos=0.0):
        """
        Calculates the virtual distances between grid point locations and
        microphone locations or the origin. These virtual distances correspond
        to travel times of the sound along a ray that is traced through the
        medium.

        Parameters
        ----------
        c : float
            The speed of sound to use for the calculation.
        gpos : array of floats of shape (3, N)
            The locations of points in the beamforming map grid in 3D cartesian
            co-ordinates.
        mpos : array of floats of shape (3, M), optional
            The locations of microphones in 3D cartesian co-ordinates. If not
            given, then only one microphone at the origin (0, 0, 0) is
            considered.

        Returns
        -------
        array of floats
            The distances in a twodimensional (N, M) array of floats. If M==1, 
            then only a onedimensional array is returned.
        """
        if isscalar(mpos):
            mpos = array((0, 0, 0), dtype=float32)[:, newaxis]

        # the DE system
        def f1(t, y, v):
            x = y[0:3]
            s = y[3:6]
            vv, dv = v(x)
            sa = sqrt(s[0] * s[0] + s[1] * s[1] + s[2] * s[2])
            x = empty(6)
            x[0:3] = c * s / sa - vv  # time reversal
            x[3:6] = dot(s, -dv.T)  # time reversal
            return x

        # integration along a single ray
        def fr(x0, n0, rmax, dt, v, xyz, t):
            s0 = n0 / (c + dot(v(x0)[0], n0))
            y0 = hstack((x0, s0))
            oo = ode(f1)
            oo.set_f_params(v)
            oo.set_integrator(
                'vode',
                rtol=1e-4,  # accuracy !
                max_step=1e-4 * rmax)  # for thin shear layer
            oo.set_initial_value(y0, 0)
            while oo.successful():
                xyz.append(oo.y[0:3])
                t.append(oo.t)
                if norm(oo.y[0:3] - x0) > rmax:
                    break
                oo.integrate(oo.t + dt)

        gs2 = gpos.shape[-1]
        gt = empty((gs2, mpos.shape[-1]))
        vv = self.ff.v
        NN = int(sqrt(self.N))
        for micnum, x0 in enumerate(mpos.T):
            xe = gpos.mean(1)  # center of grid
            r = x0[:, newaxis] - gpos
            rmax = sqrt((r * r).sum(0).max())  # maximum distance
            nv = spiral_sphere(self.N, self.Om, b=xe - x0)
            rstep = rmax / sqrt(self.N)
            rmax += rstep
            tstep = rstep / c
            xyz = []
            t = []
            lastind = 0
            for i, n0 in enumerate(nv.T):
                fr(x0, n0, rmax, tstep, vv, xyz, t)
                if i and i % NN == 0:
                    if not lastind:
                        dd = ConvexHull(vstack((gpos.T, xyz)),
                                        incremental=True)
                    else:
                        dd.add_points(xyz[lastind:], restart=True)
                    lastind = len(xyz)
                    # ConvexHull includes grid if no grid points on hull
                    if dd.simplices.min() >= gs2:
                        break
            xyz = array(xyz)
            t = array(t)
            li = LinearNDInterpolator(xyz, t)
            gt[:, micnum] = li(gpos.T)
        if gt.shape[1] == 1:
            gt = gt[:, 0]
        return c * gt  #return distance along ray
Example #10
0
class GoalBabbling():
    def __init__(self):

        # this simulates cameras and positions
        self.cam_sim = Cam_sim("../../rgb_rectified")

        self.lock = threading.Lock()
        signal.signal(signal.SIGINT, self.Exit_call)

    def initialise(self,
                   goal_size=3,
                   image_size=64,
                   channels=1,
                   batch_size=16,
                   goal_selection_mode='db',
                   exp_iteration=0,
                   hist_size=35,
                   prob_update=0.1):

        self.exp_iteration = exp_iteration

        self.goal_size = goal_size
        self.image_size = image_size
        self.channels = channels
        self.samples = []

        self.code_size = 32

        self.iteration = 0
        self.test_data_step = 500
        self.test_size = 50
        self.max_iterations = 5000

        self.history_size = hist_size  # how many batches (*batch_size) are kept?
        self.history_buffer_update_prob = prob_update  # what is the probabilty that a data element in the history buffer is substituted with a new one

        self.pos = []
        self.cmd = []
        self.img = []
        self.history_pos = []  # kept to prevent catastrophic forgetting
        self.history_img = []  # kept to prevent catastrophic forgetting

        self.goal_code = []
        self.mse_inv = []
        self.mse_fwd = []
        self.mse_inv_goal = []  # on goals only
        self.mse_fwd_goal = []  # on goals only

        self.samples_pos = []
        self.samples_img = []
        self.samples_codes = []
        self.test_positions = []

        self.goal_selection_mode = goal_selection_mode  # 'som' or 'kmeans'

        self.move = False

        self.autoencoder, self.encoder, self.decoder = None, None, None
        #self.forward_model = None
        self.forward_code_model = None
        #self.inverse_model = None
        self.inverse_code_model = None
        self.goal_som = None
        self.interest_model = InterestModel(self.goal_size)
        self.current_goal_x = -1
        self.current_goal_y = -1
        self.current_goal_idx = -1
        self.cae_epochs = 1
        self.inv_epochs = 2
        self.fwd_epochs = 2
        self.random_cmd_flag = False
        self.random_cmd_rate = 0.30
        #		self.goal_db = ['./sample_images/img_0.jpg','./sample_images/img_200.jpg','./sample_images/img_400.jpg','./sample_images/img_600.jpg','./sample_images/img_800.jpg','./sample_images/img_1000.jpg','./sample_images/img_1200.jpg','./sample_images/img_1400.jpg','./sample_images/img_1600.jpg' ]
        self.goal_image = np.zeros(
            (1, self.image_size, self.image_size, channels), np.float32)
        self.batch_size = batch_size

        self.count = 1

        self.log_lp = []  # learning progress for each goal
        #self.log_lr_inv = [] # learning rate inverse model
        self.log_goal_pos = []  # ccurrent goal position (x,y xcarve position)
        for i in range(self.goal_size * self.goal_size):
            self.log_goal_pos.append([])
        self.log_goal_pred = [
        ]  # ccurrent xcarve position (x,y xcarve position)
        for i in range(self.goal_size * self.goal_size):
            self.log_goal_pred.append([])

        self.log_goal_img = []  # ccurrent goal position (x,y xcarve position)
        for i in range(self.goal_size * self.goal_size):
            self.log_goal_img.append([])

        self.log_curr_img = [
        ]  # ccurrent xcarve position (x,y xcarve position)
        for i in range(self.goal_size * self.goal_size):
            self.log_curr_img.append([])

        self.log_goal_id = []
        self.log_cvh_inv = []

        dataset = '../../rgb_rectified/compressed_dataset.pkl'
        print('Loading test dataset ', dataset)
        self.train_images, self.test_images, self.train_cmds, self.test_cmds, self.train_pos, self.test_pos = models.load_data(
            dataset, self.image_size, step=self.test_data_step)
        # load models
        self.autoencoder, self.encoder, self.decoder = models.load_autoencoder(
            directory='./models/',
            code_size=self.code_size,
            image_size=self.image_size,
            batch_size=self.batch_size,
            epochs=self.cae_epochs)
        #		self.forward_model = models.load_forward_model(train=False)
        self.forward_code_model = models.load_forward_code_model(
            directory='./models/', code_size=self.code_size, train=False)
        #self.inverse_model = models.load_inverse_model(train=False)
        self.inverse_code_model = models.load_inverse_code_model(
            directory='./models/', code_size=self.code_size, train=False)
        if self.goal_selection_mode == 'kmeans':
            self.kmeans = models.load_kmeans()
        if self.goal_selection_mode == 'som':
            self.goal_som = models.load_som(directory='./models/',
                                            encoder=self.encoder,
                                            goal_size=self.goal_size)

        # initialise convex hulls (debug stuff)
        np.random.seed(
            10)  # generate always the same random input to the convex hull
        pt_inv = []
        for i in range(3):
            a = np.random.rand(2) * 0.01
            pt_inv.append(a)

        np.random.seed()  # change the seed

        self.convex_hull_inv = ConvexHull(np.asarray(pt_inv), incremental=True)

        p = Position()
        p.x = int(0)
        p.y = int(0)
        p.z = int(-90)
        p.speed = int(1800)

        self.prev_pos = p
        #self.run_babbling()

    def get_current_inv_mse(self):
        #motor_pred = self.inverse_model.predict(self.test_images[0:self.test_size])# (self.goal_size*self.goal_size)])
        img_codes = self.encoder.predict(self.test_images[0:self.test_size])
        motor_pred = self.inverse_code_model.predict(
            img_codes)  # (self.goal_size*self.goal_size)])
        #motor_pred = self.inverse_model.predict(self.test_images[0:self.test_size])# (self.goal_size*self.goal_size)])
        #		mse = (np.linalg.norm(motor_pred-self.test_pos[0:(self.goal_size*self.goal_size)]) ** 2) / (self.goal_size*self.goal_size)
        mse = (np.linalg.norm(motor_pred - self.test_pos[0:self.test_size])**
               2) / self.test_size
        print('Current mse inverse code model: ', mse)
        self.mse_inv.append(mse)


#	def get_current_inv_mse_on_goals(self):
#		mse = 0
#		if self.goal_selection_mode =='db' or self.goal_selection_mode =='random':
#			img_codes = self.encoder.predict(self.test_images[0:(self.goal_size*self.goal_size)])
#			motor_pred = self.inverse_code_model.predict(img_codes)
#			mse = (np.linalg.norm(motor_pred-self.test_pos[0:(self.goal_size*self.goal_size)]) ** 2) / (self.goal_size*self.goal_size)
#		print 'Current mse inverse code model on goals: ', mse
#		self.mse_inv_goal.append(mse)

    def get_current_fwd_mse(self):
        #img_pred = self.forward_model.predict(self.test_pos)
        #img_pred = self.decoder.predict(self.forward_code_model.predict(self.test_pos))
        #img_obs_code = self.encoder.predict(self.test_images[0:(self.goal_size*self.goal_size)])
        img_obs_code = self.encoder.predict(self.test_images[0:self.test_size])
        #img_pred_code = self.encoder.predict(img_pred)
        #		img_pred_code = self.forward_code_model.predict(self.test_pos[0:(self.goal_size*self.goal_size)])
        img_pred_code = self.forward_code_model.predict(
            self.test_pos[0:self.test_size])
        #mse = (np.linalg.norm(img_pred_code-img_obs_code) ** 2) /  (self.goal_size*self.goal_size)
        mse = (np.linalg.norm(img_pred_code - img_obs_code)**
               2) / self.test_size
        print('Current mse fwd model: ', mse)
        self.mse_fwd.append(mse)

    def run_babbling(self):
        p = Position()

        for _ in range(self.max_iterations):
            #test_motor_pred = self.inverse_model.predict([self.test_images[0:self.goal_size*self.goal_size]])
            #print np.hstack((test_motor_pred,self.test_pos[0:self.goal_size*self.goal_size]))

            # record logs and data
            self.get_current_inv_mse()
            self.get_current_fwd_mse()
            #self.log_lr_inv.append(K.eval(self.inverse_model.optimizer.lr))
            #print 'iteration opt inv', K.eval(self.inverse_model.optimizer.iteration)
            #print 'current lr: ', self.log_lr_inv[-1]

            print('Mode ', self.goal_selection_mode, ' hist_size ',
                  str(self.history_size), ' prob ',
                  str(self.history_buffer_update_prob), ' iteration : ',
                  self.iteration)
            self.iteration = self.iteration + 1
            #			if self.iteration > self.max_iterations:
            #				self.save_models()
            #				return

            # select a goal
            self.current_goal_idx, self.current_goal_x, self.current_goal_y = self.interest_model.select_goal(
            )
            if self.goal_selection_mode == 'kmeans':
                self.goal_code = self.kmeans.cluster_centers_[
                    self.current_goal_idx].reshape(1, self.code_size)
                print(' code ', self.goal_code)
            elif self.goal_selection_mode == 'db' or self.goal_selection_mode == 'random':
                #self.goal_image=np.asarray(cv2.imread(self.goal_db[self.current_goal_idx], 0)).reshape(1, self.image_size, self.image_size, self.channels).astype('float32') / 255
                self.goal_image = self.test_images[
                    self.current_goal_idx].reshape(1, self.image_size,
                                                   self.image_size,
                                                   self.channels)
                self.goal_code = self.encoder.predict(self.goal_image)

            elif self.goal_selection_mode == 'som':
                self.goal_code = self.goal_som._weights[
                    self.current_goal_x,
                    self.current_goal_y].reshape(1, self.code_size)

            else:
                print('wrong goal selection mode, exit!')
                sys.exit(1)

            motor_pred = []
            if self.goal_selection_mode == 'db':
                #cv2.imshow("Goal", cv2.imread(self.goal_db[self.current_goal_idx], 0))
                ####cv2.imshow("Goal", self.test_images[self.current_goal_idx])
                ####cv2.waitKey(1)
                #motor_pred = self.inverse_model.predict(self.goal_image)
                motor_pred = self.inverse_code_model.predict(self.goal_code)
                print('pred ', motor_pred, ' real ',
                      self.test_pos[self.current_goal_idx])
            else:
                goal_decoded = self.decoder.predict(self.goal_code)
                ####cv2.imshow("CAE Decoded Goal", goal_decoded[0])
                ####cv2.waitKey(1)
                #				motor_pred = self.inverse_model.predict(goal_decoded)
                motor_pred = self.inverse_code_model.predict(self.goal_code)
            #image_pred = self.forward_model.predict(np.asarray(motor_pred))
            image_pred = self.decoder.predict(
                self.forward_code_model.predict(np.asarray(motor_pred)))
            ####cv2.imshow("FWD Model Predicted Image", image_pred[0])
            ####cv2.waitKey(1)

            #image_pred_curr = forward_model.predict(np.asarray(prev_cmd))
            #cv2.imshow("FWD Model Predicted Image curr ", image_pred_curr[0])
            #cv2.waitKey(1)
            #			motor_cmd=motor_pred[0]

            #			p.x = clamp_x(motor_pred[0][0]*x_lims[1])
            #			p.y = clamp_x(motor_pred[0][1]*y_lims[1])
            noise_x = np.random.normal(0, 0.02)
            noise_y = np.random.normal(0, 0.02)
            p.x = clamp_x((motor_pred[0][0] + noise_x) * x_lims[1])
            p.y = clamp_y((motor_pred[0][1] + noise_y) * y_lims[1])

            ran = random.random()
            if ran < self.random_cmd_rate or (
                    len(self.history_pos) / self.batch_size
            ) != self.history_size or self.goal_selection_mode == 'random':  # choose random motor commands from time to time
                print('generating random motor command')
                p.x = random.uniform(x_lims[0], x_lims[1])
                p.y = random.uniform(y_lims[0], y_lims[1])
                self.random_cmd_flag = True
            else:
                self.random_cmd_flag = False

            print('predicted position ', motor_pred[0], 'p+noise ',
                  motor_pred[0][0] + noise_x, ' ', motor_pred[0][1] + noise_y,
                  ' clamped ', p.x, ' ', p.y, ' noise.x ', noise_x, ' n.y ',
                  noise_y)

            p.z = int(-90)
            p.speed = int(1400)

            self.create_simulated_data(p, self.prev_pos)
            self.prev_pos = p

            if self.iteration % 50 == 0:
                #test_codes= self.encoder.predict(self.test_images[0:self.goal_size*self.goal_size].reshape(self.goal_size*self.goal_size, self.image_size, self.image_size, self.channels))
                if self.goal_selection_mode == 'db' or self.goal_selection_mode == 'random':
                    plot_cvh(self.convex_hull_inv,
                             title=self.goal_selection_mode + '_' +
                             str(self.exp_iteration) + 'cvh_inv',
                             iteration=self.iteration,
                             dimensions=2,
                             log_goal=self.test_pos[0:self.goal_size *
                                                    self.goal_size],
                             num_goals=self.goal_size * self.goal_size)
                elif self.goal_selection_mode == 'kmeans':
                    goals_pos = self.inverse_code_model.predict(
                        self.kmeans.cluster_centers_[0:self.goal_size *
                                                     self.goal_size].reshape(
                                                         self.goal_size *
                                                         self.goal_size,
                                                         self.code_size))
                    plot_cvh(self.convex_hull_inv,
                             title=self.goal_selection_mode + '_' +
                             str(self.exp_iteration) + 'cvh_inv',
                             iteration=self.iteration,
                             dimensions=2,
                             log_goal=goals_pos,
                             num_goals=self.goal_size * self.goal_size)
                elif self.goal_selection_mode == 'som':
                    goals_pos = self.inverse_code_model.predict(
                        self.goal_som._weights.reshape(
                            len(self.goal_som._weights) *
                            len(self.goal_som._weights[0]),
                            len(self.goal_som._weights[0][0])))
                    plot_cvh(self.convex_hull_inv,
                             title=self.goal_selection_mode + '_' +
                             str(self.exp_iteration) + 'cvh_inv',
                             iteration=self.iteration,
                             dimensions=2,
                             log_goal=goals_pos,
                             num_goals=self.goal_size * self.goal_size)
                #test_codes_ipca = self.fwd_ipca.fit_transform(test_codes)
                #plot_cvh(self.convex_hull_fwd, title=self.goal_selection_mode+'_'+str(self.exp_iteration)+'cvh_fwd', iteration = self.iteration, dimensions=2, log_goal=test_codes_ipca, num_goals=self.goal_size*self.goal_size)

            observation = self.img[-1]
            ####cv2.imshow("Current observation", observation)
            # update competence of the current goal (it is supposed that at this moment the action is finished
            if len(self.img) > 0 and (not self.random_cmd_flag
                                      or self.goal_selection_mode == 'random'):

                #observation_code = self.encoder.predict(observation.reshape(1, self.image_size, self.image_size, self.channels))
                #prediction_error = np.linalg.norm(np.asarray(self.goal_code[:])-np.asarray(observation_code[:]))
                cmd = [p.x / float(x_lims[1]), p.y / float(y_lims[1])]
                prediction_code = self.forward_code_model.predict(
                    np.asarray(cmd).reshape((1, 2)))

                prediction_error = np.linalg.norm(
                    np.asarray(self.goal_code[:]) -
                    np.asarray(prediction_code[:]))
                self.interest_model.update_competences(self.current_goal_x,
                                                       self.current_goal_y,
                                                       prediction_error)
                #print 'Prediction error: ', prediction_error, ' learning progress: ', self.interest_model.get_learning_progress(self.current_goal_x, self.current_goal_y)

                self.log_lp.append(
                    np.asarray(deepcopy(
                        self.interest_model.learning_progress)))
                self.log_goal_id.append(self.current_goal_idx)

            #print self.log_lp
            # fit models
            if len(self.img) > self.batch_size:
                if len(self.img) == len(self.pos):
                    # first fill the history buffer, then update the models
                    if (len(self.history_pos) /
                            self.batch_size) != self.history_size:
                        if len(self.history_pos) == 0:
                            self.history_pos = deepcopy(
                                self.pos[-(self.batch_size):])
                            self.history_img = deepcopy(
                                self.img[-(self.batch_size):])
                        self.history_pos = np.vstack(
                            (self.history_pos, self.pos[-(self.batch_size):]))
                        self.history_img = np.vstack(
                            (self.history_img, self.img[-(self.batch_size):]))

                    else:
                        img_io = []
                        pos_io = []
                        if (self.history_size != 0):
                            for i in range(-(self.batch_size), 0):
                                #print i
                                r = random.random()
                                if r < self.history_buffer_update_prob:
                                    r_i = random.randint(
                                        0,
                                        self.history_size * self.batch_size -
                                        1)
                                    #print 'r_i ', r_i, ' i ', i
                                    self.history_pos[r_i] = deepcopy(
                                        self.pos[i])
                                    self.history_img[r_i] = deepcopy(
                                        self.img[i])

                            # update models
                            img_io = np.vstack(
                                (np.asarray(self.history_img[:]).reshape(
                                    len(self.history_img), self.image_size,
                                    self.image_size, self.channels),
                                 np.asarray(
                                     self.img[-(self.batch_size):]).reshape(
                                         self.batch_size, self.image_size,
                                         self.image_size, self.channels)))
                            pos_io = np.vstack(
                                (np.asarray(self.history_pos[:]),
                                 np.asarray(self.pos[-(self.batch_size):])))
                        else:
                            img_io = np.asarray(
                                self.img[-(self.batch_size):]).reshape(
                                    self.batch_size, self.image_size,
                                    self.image_size, self.channels)
                            pos_io = np.asarray(self.pos[-(self.batch_size):])
                        #models.train_forward_model_on_batch(self.forward_model, pos_io, img_io, batch_size=self.batch_size, epochs = self.epochs)
                        codes_io = self.encoder.predict(img_io)
                        models.train_forward_code_model_on_batch(
                            self.forward_code_model,
                            pos_io,
                            codes_io,
                            batch_size=self.batch_size,
                            epochs=self.fwd_epochs)
                        #models.train_inverse_model_on_batch(self.inverse_model, img_io, pos_io, batch_size=self.batch_size, epochs = self.inv_epochs)
                        models.train_inverse_code_model_on_batch(
                            self.inverse_code_model,
                            codes_io,
                            pos_io,
                            batch_size=self.batch_size,
                            epochs=self.inv_epochs)

                        #train_autoencoder_on_batch(self.autoencoder, self.encoder, self.decoder, np.asarray(self.img[-32:]).reshape(32, self.image_size, self.image_size, self.channels), batch_size=self.batch_size, cae_epochs=5)

                    # update convex hulls
                    obs_codes = self.encoder.predict(
                        np.asarray(self.img[-(self.batch_size):]).reshape(
                            self.batch_size, self.image_size, self.image_size,
                            self.channels))

                    #print self.pos[-32:]
                    #print np.asarray(self.pos[-32:]).reshape((32,2))
                    self.convex_hull_inv.add_points(np.asarray(
                        self.pos[-(self.batch_size):]).reshape(
                            ((self.batch_size), 2)),
                                                    restart=True)
                    self.log_cvh_inv.append(self.convex_hull_inv.volume)
                    #print 'conv hull inv. volume: ', self.convex_hull_inv.volume
                    #print 'a', obs_codes[:]
                    #self.fwd_ipca.partial_fit(obs_codes[:])
                    #ipca_codes = self.fwd_ipca.transform(obs_codes[:])
                    #print 'p', ipca_codes
                    #print 'p', ipca_codes[:]
                    #self.convex_hull_fwd.add_points(np.asarray(obs_codes[:]), restart=False)
                    # this may be inconsistent. IPCA could change over time, so previous points projected for the CVH calculation could change. Check this
                    #self.convex_hull_fwd.add_points(np.asarray(ipca_codes[:]), restart=True)
                    #print 'conv hull fwd. volume: ', self.convex_hull_fwd.volume
                    #self.log_cvh_fwd.append(self.convex_hull_fwd.volume)

                else:
                    print('lenghts not equal')

            if not self.random_cmd_flag and len(self.cmd) > 0:
                #print 'test pos', self.test_positions[0:10]
                test_p = self.test_pos[self.current_goal_idx]
                #curr_cmd = self.cmd[-1]
                #pred_p = self.inverse_model.predict(self.goal_image)
                pred_p = self.inverse_code_model.predict(self.goal_code)
                self.log_goal_pos[self.current_goal_idx].append(
                    [test_p[0], test_p[1]])
                #self.log_curr_pos[self.current_goal_idx].append([curr_cmd[0], curr_cmd[1] ])
                self.log_goal_pred[self.current_goal_idx].append(
                    [pred_p[0][0], pred_p[0][1]])
                #print 'hg ', self.log_goal_pos
                #g_code = self.forward_model.predict(np.asarray(test_p).reshape((1,2)))
                #g_code = self.decoder.predict(self.forward_code_model.predict(np.asarray(test_p).reshape((1,2))))
                #g_code = self.fwd_ipca.transform(self.forward_code_model.predict(np.asarray(test_p).reshape((1,2))) )
                #c_code = self.forward_model.predict(np.asarray(curr_cmd).reshape((1,2)))
                #c_code = self.decoder.predict(self.forward_code_model.predict(np.asarray(curr_cmd).reshape((1,2))) )
                #c_code = self.fwd_ipca.transform(self.forward_code_model.predict(np.asarray(curr_cmd).reshape((1,2))) )
                #print 'g ipca ', g_code
                #print 'c ipca ', c_code
                #self.log_goal_img[self.current_goal_idx].append([g_code[0][0],g_code[0][1], g_code[0][2],g_code[0][3] ])
                #self.log_curr_img[self.current_goal_idx].append([c_code[0][0],c_code[0][1], c_code[0][2],c_code[0][3] ])
                #self.log_goal_img[self.current_goal_idx].append([g_code[0][0],g_code[0][1] ])
                #self.log_curr_img[self.current_goal_idx].append([c_code[0][0],c_code[0][1] ])

        print('Saving models')
        self.save_models()

    def create_simulated_data(self, cmd, pos):
        self.lock.acquire()
        a = [int(pos.x), int(pos.y)]
        b = [int(cmd.x), int(cmd.y)]

        #		b[0] = int(cmd.x)
        #       b[1] = int(cmd.y)
        tr = self.cam_sim.get_trajectory(a, b)
        trn = self.cam_sim.get_trajectory_names(a, b)
        #a[0] = b[0]
        #a[1] = b[1]

        rounded = self.cam_sim.round2mul(tr, 5)  # only images every 5mm
        #		print 'trajectory '
        for i in range(len(tr)):
            #			print 'curr_dir ' , os.getcwd()
            #			print(tr[i],rounded[i],trn[i])
            self.pos.append([
                float(rounded[i][0]) / x_lims[1],
                float(rounded[i][1]) / y_lims[1]
            ])
            self.cmd.append(
                [float(int(cmd.x)) / x_lims[1],
                 float(int(cmd.y)) / y_lims[1]])
            cv2_img = cv2.imread(trn[i], 1)
            if self.channels == 1:
                cv2_img = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2GRAY)
            cv2_img = cv2.resize(cv2_img, (self.image_size, self.image_size),
                                 interpolation=cv2.INTER_LINEAR)
            cv2_img = cv2_img.astype('float32') / 255
            cv2_img.reshape(1, self.image_size, self.image_size, self.channels)
            self.img.append(cv2_img)

        self.lock.release()

    def ats_callback(self, image, cmd, pos):
        self.lock.acquire()
        self.msg_cmd = cmd
        self.msg_pos = pos
        self.msg_image = image

        self.pos.append([
            float(self.msg_pos.x) / x_lims[1],
            float(self.msg_pos.y) / y_lims[1]
        ])

        self.cmd.append([
            float(self.msg_cmd.x) / x_lims[1],
            float(self.msg_cmd.y) / y_lims[1]
        ])
        self.samples.append({
            'image': self.msg_image,
            'position': self.msg_pos,
            'command': self.msg_cmd
        })

        cv2_img = bridge.imgmsg_to_cv2(self.msg_image, "bgr8")
        if self.channels == 1:
            cv2_img = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2GRAY)
        cv2_img = cv2_img.astype('float32') / 255
        cv2_img.reshape(1, self.image_size, self.image_size, self.channels)
        self.img.append(cv2_img)

        #img_list = []
        #for i in range(self.batch_size):
        #	img_list.append(self.img[-1].reshape(self.image_size, self.image_size, self.channels))
        #print np.asarray(self.img[-1]).reshape(1, self.image_size, self.image_size, self.channels).shape
        #img_code = self.encoder.predict(np.asarray(self.img[-1]).reshape(1, self.image_size, self.image_size, self.channels)	)
        #print np.asarray(img_list).shape
        #img_code = self.encoder.predict(np.asarray(img_list))
        #error = np.fabs(np.linalg.norm(np.asarray(self.goal_code)-np.asarray(img_code[0])))
        #self.log_distance_to_goal.append(error)
        #error=0
        #print 'error ', error
        self.lock.release()

    #		if len(images) ==batches+1:
    #			print 'Updating fwd model'
    #print np.asarray(prev_poss).shape, " ", np.asarray(prev_cmds).shape

    #			train_forward_model_on_batch(forward_model, np.hstack((np.asarray(prev_pos), np.asarray(prev_cmd))) , np.asarray(images), batchttps://stackoverflow.com/questions/961632/converting-integer-to-string-in-pythonh_size=10)

    #			print 'Updating inv model'
    #			train_inverse_model_on_batch(inverse_model, encoder, np.asarray(prev_pos), np.asarray(images), np.asarray(prev_cmd), batch_size=10)
    #			prev_pos =[]#
    #			prev_cmd =[]
    #			images = []
    #current_pos = []

    def save_models(self):

        timestamp = datetime.datetime.now().strftime('%Y-%m-%d_%X')
        name = 'goal_babbling'
        filename = './data/' + name + '_' + timestamp + '.pkl'
        #with gzip.open(filename, 'wb') as file:
        #	cPickle.dump(self.samples, file, protocol=2)
        ####cv2.destroyAllWindows()
        #print 'Dataset saved to ', filename

        self.autoencoder.save('./models/autoencoder.h5', overwrite=True)
        self.encoder.save('./models/encoder.h5', overwrite=True)
        self.decoder.save('./models/decoder.h5', overwrite=True)
        #		self.inverse_model.save('./models/inverse_model.h5')
        self.inverse_code_model.save('./models/inverse_code_model.h5',
                                     overwrite=True)
        #self.forward_model.save('./models/gb/forward_model.h5')
        self.forward_code_model.save('./models/forward_code_model.h5',
                                     overwrite=True)

        #trained_som_weights = self.goal_som.get_weights().copy()
        #som_file = h5py.File('./models/goal_som.h5', 'w')
        #som_file.create_dataset('goal_som', data=trained_som_weights)
        #som_file.close()

        #np.savetxt('./models/log_goal_pos.txt', self.log_goal_pos)
        pickle.dump(self.log_goal_pos, open('./models/log_goal_pos.txt', 'wb'))
        #go_file = open('./models/log_goal_pos.txt','w')
        #go_file.write(str(self.log_goal_pos))
        #go_file.close()

        #np.savetxt('./models/log_curr_pos.txt', self.log_curr_pos)
        #		cPickle.dump(self.log_curr_pos, open('./models/log_curr_pos.txt', 'wb'))
        pickle.dump(self.log_goal_pred, open('./models/log_goal_pred.txt',
                                             'wb'))
        #cu_file = open('./models/log_curr_pos.txt','w')
        #cu_file.write(str(self.log_curr_pos))
        #cu_file.close()

        np.savetxt('./models/log_learning_progress.txt', self.log_lp)
        #lp_file = open('./models/log_learning_progress.txt','w')
        #lp_file.write(str(self.log_lp))
        #lp_file.close()

        np.savetxt('./models/log_goal_id.txt', self.log_goal_id)
        #gi_file = open('./models/log_goal_id.txt','w')
        #gi_file.write(str(self.log_goal_id))
        #gi_file.close()

        #np.savetxt('./models/log_distance_to_goal.txt', self.log_distance_to_goal)
        #dg_file = open('./models/log_distance_to_goal.txt','w')
        #dg_file.write(str(self.log_distance_to_goal))
        #dg_file.close()

        np.savetxt('./models/log_mse_inv.txt', self.mse_inv)
        plot_simple(self.mse_inv, 'MSE INV', save=True, show=False)

        np.savetxt('./models/log_mse_fwd.txt', self.mse_fwd)
        plot_simple(self.mse_fwd, 'MSE FWD', save=True, show=False)

        np.savetxt('./models/log_cvh_inv.txt', self.log_cvh_inv)
        plot_simple(self.log_cvh_inv, 'CVH Inv Volume', save=True, show=False)

        #np.savetxt('./models/log_cvh_fwd.txt', self.log_cvh_fwd)
        #plot_simple(self.log_cvh_fwd, 'CVH Fwd Volume', save = True, show = False)

        #np.savetxt('./models/log_lr_inv.txt', self.log_lr_inv)
        #plot_simple(self.log_lr_inv, 'LR_inv')

        plot_learning_progress(self.log_lp,
                               self.log_goal_id,
                               num_goals=self.goal_size * self.goal_size,
                               save=True,
                               show=False)
        #		plot_log_goal_inv(self.log_goal_pos, self.log_curr_pos, num_goals = self.goal_size*self.goal_size, save = True, show = False)
        plot_log_goal_inv(self.log_goal_pos,
                          self.log_goal_pred,
                          num_goals=self.goal_size * self.goal_size,
                          save=True,
                          show=False)
        #plot_log_goal_fwd(self.log_goal_img, self.log_curr_img, num_goals = self.goal_size*self.goal_size, save = True, show = False)

        self.clear_session()

        print('Models saved')
        self.goto_starting_pos()

    def clear_session(self):
        print('Clearning variables')
        #del self.log_goal_pred
        #del self.log_curr_img
        #del self.log_cvh_inv
        #del self.log_goal_id
        #del self.log_goal_img
        #del self.log_goal_pos
        #del self.log_lp
        #del self.samples
        #del self.samples_img
        #del self.samples_codes
        #del self.samples_pos
        #del self.autoencoder
        #del self.encoder
        #del self.decoder
        #del self.inverse_code_model
        #del self.forward_code_model
        #del self.mse_inv
        #del self.mse_fwd
        #del self.history_img
        #del self.history_pos

        # reset
        print('Clearing TF session')
        if tf.__version__ < "1.8.0":
            tf.reset_default_graph()
        else:
            tf.compat.v1.reset_default_graph()

    def Exit_call(self, signal, frame):
        self.goto_starting_pos()
        self.save_models()

    def goto_starting_pos(self):
        p = Position()
        p.x = int(0)
        p.y = int(0)
        p.z = int(-50)
        p.speed = int(1400)

        self.create_simulated_data(p, self.prev_pos)
        self.prev_pos = p
Example #11
0
class atari_env(object):  # to change env: extra punishment
    def __init__(self, args, rank=0, save_img=False):

        self.total_step = 0
        self.rank = rank
        self.img = []
        self.save_img = save_img
        self.args = args
        self.grid = 3
        self.env_path = '../env/AnimalAI'
        self.worker_id = random.randint(1, 100)
        self.arena_config_in = ArenaConfig('configs/3-Obstacles_my.yaml')
        self.base_dir = 'models/dopamine'
        self.gin_files = ['configs/rainbow.gin']
        self.env = self.create_env_fn()
        self.previous_velocity = [1, 1]
        self.observation_space = self.env.observation_space
        self.theta = 3.1415926 / 2
        self.pi = 3.1415926
        self.rotation = 40
        self.convex_hull = None
        self.points = [np.array([0, 0])]
        self.point = np.array([0, 0])
        self.turned = 0
        self.velocity = {
            0: [1, 0],
            1: [-1, 0],
            2: [0, 1],
            3: [0, -1],
            4: [0.7, 0.7],
            5: [-0.7, 0.7],
            6: [-0.7, -0.7],
            7: [0.7, -0.7],
        }

    def create_env_fn(self):
        env = AnimalAIEnv(environment_filename=self.env_path,
                          worker_id=self.worker_id,
                          n_arenas=1,
                          arenas_configurations=self.arena_config_in,
                          docker_training=False,
                          retro=False,
                          inference=self.args['inference'],
                          seed=self.rank,
                          resolution=84)
        return env

    def update(self, theta, angle):
        theta = theta + angle
        if theta > self.pi:
            theta = theta - self.pi
        if theta < -self.pi:
            theta = theta + self.pi
        return theta

    def transform_action(self, action):
        action = action[0][0]
        if action == 0:
            return [1, 0]
        elif action == 1:
            return [0, 1]
        else:
            return [0, 2]

    def step(self, action):

        # turn to the right direction
        action = self.transform_action(action)
        state, reward, done, info = self.env.step(action)
        instrinsic_reward = 0
        if reward < 0 and reward > -0.1:
            reward = 0
        self.total_step = self.total_step + 1
        if self.total_step % 500 == 0:
            print("reward, action, done, total_step, thread is ", reward,
                  action, done, self.total_step, self.rank)
        if np.linalg.norm(np.array(
                state[1])) < 1 and args['stationary_punish']:
            instrinsic_reward = -0.1
        if reward > 0:
            reward = 4

        if self.args['convex_hull']:
            direction = np.array([math.cos(self.theta), math.sin(self.theta)])
            velocity = direction * np.linalg.norm(np.array(state[1]))
            if np.linalg.norm(velocity) > 0.1:
                self.point = self.point + velocity
                self.points.append(self.point)
                if len(self.points
                       ) > 10 and self.turned > 3 and self.convex_hull is None:
                    self.convex_hull = ConvexHull(np.array(self.points),
                                                  incremental=True)
                elif self.convex_hull is not None:
                    previous_area = self.convex_hull.area
                    self.convex_hull.add_points(np.array([self.point]))
                    new_area = self.convex_hull.area
                    convex_reward = max(new_area - previous_area, 0)
                    convex_reward = math.sqrt(convex_reward)  # [0,4]
                    instrinsic_reward = instrinsic_reward + convex_reward * self.args[
                        'ir_weight']
                    #reward = reward + instrinsic_reward * self.args['ir_weight']

            if action[1] == 1:
                self.theta = self.update(self.theta, -6 / 180 * self.pi)
                self.turned = self.turned + 1
            if action[1] == 2:
                self.theta = self.update(self.theta, 6 / 180 * self.pi)
                self.turned = self.turned + 1

        state = state[0]
        state = state.transpose(2, 0, 1)
        with torch.no_grad():
            state = torch.tensor(state).unsqueeze(0).float().to(device)

        return state, (reward, instrinsic_reward), done, info

    def reset(self):
        if self.args['convex_hull']:
            self.theta = 3.1415926 / 2
            self.convex_hull = None
            self.points = [np.array([0, 0])]
            self.point = np.array([0, 0])
        state = self.env.reset()[0]
        self.turned = 0
        state = state.transpose(2, 0, 1)

        with torch.no_grad():
            state = torch.tensor(state).unsqueeze(0).float().to(device)
        return state

    def render(self):
        self.env.render()

    def seed(self, seed):

        self.env.seed(seed)

    def close(self):
        self.env.close()
                    str(len(binPnts[-1])) +
                    ' points found within the max threshold distance of a ligand atom\n'
                )
                # print('From ' + str(len(nonBuriedPnts)) + ' points, ' + str(len(binPnts[-1])) + ' points found within the threshold distance of a ligand atom\n')

                # Exclude points outside of convex hull
                initHull = ConvexHull(proCoords)
                hullVerts = initHull.points[initHull.vertices]
                hull = ConvexHull(points=hullVerts, incremental=True)
                inHullPnts = []
                for p in binPnts[
                        -1]:  # Exclude points from largest cpocket threshold that are exterior to convex hull first, then check for those points prescence in the smaller pocket threshold point sets
                    newVertInd = len(
                        hull.points
                    )  # If a new vert shows up with this index, it is outside of the convex hull
                    hull.add_points(np.array([p]))
                    if newVertInd in hull.vertices:  # if point is outside convex hull
                        hull = ConvexHull(
                            points=hullVerts,
                            incremental=True)  # reset convex hull
                    else:
                        inHullPnts.append(p)
                hull.close()
                # coords2Mol2('./data/unilectin/structures/bSites/pntsb4DBSCAN/' + pdb + '_' + bs.replace(':','-') + '.mol2', inHullPnts)
                logFH.write('From ' + str(len(binPnts[-1])) + ' points, ' +
                            str(len(inHullPnts)) +
                            ' points found within the convex hull\n\n')
                # print('From ' + str(len(binPnts[-1])) + ' points, ' + str(len(inHullPnts)) + ' points found within the convex hull\n')

                # Drop points outside of the convex hull from the binned points as well
                for i in range(len(binPnts)):
Example #13
0
class Agent(object):
    def __init__(self):
        """
         Load your agent here and initialize anything needed
         WARNING: any path to files you wish to access on the docker should be ABSOLUTE PATHS
        """
        self.green = np.array([129.0, 191.0, 65.0])
        self.yellow = np.array([100.0, 65.0, 5.0])
        self.red = np.array([185.0, 50.0, 50.0])
        self.blue = np.array([50.0, 77.0, 121.0])
        self.grey = np.array([115.0, 115.0, 115.0])
        self.blue_threshold = 50
        self.grey_threshold = 100
        self.best_dir = None
        self.explore_turns = 0
        self.obstacle_threshold = 1764

        self.mode = 'spin'
        self.spin_turns = 0
        self.steps = 0
        self.k = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])
        self.mean = np.zeros((3, 3))
        self.mean = self.mean + 1 / 9
        self.k2 = np.array([[1, 0, -1], [0, 0, 0], [-1, 0, 1]])
        self.max_r = -100

        self.theta = 3.1415926 / 2
        self.pi = 3.1415926
        self.points = [
            np.array([0, 0]),
            np.array([0.1, 0]),
            np.array([0, 0.1])
        ]
        self.convex_hull = ConvexHull(np.array(self.points), incremental=True)
        self.point = np.array([0, 0])
        self.attempt_point = None
        self.init = True
        self.force_turn = False

        self.fetch_steps = 0

        self.semantic_mask = semantic_mask()

    def reset(self, t=250):
        """
        Reset is called before each episode begins
        Leave blank if nothing needs to happen there
        :param t the number of timesteps in the episode
        """
        self.setmode('spin')
        self.steps = 0
        self.points = [
            np.array([0, 0]),
            np.array([0.1, 0]),
            np.array([0, 0.1])
        ]
        self.convex_hull = ConvexHull(np.array(self.points), incremental=True)
        self.point = np.array([0, 0])
        self.init = True
        self.force_turn = False
        self.t = t

    def get_colours(self, pixels, colour):
        if np.array_equal(colour, self.blue):
            return np.all(pixels > np.minimum(colour * .95, colour - 5),
                          axis=-1) & np.all(
                              pixels < np.maximum(colour * 1.05, colour + 5),
                              axis=-1)
        return np.all(
            pixels > np.minimum(colour * .8, colour - 25), axis=-1) & np.all(
                pixels < np.maximum(colour * 1.2, colour + 25), axis=-1)

    def inspect(self, img):
        green = self.get_colours(img, self.green)
        yellow = self.get_colours(img, self.yellow)
        ag, bg = np.where(green == True)
        ay, by = np.where(yellow == True)
        return len(ag) > 0 or len(ay) > 0

    def fetch(self, obs):
        img = obs[0]
        green = self.get_colours(img, self.green)
        yellow = self.get_colours(img, self.yellow)
        a, b = np.where(green == True)
        ay, by = np.where(yellow == True)
        self.fetch_steps = self.fetch_steps + 1
        if len(ay) != 0:
            # print('ye')
            self.spin_turns = 0
            if np.mean(by) > 40:
                action = [1, 1]
            elif np.mean(by) < 40:
                action = [1, 2]
            else:
                action = [1, 0]
        elif len(a) != 0:
            # print('gr')
            self.spin_turns = 0
            if np.mean(b) > 60:
                action = [0, 1]
            elif np.mean(b) < 20:
                action = [0, 2]
            elif np.mean(b) > 40:
                action = [1, 1]
            elif np.mean(b) < 40:
                action = [1, 2]
            else:
                action = [1, 0]
        else:
            action = [0, 1]
            self.setmode('spin')

        if self.fetch_steps > 2 and obs[1][2] < 0.5:
            grey = self.get_colours(img, self.grey)
            a, b = np.where(grey == True)
            if np.mean(b) > 40:
                action = [0, 2]
            else:
                action = [0, 1]
            if np.mean(b) < 25 or np.mean(b) > 57:
                self.fetch_steps = 0
        return action

    def spin(self, obs, reward, done, info):
        img = obs[0]

        action = [0, 1]
        self.spin_turns = self.spin_turns + 1
        if self.spin_turns > 360 / 6:
            self.setmode('explore')
            if self.init:
                self.init = False
        return action

    def check(self, obs):

        img = obs[0]
        blue = self.get_colours(img, self.blue)
        a, b = np.where(blue == True)
        if len(a) > 0:
            self.sky_appear = True
        else:
            if self.sky_appear == True:
                self.setmode('explore')

        self.spin_turns = self.spin_turns + 1
        if self.spin_turns > 360 / 6:
            self.setmode('explore')

        return self.check_turn

    def explore(self, obs, reward, done, info):
        self.explore_turns = self.explore_turns - 1
        if self.explore_turns > 0:
            return self.explore_turn
        if self.explore_turns == 0:
            self.force_turn = False
        if obs[1][0] > 0.5:
            return [1, 1]
        if obs[1][0] < -0.5:
            return [1, 2]
        img = obs[0]
        blue = self.get_colours(img, self.blue)
        a, b = np.where(blue == True)
        if len(a) > 10:
            self.sky_mean = np.mean(b)
        if obs[1][2] < 0.5 and self.explore_turns < -5:
            self.setmode('check', obs=obs)
            return [2, 0]
        return [1, 0]

    def valid_openning(self, obs):
        img = obs[0]
        blue = self.get_colours(img[:, 41:45, :], self.blue)
        a, b = np.where(blue == True)
        grey = self.semantic_mask.grey_mask(img)
        ga, gb = np.where(grey[42:, :] == True)
        return len(a) > self.blue_threshold and len(
            ga) < self.obstacle_threshold and self.danger(img) == False

    def score(self, img):
        green = self.get_colours(img, self.green)
        yellow = self.get_colours(img, self.yellow)
        ag, bg = np.where(green == True)
        ay, by = np.where(yellow == True)
        return 10000 * max(len(ag), len(ay))

    def getedge(self, img):
        edge = img
        edge = np.mean(edge, axis=-1)
        img = Image.fromarray(edge.astype(np.uint8))
        img.save('crr/my' + str(self.steps) + '.png')
        edge = ndimage.convolve(edge, self.mean, mode='constant', cval=0.0)
        edge = ndimage.convolve(edge, self.k, mode='constant', cval=0.0)
        # edge = feature.canny(edge, sigma=3)*255
        print(np.mean(edge))
        ff = edge.astype(np.uint8)
        img = Image.fromarray(ff)
        img.save('cr/my' + str(self.steps) + '.png')
        return edge

    def update_convexhull(self, obs):
        direction = np.array([math.cos(self.theta), math.sin(self.theta)])
        velocity = direction * obs[1][2]
        if np.linalg.norm(velocity) > 0.1:
            self.point = self.point + velocity
            self.points.append(self.point)

            self.convex_hull.add_points(np.array([self.point]))

    def setmode(self, mode, obs=None):
        self.mode = mode
        if mode == 'spin':
            self.spin_turns = 0
            self.max_r = -100
        elif mode == 'explore':
            desired_spin = self.update(-self.theta, self.best_dir)
            if desired_spin > 0:
                self.explore_turn = [0, 2]
                self.explore_turns = desired_spin // (6 / 180 * self.pi)
            else:
                self.explore_turn = [0, 1]
                self.explore_turns = desired_spin // (-6 / 180 * self.pi)
            self.sky_mean = -1
            self.convex_hull.add_points(np.array([self.attempt_point]))
        elif mode == 'fetch':
            self.fetch_steps = 0
        elif mode == 'check':
            assert obs is not None
            self.spin_turns = 0
            self.max_r = -100

            img = obs[0]
            blue = self.get_colours(img, self.blue)
            a, b = np.where(blue == True)
            if len(a) > 0:
                if np.mean(b) < 42:
                    self.check_turn = [0, 2]
                else:
                    self.check_turn = [0, 1]
                self.sky_appear = True
            else:
                if self.sky_mean < 42:
                    self.check_turn = [0, 2]
                else:
                    self.check_turn = [0, 1]
                self.sky_appear = False
        elif mode == 'forward':
            self.mode = 'forward'

    def forward(self, obs):
        if self.nodanger(obs[0]):
            self.setmode('spin')
        return [1, 0]

    def update(self, theta, angle):
        theta = theta + angle
        if theta > self.pi:
            theta = theta - 2 * self.pi
        if theta < -self.pi:
            theta = theta + 2 * self.pi
        return theta

    def free_reward(self, obs):
        ch = ConvexHull(np.array(self.points), incremental=True)
        area = ch.area
        direction = np.array([math.cos(self.theta), math.sin(self.theta)])
        velocity = direction * 50
        newp = self.point + velocity
        ch.add_points(np.array([newp]))
        narea = ch.area
        return narea - area, newp

    def savefig(self, img):
        img = Image.fromarray(img.astype(np.uint8))
        img.save('crr/my' + str(self.steps) + '.png')

    def danger(self, img):
        red = self.get_colours(img, self.red)
        kk = red[50:, 35:50]
        aa, bb = np.where(kk)
        return len(aa) > 0

    def nodanger(self, img):
        red = self.get_colours(img, self.red)
        kk = red[50:, :]
        aa, bb = np.where(kk)
        return len(aa) == 0

    def step(self, obs, reward, done, info):
        """
        A single step the agent should take based on the current state of the environment
        We will run the Gym environment (AnimalAIEnv) and pass the arguments returned by env.step() to
        the agent.

        Note that should if you prefer using the BrainInfo object that is usually returned by the Unity
        environment, it can be accessed from info['brain_info'].

        :param obs: agent's observation of the current environment
        :param reward: amount of reward returned after previous action
        :param done: whether the episode has ended.
        :param info: contains auxiliary diagnostic information, including BrainInfo.
        :return: the action to take, a list or size 2
        """
        a, b = obs
        obs = (a * 255, b)
        if self.inspect(
                obs[0]
        ) and self.force_turn == False and self.init == False and self.danger(
                obs[0]) == False:
            self.setmode('fetch')

        #points = self.semantic_mask.boundary_mask(obs[0])

        #self.semantic_mask.checkmask(obs[0],self.semantic_mask.boundary_mask(obs[0]))

        if savefile:
            self.savefig(obs[0])
        self.steps = self.steps + 1

        self.update_convexhull(obs)
        if (self.valid_openning(obs) or self.inspect(
                obs[0])) and (self.mode == 'spin' or self.mode == 'check'):
            free_r, newp = self.free_reward(obs)
            if self.inspect(obs[0]):
                free_r = self.score(obs[0])
                self.force_turn = True
            if free_r > self.max_r:
                self.max_r = free_r
                self.best_dir = self.theta
                self.attempt_point = newp
        print(self.mode)
        if self.mode == 'spin':
            action = self.spin(obs, reward, done, info)
        elif self.mode == 'explore':
            action = self.explore(obs, reward, done, info)
        elif self.mode == 'fetch':
            action = self.fetch(obs)
        elif self.mode == 'forward':
            action = self.forward(obs)
        else:
            action = self.check(obs)

        if self.danger(obs[0]) and (obs[1][2] > 0.001 or action[1] == 1):
            action = [0, 1]
            if obs[1][2] > 5:
                action = [2, 1]
            self.setmode('forward')
        if action[1] == 1:
            self.theta = self.update(self.theta, -6 / 180 * self.pi)
        if action[1] == 2:
            self.theta = self.update(self.theta, 6 / 180 * self.pi)

        return action
Example #14
0
class DelaunayTriangulation:
    def __init__(self, convex_covering_space):
        self.__convex_covering_space = convex_covering_space
        self.__coordinates_to_points = None
        self.__coordinates_number = 0
        self.__points_to_coordinates = None
        self.__points = None
        self.__convex_hull = None

    def add_points(self, points, restart=False):
        import numpy as np
        coordinates, c2p, p2c = self.__convex_covering_space.coordinates(
            points)
        self.__points = self.__convex_covering_space.extend(
            self.__points, points)
        if self.__convex_hull is None:
            from scipy.spatial import ConvexHull
            if self.__convex_covering_space.infinity is not None:
                coordinates = np.vstack(
                    (self.__convex_covering_space.infinity, coordinates))
                self.__coordinates_to_points = np.hstack(([0], c2p + 1)) - 1
                self.__points_to_coordinates = p2c + 1
            else:
                self.__coordinates_to_points = c2p
                self.__points_to_coordinates = p2c
            self.__convex_hull = ConvexHull(coordinates, incremental=True)
        else:
            self.__coordinates_to_points = np.hstack(
                (self.__coordinates_to_points, c2p + self.__points_number))
            self.__points_to_coordinates = np.hstack(
                (self.__points_to_coordinates,
                 p2c + self.__coordinates_number))

            self.__convex_hull.add_points(coordinates, restart)
        self.__coordinates_number = len(self.__coordinates_to_points)
        self.__points_number = len(self.__points_to_coordinates)

    @property
    def points(self):
        return self.__points

    @property
    def simplices(self):
        self.__simplices = self.__coordinates_to_points[
            self.__filter_external_simplices(
                self.__filter_infinite_simplices(
                    self.__convex_hull.simplices))]
        return self.__simplices

    @property
    def neighbors(self):
        pass

    def __filter_infinite_simplices(self, simplices):
        import numpy as np
        if self.__convex_covering_space.infinity is not None:
            finite_mask = np.all(simplices != 0, axis=1)
            simplices = simplices[finite_mask]
        return simplices

    def __filter_external_simplices(self, simplices):
        import numpy as np
        internal_mask = np.any(self.__points_to_coordinates[
            self.__coordinates_to_points[simplices]] == simplices,
                               axis=1)
        simplices = simplices[internal_mask]
        return simplices
Example #15
0
    warden_points = np.array(warden_list)
    warden_cv = ConvexHull(warden_points, True)
if poach > 0:
    poach_points = np.array(poach_list)
    poach_cv = ConvexHull(poach_list, True)
for i in range(manatee):
    x, y = map(int, stdin.readline().split())
    info = Manatee_info()
    info.x_coordi = x
    info.y_coordi = y
    manatee_list.append(info)
if warden > 0:
    for info in manatee_list:
        temp_points = warden_cv.points
        temp_vertices = warden_cv.vertices
        warden_cv.add_points(info.get_points(), True)
        com_result = np.array_equal(temp_vertices, warden_cv.vertices)
        warden_cv = ConvexHull(temp_points, True)
        if com_result:
            info.manatee_status = "safe"
if poach > 0:
    for info in manatee_list:
        temp_points = poach_cv.points
        temp_vertices = poach_cv.vertices
        poach_cv.add_points(info.get_points(), True)
        com_result = np.array_equal(temp_vertices, poach_cv.vertices)
        poach_cv = ConvexHull(temp_points, True)
        if com_result and info.manatee_status != "safe":
            info.manatee_status = "endangered"
for info in manatee_list:
    stdout.write(info.get_result())
Example #16
0
plt.plot(police_points[:, 0], police_points[:, 1], 'o', color='black')
plt.plot(bar_points[:, 0], bar_points[:, 1], 'o', color='blue')

police_hull = ConvexHull(police_points, incremental=True)
bar_hull = ConvexHull(bar_points, incremental=True)

draw_simplices_hull(police_hull, police_points, 'k-')
draw_simplices_hull(bar_hull, bar_points, 'r-')
plt.show()
#집과 경찰서와의 관계
for home in home_list:
    temp_vertices = police_hull.vertices
    home_point = np.array(home.get_points())
    print(home_point)
    police_hull_added_home = police_hull.add_points(home_point)
    safe = np.array_equal(temp_vertices, police_hull_added_home)
    if safe:
        home.status = 'safe'
#집과 술집과의 관계
for home in home_list:
    temp_vertices = police_hull.vertices
    home_point = np.array(home.get_points())
    bar_hull_added_home = bar_hull.add_points(home_point)
    danger = np.array_equal(temp_vertices, bar_hull_added_home)
    if danger and home.status == 'safe':
        home.status = 'vulnerable'
    if danger and home.status == 'vulnerable':
        home.status = 'endangered'

for home in home_list:
class BinarySeparation(object):
    def __init__(self, E, A, B, is_manifold_true=False):
        self.field_size = math.inf
        self.size_E = len(E)
        self.dimension_E = len(E[0])
        self.colors_E = []

        self.A = A
        self.B = B
        self.size_A = len(A)
        self.size_B = len(B)

        # set colors
        for i in range(self.size_E):
            if i in self.A:
                self.colors_E.append("red")
            elif i in self.B:
                self.colors_E.append("green")
            else:
                self.colors_E.append("blue")

        # point distances
        self.convex_A_distances = {}
        self.convex_B_distances = {}
        self.convex_A_hull_distances = {}
        self.convex_B_hull_distances = {}

        self.E = E
        self.hull_C_A = ConvexHull(self.E[self.A], 1)
        self.C_A, added = get_points_inside_convex_hull(self.E, self.hull_C_A, self.A, [x for x in range(self.size_E) if x not in self.A])
        for x in added:
            self.colors_E[x] = 'orange'

        self.hull_C_B = ConvexHull(self.E[self.B], 1)
        self.C_B, added = get_points_inside_convex_hull(self.E, self.hull_C_B, self.B, [x for x in range(self.size_E) if x not in self.B])
        for x in added:
            self.colors_E[x] = 'violet'

        if is_manifold_true:
            # manifolds from hull
            self.m1 = gel.Manifold()
            for s in self.hull_C_A.simplices:
                self.m1.add_face(self.hull_C_A.points[s])

            self.m1dist = gel.MeshDistance(self.m1)
            self.m2 = gel.Manifold()
            for s in self.hull_C_B.simplices:
                self.m2.add_face(self.hull_C_B.points[s])

            self.m2dist = gel.MeshDistance(self.m2)

        # Set unlabeled elements
        self.F = list(set(range(self.size_E)) - set([x for x in self.C_A or x in self.C_B]))

        # Pre-computation of monotone linkages
        self.convex_a_neighbors()
        self.convex_b_neighbors()

    def plot_2d_classification(self, name="Test", colorlist=[]):

        # initialize first half space
        PointsH_1 = np.ndarray(shape=(len(self.C_A), self.dimension_E))
        counter = 0
        for i in self.C_A:
            PointsH_1[counter] = self.get_point(i)
            counter += 1
        self.C_A = ConvexHull(PointsH_1, 1)

        # Initialize second half space
        PointsH_2 = np.ndarray(shape=(len(self.C_B), self.dimension_E))
        counter = 0
        for i in self.C_B:
            PointsH_2[counter] = self.get_point(i)
            counter += 1
        self.C_B = ConvexHull(PointsH_2, 1)

        # Draw convex initial_hulls of disjoint convex sets
        for simplex in self.C_A.simplices:
            plt.plot(self.C_A.points[simplex, 0], self.C_A.points[simplex, 1], 'k-')
        for simplex in self.C_B.simplices:
            plt.plot(self.C_B.points[simplex, 0], self.C_B.points[simplex, 1], 'k-')

        x_val_dict = {}
        y_val_dict = {}

        if colorlist == []:
            colorlist = self.colors_E

        for i, x in enumerate(colorlist, 0):
            if x not in x_val_dict:
                x_val_dict[x] = [self.E[i][0]]
                y_val_dict[x] = [self.E[i][1]]
            else:
                x_val_dict[x].append(self.E[i][0])
                y_val_dict[x].append(self.E[i][1])

        for key, value in x_val_dict.items():
            plt.scatter(value, y_val_dict[key], c=key)

        tikzplotlib.save(name + ".tex")
        plt.show()

    def plot_3d_classification(self):

        # initialize first half space
        PointsH_1 = np.ndarray(shape=(len(self.C_A), self.dimension_E))
        counter = 0
        for i in self.C_A:
            PointsH_1[counter] = self.get_point(i)
            counter += 1

        self.C_A = ConvexHull(PointsH_1, 1)

        # Initialize second half space
        PointsH_2 = np.ndarray(shape=(len(self.C_B), self.dimension_E))
        counter = 0
        for i in self.C_B:
            PointsH_2[counter] = self.get_point(i)
            counter += 1

        self.C_B = ConvexHull(PointsH_2, 1)

        # print(self.ConvexHull1.equations)

        ax = plt.axes(projection='3d')

        for simplex in self.C_A.simplices:
            ax.plot3D(self.C_A.points[simplex, 0], self.C_A.points[simplex, 1],
                      self.C_A.points[simplex, 2], 'k-')
        for simplex in self.C_B.simplices:
            ax.plot3D(self.C_B.points[simplex, 0], self.C_B.points[simplex, 1],
                      self.C_B.points[simplex, 2], 'k-')

        X_coord = np.ones(self.size_E)
        Y_coord = np.ones(self.size_E)
        Z_coord = np.ones(self.size_E)
        for i in range(self.size_E):
            X_coord[i] = self.E[i][0]
            Y_coord[i] = self.E[i][1]
            Z_coord[i] = self.E[i][2]

        ax.scatter3D(X_coord, Y_coord, Z_coord, c=self.colors_E)
        plt.show()

    def get_point(self, n):
        return self.E[n]

    def convex_a_neighbors(self, is_manifold=False):
        for n in self.F:
            min_dist = sys.maxsize
            for x in self.C_A:
                point_dist = dist(self.get_point(n), self.get_point(x))
                if point_dist < min_dist:
                    min_dist = point_dist
            self.convex_A_distances[n] = min_dist

            if is_manifold:
                d = self.m1dist.signed_distance(self.get_point(n))
                self.convex_A_hull_distances[n] = d * np.sign(d)
        self.convex_A_distances = OrderedDict(sorted(self.convex_A_distances.items(), key=lambda x: x[1], reverse=True))

        if is_manifold:
            self.convex_A_hull_distances = OrderedDict(
                sorted(self.convex_A_hull_distances.items(), key=lambda x: x[1], reverse=True))

    def convex_b_neighbors(self, is_manifold=False):
        for n in self.F:
            min_dist = sys.maxsize
            for x in self.C_B:
                point_dist = dist(self.get_point(n), self.get_point(x))
                if point_dist < min_dist:
                    min_dist = point_dist
            self.convex_B_distances[n] = min_dist

            if is_manifold:
                d = self.m2dist.signed_distance(self.get_point(n))
                self.convex_A_hull_distances[n] = d * np.sign(d)
        self.convex_B_distances = OrderedDict(sorted(self.convex_B_distances.items(), key=lambda x: x[1], reverse=True))

        if is_manifold:
            self.convex_B_hull_distances = OrderedDict(
                sorted(self.convex_B_distances.items(), key=lambda x: x[1], reverse=True))

    def decide_nearest(self):
        len1 = len(self.convex_A_distances)
        len2 = len(self.convex_B_distances)

        if len1 == 0:
            return 0
        elif len2 == 0:
            return 1
        elif next(reversed(self.convex_A_distances.values())) <= next(reversed(self.convex_B_distances.values())):
            return 1
        else:
            return 0

    def decide_nearest_hull(self):
        len1 = len(self.convex_A_hull_distances)
        len2 = len(self.convex_B_hull_distances)

        if len1 == 0:
            return 0
        elif len2 == 0:
            return 1
        elif next(reversed(self.convex_A_hull_distances.values())) <= next(
                reversed(self.convex_B_hull_distances.values())):
            return 1
        else:
            return 0

    def decide_farthest(self):
        len1 = len(self.convex_A_distances)
        len2 = len(self.convex_B_distances)

        if len1 == 0:
            return 0
        elif len2 == 0:
            return 1
        elif (next(iter(self.convex_A_distances.values())) <= next(iter(self.convex_B_distances.values()))):
            return 1
        else:
            return 0

    def add_C_A_nearest(self):
        point = list(self.convex_A_distances.items())[0][0]
        self.C_A.append(point)
        self.colors_E[point] = "orange"
        self.F.remove(point)
        del self.convex_A_distances[point]
        del self.convex_B_distances[point]

    def add_C_B_nearest(self):
        point = list(self.convex_B_distances.items())[0][0]
        self.C_B.append(point)
        self.colors_E[point] = "violet"
        self.F.remove(point)
        del self.convex_B_distances[point]
        del self.convex_A_distances[point]

    # SeCos-Algorithm from paper applied to convex initial_hulls in R^d
    def greedy_alg(self):
        end = False
        oracle_calls = 0

        # take an arbitrary element out of F
        random.shuffle(self.F)
        # label vector of the elements (binary classification {-1, 1}
        labels = -1 * np.ones(shape=(self.size_E, 1))

        # E labels of initial labelled data
        for i in self.C_A:
            labels[i] = 1
        for i in self.C_B:
            labels[i] = 0

        if not intersect(self.C_A, self.C_B):
            while len(self.F) > 0 and end == False:
                # print(len(self.F))
                next_point = self.F.pop()
                new_hull_C_A = ConvexHull(self.E[self.C_A], 1)
                new_hull_C_A.add_points([self.get_point(next_point)], 1)
                new_C_A, added = get_points_inside_convex_hull(self.E, new_hull_C_A, self.C_A, self.F + self.C_B,
                                                               next_point)
                oracle_calls += 1

                if not intersect(new_C_A, self.C_B):
                    self.hull_C_A = new_hull_C_A
                    self.C_A = new_C_A
                    self.F = list(set(self.F) - set(added))

                    for x in added:
                        labels[x] = 1
                        self.colors_E[x] = "orange"

                else:
                    new_hull_C_B = ConvexHull(self.E[self.C_B], 1)
                    new_hull_C_B.add_points([self.get_point(next_point)], 1)
                    new_C_B, added = get_points_inside_convex_hull(self.E, new_hull_C_B, self.C_B, self.F + self.C_A,
                                                                   next_point)
                    oracle_calls += 1

                    if not intersect(self.C_A, new_C_B):
                        self.hull_C_B = new_hull_C_B
                        self.C_B = new_C_B
                        self.F = list(set(self.F) - set(added))
                        for x in added:
                            labels[x] = 0
                            self.colors_E[x] = "violet"
        else:
            return [], [], False

        return oracle_calls, labels, True

    def greedy_fast_alg(self):
        end = False
        oracle_calls = 0
        random.shuffle(self.F)

        # label vector of the elements (binary classification {1, 0} -1 are unclassified
        labels = -1 * np.ones(shape=(self.size_E, 1))
        outside_1 = self.F + self.C_B
        outside_2 = self.F + self.C_A

        # E labels of initial labelled data
        for i in self.C_A:
            labels[i] = 1
        for i in self.C_B:
            labels[i] = 0

        # E initial initial_hulls
        inside1, added, intersection = get_inside_points(self.E, self.C_A, self.F, CheckSet=self.C_B)
        oracle_calls += 1

        if not intersection:
            for x in added:
                labels[x] = 1
                self.colors_E[x] = "orange"
            self.C_A = inside1

        # E initial initial_hulls
        inside2, added, intersection = get_inside_points(self.E, self.C_B, self.F, CheckSet=self.C_A)
        oracle_calls += 1

        if not intersection:
            for x in added:
                labels[x] = 0
                self.colors_E[x] = "violet"
            self.C_B = inside2

        if not intersect(inside1, inside2):
            for x in self.C_A:
                if x in self.F:
                    self.F.remove(x)
            for x in self.C_B:
                if x in self.F:
                    self.F.remove(x)

            while len(self.F) > 0 and end == False:
                # print(len(self.F))
                next_point = self.F.pop()
                inside1, added, intersection = get_inside_points(self.E, self.C_A, self.F, next_point,
                                                                 self.C_B)
                oracle_calls += 1

                if not intersection:
                    for x in added:
                        labels[x] = 1
                        self.colors_E[x] = "orange"
                        if x in self.F:
                            self.F.remove(x)
                    self.C_A = inside1

                else:
                    inside2, added, intersection = get_inside_points(self.E, self.C_B, self.F, next_point,
                                                                     self.C_A)
                    oracle_calls += 1

                    if not intersection:
                        for x in added:
                            labels[x] = 0
                            self.colors_E[x] = "violet"
                            if x in self.F:
                                self.F.remove(x)
                        self.C_B = inside2
        else:
            return ([], [], False)

        return (oracle_calls, labels, True)

    def greedy_alg2(self):
        end = False
        oracle_calls = 0
        random.shuffle(self.F)

        # label vector of the elements (binary classification {-1, 1}
        labels = -1 * np.ones(shape=(self.size_E, 1))
        outside_1 = self.F + self.C_B
        outside_2 = self.F + self.C_A

        # E labels of initial labelled data
        for i in self.C_A:
            labels[i] = 1
        for i in self.C_B:
            labels[i] = 0

        # E initial initial_hulls
        Hull1 = self.C_A
        inside1, added = get_points_inside_convex_hull(self.E, Hull1, self.C_A, self.F + self.C_B)
        oracle_calls += 1

        if not intersect(inside1, self.C_B):
            for x in added:
                labels[x] = 1
                self.colors_E[x] = "orange"
            self.C_A = inside1

        # E initial initial_hulls
        Hull2 = self.C_B
        inside2, added = get_points_inside_convex_hull(self.E, Hull2, self.C_B, self.F + self.C_A)
        oracle_calls += 1

        if not intersect(inside2, self.C_A):
            for x in added:
                labels[x] = 0
                self.colors_E[x] = "violet"
            self.C_B = inside2

        if not intersect(inside1, inside2):
            for x in self.C_A:
                if x in self.F:
                    self.F.remove(x)
            for x in self.C_B:
                if x in self.F:
                    self.F.remove(x)

            while len(self.F) > 0 and end == False:
                # print(len(self.F))
                next_point = self.F.pop()

                if (random.randint(0, 1)):
                    Hull1 = self.C_A
                    Hull1.add_points([self.get_point(next_point)], 1)
                    inside1, added = get_points_inside_convex_hull(self.E, Hull1, self.C_A, self.F + self.C_B,
                                                                   next_point)
                    oracle_calls += 1

                    if not intersect(inside1, self.C_B):
                        self.C_A.add_points([self.get_point(next_point)], 1)
                        for x in added:
                            labels[x] = 1
                            self.colors_E[x] = "orange"
                            if x in self.F:
                                self.F.remove(x)
                        self.C_A = inside1

                    else:
                        # initialize first half space
                        PointsH_1 = np.ndarray(shape=(len(self.C_A), self.dimension_E))
                        counter = 0
                        for i in self.C_A:
                            PointsH_1[counter] = self.get_point(i)
                            counter += 1

                        self.C_A = ConvexHull(PointsH_1, 1)

                        Hull2 = self.C_B
                        Hull2.add_points([self.get_point(next_point)], 1)
                        inside2, added = get_points_inside_convex_hull(self.E, Hull2, self.C_B,
                                                                       self.F + self.C_A, next_point)
                        oracle_calls += 1

                        if not intersect(self.C_A, inside2):
                            self.C_B.add_points([self.get_point(next_point)], 1)
                            for x in added:
                                labels[x] = 0
                                self.colors_E[x] = "violet"
                                if x in self.F:
                                    self.F.remove(x)
                            self.C_B = inside2
                        else:
                            # initialize second half space
                            PointsH_2 = np.ndarray(shape=(len(self.C_B), self.dimension_E))
                            counter = 0
                            for i in self.C_B:
                                PointsH_2[counter] = self.get_point(i)
                                counter += 1

                            self.C_B = ConvexHull(PointsH_2, 1)

                else:
                    Hull2 = self.C_B
                    Hull2.add_points([self.get_point(next_point)], 1)
                    inside2, added = get_points_inside_convex_hull(self.E, Hull2, self.C_B, self.F + self.C_A,
                                                                   next_point)
                    oracle_calls += 1

                    if not intersect(inside2, self.C_A):
                        self.C_B.add_points([self.get_point(next_point)], 1)
                        for x in added:
                            labels[x] = 0
                            self.colors_E[x] = "violet"
                            if x in self.F:
                                self.F.remove(x)
                        self.C_B = inside2

                    else:
                        # initialize first half space
                        PointsH_2 = np.ndarray(shape=(len(self.C_B), self.dimension_E))
                        counter = 0
                        for i in self.C_B:
                            PointsH_2[counter] = self.get_point(i)
                            counter += 1

                        self.C_B = ConvexHull(PointsH_2, 1)

                        Hull1 = self.C_A
                        Hull1.add_points([self.get_point(next_point)], 1)
                        inside1, added = get_points_inside_convex_hull(self.E, Hull1, self.C_A,
                                                                       self.F + self.C_B, next_point)
                        oracle_calls += 1

                        if not intersect(self.C_B, inside1):
                            self.C_A.add_points([self.get_point(next_point)], 1)
                            for x in added:
                                labels[x] = 1
                                self.colors_E[x] = "orange"
                                if x in self.F:
                                    self.F.remove(x)
                            self.C_A = inside1
                        else:
                            # initialize second half space
                            PointsH_1 = np.ndarray(shape=(len(self.C_A), self.dimension_E))
                            counter = 0
                            for i in self.C_A:
                                PointsH_1[counter] = self.get_point(i)
                                counter += 1

                            self.C_A = ConvexHull(PointsH_1, 1)
        else:
            return ([], [], False)

        return (oracle_calls, labels, True)

    def optimal_alg(self):
        time_point = time.time()
        oracle_calls = 0
        counter = 0
        labels = -1 * np.ones(shape=(self.size_E, 1))
        outside_points_1 = [x for x in self.convex_A_distances.keys()] + self.C_B
        outside_points_2 = [x for x in self.convex_B_distances.keys()] + self.C_A

        # add labels
        for i in self.C_A:
            labels[i] = 1
        for i in self.C_B:
            labels[i] = 0

        if not intersect(self.C_A, self.C_B):
            while (len(self.convex_A_distances) > 0 or len(self.convex_B_distances) > 0):
                # print(len(self.Set1Distances), len(self.Set2Distances))
                added = []

                # First set is nearer to nearest not classified point
                if self.decide_nearest():
                    time_point = time_step("Find Neighbour:", time_point)
                    if len(self.convex_A_distances) > 0:
                        next_point = self.convex_A_distances.popitem()[0]
                        new_hull_C_A = ConvexHull(self.E[self.C_A], 1)
                        new_hull_C_A.add_points([self.get_point(next_point)], 1)
                        time_point = time_step("Adding Convex Hull E 1:", time_point)
                        new_C_A, added = get_points_inside_convex_hull(self.E, new_hull_C_A, self.C_A,
                                                                       outside_points_1)
                        oracle_calls += 1
                        time_point = time_step("Getting inside E:", time_point)

                        # if there is no intersection the point can be added to the first convex set
                        if not intersect(new_C_A, self.C_B):
                            time_point = time_step("Intersection Test:", time_point)
                            self.C_A = new_C_A
                            self.hull_C_A = new_hull_C_A
                            for x in added:
                                # add to labels
                                labels[x] = 1
                                self.colors_E[x] = "orange"
                                if x in self.convex_A_distances.keys():
                                    del self.convex_A_distances[x]
                                if x in self.convex_B_distances.keys():
                                    del self.convex_B_distances[x]
                            outside_points_1 = list(set(outside_points_1) - set(added))
                            time_point = time_step("Update arrays:", time_point)


                        # if there is an intersection we have to check if it can be added to the second set
                        else:
                            # Test second half space
                            new_hull_C_B = ConvexHull(self.E[self.C_B], 1)
                            new_hull_C_B.add_points([self.get_point(next_point)], 1)
                            new_C_B, added = get_points_inside_convex_hull(self.E, new_hull_C_B, self.C_B,
                                                                           outside_points_2)
                            oracle_calls += 1

                            # the point can be added to the second set,
                            # if we reach this point the first time all the other E which are classified did not change the optimal margin
                            if not intersect(self.C_A, new_C_B):
                                self.C_B = new_C_B
                                self.hull_C_B = new_hull_C_B
                                for x in added:
                                    # add to labels
                                    labels[x] = 0
                                    self.colors_E[x] = "violet"
                                    if x in self.convex_A_distances.keys():
                                        del self.convex_A_distances[x]
                                    if x in self.convex_B_distances.keys():
                                        del self.convex_B_distances[x]
                                outside_points_2 = list(set(outside_points_2) - set(added))
                            # the point cannot be added to any set
                            else:
                                if next_point in outside_points_1:
                                    outside_points_1.remove(next_point)
                                if next_point in outside_points_2:
                                    outside_points_2.remove(next_point)

                    time_point = time_step("Point add Hull:", time_point)

                # Second set is nearer to nearest not classified point
                else:
                    time_point = time_step("Find Neighbour:", time_point)
                    if len(self.convex_B_distances) > 0:
                        next_point = self.convex_B_distances.popitem()[0]
                        new_hull_C_B = ConvexHull(self.E[self.C_B], 1)
                        new_hull_C_B.add_points([self.get_point(next_point)], 1)
                        new_C_B, added = get_points_inside_convex_hull(self.E, new_hull_C_B, self.C_B,
                                                                       outside_points_2)
                        oracle_calls += 1

                        # we can add the new point to the second, the nearer set
                        if not intersect(new_C_B, self.C_A):
                            self.C_B = new_C_B
                            self.hull_C_B = new_hull_C_B
                            for x in added:
                                # add to labels
                                labels[x] = 0
                                self.colors_E[x] = "violet"
                                if x in self.convex_A_distances.keys():
                                    del self.convex_A_distances[x]
                                if x in self.convex_B_distances.keys():
                                    del self.convex_B_distances[x]
                            outside_points_2 = list(set(outside_points_2) - set(added))

                        # we check if we can add the point to the first set
                        # if we reach this point the first time all the other E which are classified did not change the optimal margin
                        else:
                            # Test first half space
                            new_hull_C_A = ConvexHull(self.E[self.C_A], 1)
                            new_hull_C_A.add_points([self.get_point(next_point)], 1)
                            new_C_A, added = get_points_inside_convex_hull(self.E, new_hull_C_A, self.C_A,
                                                                           outside_points_1)
                            oracle_calls += 1

                            # the point can be classified to the second set
                            if not intersect(self.C_B, new_C_A):
                                self.hull_C_A = new_hull_C_A
                                self.C_A = new_C_A
                                for x in added:
                                    # add to labels
                                    labels[x] = 1
                                    self.colors_E[x] = "orange"
                                    if x in self.convex_A_distances.keys():
                                        del self.convex_A_distances[x]
                                    if x in self.convex_B_distances.keys():
                                        del self.convex_B_distances[x]
                                outside_points_1 = list(set(outside_points_1) - set(added))

                            # we cannot classify the point
                            else:
                                if next_point in outside_points_1:
                                    outside_points_1.remove(next_point)
                                if next_point in outside_points_2:
                                    outside_points_2.remove(next_point)

                time_point = time_step("Point add Hull:", time_point)
        else:
            return [], [], False

        return oracle_calls, labels, True

    def optimal_hull_alg(self):
        time_point = time.time()
        oracle_calls = 0
        counter = 0
        labels = -1 * np.ones(shape=(self.size_E, 1))
        outside_points_1 = [x for x in self.convex_A_hull_distances.keys()] + self.C_B
        outside_points_2 = [x for x in self.convex_B_hull_distances.keys()] + self.C_A

        # add labels
        for i in self.C_A:
            labels[i] = 1
        for i in self.C_B:
            labels[i] = 0

        # check if initial_hulls are intersecting
        Hull1 = self.C_A
        inside1, added = get_points_inside_convex_hull(self.E, Hull1, self.C_A, outside_points_1)
        self.C_A = inside1
        Hull2 = self.C_B
        inside2, added = get_points_inside_convex_hull(self.E, Hull2, self.C_B, outside_points_2)
        self.C_B = inside2

        if not intersect(inside1, inside2):
            while (len(self.convex_A_hull_distances) > 0 or len(self.convex_B_hull_distances) > 0):
                # print(len(self.Set1HullDistances), len(self.Set2HullDistances))
                added = []

                # First set is nearer to nearest not classified point
                if self.decide_nearest_hull():

                    time_point = time_step("Find Neighbour:", time_point)

                    if len(self.convex_A_hull_distances) > 0:
                        next_point = self.convex_A_hull_distances.popitem()[0]

                        Hull1 = self.C_A
                        Hull1.add_points([self.get_point(next_point)], 1)
                        time_point = time_step("Adding Convex Hull E 1:", time_point)

                        inside1, added = get_points_inside_convex_hull(self.E, Hull1, self.C_A,
                                                                       outside_points_1)
                        oracle_calls += 1
                        time_point = time_step("Getting inside E:", time_point)

                        # if there is no intersection the point can be added to the first convex set
                        if not intersect(inside1, self.C_B):
                            time_point = time_step("Intersection Test:", time_point)
                            self.C_A = Hull1
                            time_point = time_step("Adding Convex Hull E:", time_point)

                            for x in added:
                                # add to labels
                                labels[x] = 1
                                self.colors_E[x] = "orange"
                                if x in self.convex_A_hull_distances.keys():
                                    del self.convex_A_hull_distances[x]
                                if x in self.convex_B_hull_distances.keys():
                                    del self.convex_B_hull_distances[x]

                                outside_points_1.remove(x)
                            self.C_A = inside1

                            time_point = time_step("Update arrays:", time_point)


                        # if there is an intersection we have to check if it can be added to the second set
                        else:
                            time_point = time_step("Intersection Test:", time_point)
                            # Renew first half space
                            PointsH_1 = np.ndarray(shape=(len(self.C_A), self.dimension_E))
                            counter = 0
                            for i in self.C_A:
                                PointsH_1[counter] = self.get_point(i)
                                counter += 1
                            self.C_A = ConvexHull(PointsH_1, 1)

                            # Test second half space
                            Hull2 = self.C_B
                            Hull2.add_points([self.get_point(next_point)], 1)
                            inside2, added = get_points_inside_convex_hull(self.E, Hull2, self.C_B,
                                                                           outside_points_2)
                            oracle_calls += 1

                            # the point can be added to the second set,
                            # if we reach this point the first time all the other E which are classified did not change the optimal margin
                            if not intersect(self.C_A, inside2):
                                self.C_B = Hull2
                                for x in added:
                                    # add to labels
                                    labels[x] = 0
                                    self.colors_E[x] = "violet"
                                    if x in self.convex_A_hull_distances.keys():
                                        del self.convex_A_hull_distances[x]
                                    if x in self.convex_B_hull_distances.keys():
                                        del self.convex_B_hull_distances[x]
                                    outside_points_2.remove(x)
                                self.C_B = inside2


                            # the point cannot be added to any set
                            else:
                                # Renew second half space
                                PointsH_2 = np.ndarray(shape=(len(self.C_B), self.dimension_E))
                                counter = 0
                                for i in self.C_B:
                                    PointsH_2[counter] = self.get_point(i)
                                    counter += 1
                                self.C_B = ConvexHull(PointsH_2, 1)
                                if next_point in outside_points_1:
                                    outside_points_1.remove(next_point)
                                if next_point in outside_points_2:
                                    outside_points_2.remove(next_point)

                    time_point = time_step("Point add Hull:", time_point)


                # Second set is nearer to nearest not classified point
                else:

                    time_point = time_step("Find Neighbour:", time_point)

                    if len(self.convex_B_hull_distances) > 0:
                        next_point = self.convex_B_hull_distances.popitem()[0]
                        Hull2 = self.C_B
                        Hull2.add_points([self.get_point(next_point)], 1)
                        inside2, added = get_points_inside_convex_hull(self.E, Hull2, self.C_B,
                                                                       outside_points_2)
                        oracle_calls += 1

                        # we can add the new point to the second, the nearer set
                        if not intersect(inside2, self.C_A):
                            self.C_B = Hull2
                            for x in added:
                                # add to labels
                                labels[x] = 0
                                self.colors_E[x] = "violet"
                                if x in self.convex_A_hull_distances.keys():
                                    del self.convex_A_hull_distances[x]
                                if x in self.convex_B_hull_distances.keys():
                                    del self.convex_B_hull_distances[x]
                                outside_points_2.remove(x)
                            self.C_B = inside2



                        # we check if we can add the point to the first set
                        # if we reach this point the first time all the other E which are classified did not change the optimal margin
                        else:
                            # Renew second half space
                            PointsH_2 = np.ndarray(shape=(len(self.C_B), self.dimension_E))
                            counter = 0
                            for i in self.C_B:
                                PointsH_2[counter] = self.get_point(i)
                                counter += 1
                            self.C_B = ConvexHull(PointsH_2, 1)

                            # Test first half space
                            Hull1 = self.C_A
                            Hull1.add_points([self.get_point(next_point)], 1)
                            inside1, added = get_points_inside_convex_hull(self.E, Hull1, self.C_A,
                                                                           outside_points_1)
                            oracle_calls += 1

                            # the point can be classified to the second set
                            if not intersect(self.C_B, inside1):
                                self.C_A = Hull1
                                for x in added:
                                    # add to labels
                                    labels[x] = 1
                                    self.colors_E[x] = "orange"
                                    if x in self.convex_A_hull_distances.keys():
                                        del self.convex_A_hull_distances[x]
                                    if x in self.convex_B_hull_distances.keys():
                                        del self.convex_B_hull_distances[x]
                                    outside_points_1.remove(x)
                                self.C_A = inside1




                            # we cannot classify the point
                            else:
                                # Renew first half space
                                PointsH_1 = np.ndarray(shape=(len(self.C_A), self.dimension_E))
                                counter = 0
                                for i in self.C_A:
                                    PointsH_1[counter] = self.get_point(i)
                                    counter += 1
                                self.C_A = ConvexHull(PointsH_1, 1)
                                if next_point in outside_points_1:
                                    outside_points_1.remove(next_point)
                                if next_point in outside_points_2:
                                    outside_points_2.remove(next_point)

                time_point = time_step("Point add Hull:", time_point)
        else:
            return ([], [], False)

        return (oracle_calls, labels, True)

    def optimal_runtime_alg(self):
        oracle_calls = 0
        end = False
        counter = 0
        while len(self.F) > 0 and end == False:
            # print(len(self.F))
            if not self.decide_farthest():
                items = list(self.convex_A_distances.items())
                if len(items) > 0:
                    next_point = items[len(items) - 1][0]

                    Hull1 = self.C_A
                    Hull1.add_points([self.get_point(next_point)], 1)
                    inside1 = get_points_inside_convex_hull(self.E, Hull1)
                    oracle_calls += 1
                    if not intersect(inside1, self.C_B):
                        self.C_A.add_points([self.get_point(next_point)], 1)
                        for x in inside1:
                            if x not in self.C_A:
                                self.colors_E[x] = "orange"
                                self.F.remove(x)
                                del self.convex_A_distances[x]
                                del self.convex_B_distances[x]
                        self.C_A = inside1

                    else:
                        # Renew first half space
                        PointsH_1 = np.ndarray(shape=(len(self.C_A), 2))
                        counter = 0
                        for i in self.C_A:
                            PointsH_1[counter] = self.get_point(i)
                            counter += 1
                        self.C_A = ConvexHull(PointsH_1, 1)

                        # Test second half space
                        Hull2 = self.C_B
                        Hull2.add_points([self.get_point(next_point)], 1)
                        inside2 = get_points_inside_convex_hull(self.E, Hull2)
                        oracle_calls += 1

                        if not intersect(self.C_A, inside2):
                            self.C_B.add_points([self.get_point(next_point)], 1)
                            for x in inside2:
                                if x not in self.C_B:
                                    self.colors_E[x] = "violet"
                                    self.F.remove(x)
                                    del self.convex_A_distances[x]
                                    del self.convex_B_distances[x]
                            self.C_B = inside2
                        else:
                            # Renew second half space
                            PointsH_2 = np.ndarray(shape=(len(self.C_B), 2))
                            counter = 0
                            for i in self.C_B:
                                PointsH_2[counter] = self.get_point(i)
                                counter += 1
                            self.C_B = ConvexHull(PointsH_2, 1)
                            self.F.remove(next_point)
                            del self.convex_A_distances[next_point]
                            del self.convex_B_distances[next_point]

            else:
                items = list(self.convex_B_distances.items())
                if len(items) > 0:
                    next_point = items[len(items) - 1][0]
                    Hull2 = self.C_B
                    Hull2.add_points([self.get_point(next_point)], 1)
                    inside2 = get_points_inside_convex_hull(self.E, Hull2, self.C_B)
                    oracle_calls += 1

                    if not intersect(inside2, self.C_A):
                        self.C_B.add_points([self.get_point(next_point)], 1)
                        for x in inside2:
                            if x not in self.C_B:
                                self.colors_E[x] = "violet"
                                self.F.remove(x)
                                del self.convex_A_distances[x]
                                del self.convex_B_distances[x]
                        self.C_B = inside2

                    else:
                        # Renew second half space
                        PointsH_2 = np.ndarray(shape=(len(self.C_B), 2))
                        counter = 0
                        for i in self.C_B:
                            PointsH_2[counter] = self.get_point(i)
                            counter += 1
                        self.C_B = ConvexHull(PointsH_2, 1)

                        # Test first half space
                        Hull1 = self.C_A
                        Hull1.add_points([self.get_point(next_point)], 1)
                        inside1 = get_points_inside_convex_hull(self.E, Hull1)
                        oracle_calls += 1

                        if not intersect(self.C_B, inside1):
                            self.C_A.add_points([self.get_point(next_point)], 1)
                            for x in inside1:
                                if x not in self.C_A:
                                    self.colors_E[x] = "orange"
                                    self.F.remove(x)
                                    del self.convex_A_distances[x]
                                    del self.convex_B_distances[x]
                            self.C_A = inside1
                        else:
                            # Renew first half space
                            PointsH_1 = np.ndarray(shape=(len(self.C_A), 2))
                            counter = 0
                            for i in self.C_A:
                                PointsH_1[counter] = self.get_point(i)
                                counter += 1
                            self.C_A = ConvexHull(PointsH_1, 1)
                            self.F.remove(next_point)
                            del self.convex_A_distances[next_point]
                            del self.convex_B_distances[next_point]

        return oracle_calls
Example #18
0
box2 = [[0, 0],
        [1, 0],
        [1, 1],
        [0.5, 0.5],  # this point is in the center of the square
        [0, 1]]

ch1 = ConvexHull(points=box1, incremental=True)
print("ch1:")
summarize_hull_properties(ch1)

print('''\n\nCompared to the above, the following hull contains an extra point at 
0.5, 0.5. We can tell it is INSIDE the hull but not on its boundary, by any
of these:
A) adding that point did not increase the area.
B) That point is not included in the vertices.
C) Therefore, that point is also not an endpoint of any simplices.
''')
ch2 = ConvexHull(points=box2, incremental=True)
print("ch2:")
summarize_hull_properties(ch2)

print('\nNext, we add some outside points to the previous ConvexHulls and notice things change:')
ch1.add_points([[1.99, 0], [0, 2.0]])
print("ch1 is now:")
summarize_hull_properties(ch1)

ch2.add_points([[2.0, 0], [0, 2.000000001]])
print("ch2 is now:")
summarize_hull_properties(ch1)

Example #19
0
    def r( self, c, gpos, mpos=0.0):
        """
        Calculates the virtual distances between grid point locations and
        microphone locations or the origin. These virtual distances correspond
        to travel times of the sound along a ray that is traced through the
        medium.

        Parameters
        ----------
        c : float
            The speed of sound to use for the calculation.
        gpos : array of floats of shape (3, N)
            The locations of points in the beamforming map grid in 3D cartesian
            co-ordinates.
        mpos : array of floats of shape (3, M), optional
            The locations of microphones in 3D cartesian co-ordinates. If not
            given, then only one microphone at the origin (0, 0, 0) is
            considered.

        Returns
        -------
        array of floats
            The distances in a twodimensional (N, M) array of floats. If M==1, 
            then only a onedimensional array is returned.
        """
        if isscalar(mpos):
            mpos = array((0, 0, 0), dtype = float32)[:, newaxis]

        # the DE system
        def f1(t, y, v):
            x = y[0:3]
            s = y[3:6]
            vv, dv = v(x)
            sa = sqrt(s[0]*s[0]+s[1]*s[1]+s[2]*s[2])
            x = empty(6)
            x[0:3] = c*s/sa - vv # time reversal
            x[3:6] = dot(s, -dv.T) # time reversal
            return x

        # integration along a single ray
        def fr(x0, n0, rmax, dt, v, xyz, t):
            s0 = n0 / (c+dot(v(x0)[0], n0))
            y0 = hstack((x0, s0))
            oo = ode(f1)
            oo.set_f_params(v)
            oo.set_integrator('vode', 
                              rtol=1e-4, # accuracy !
                              max_step=1e-4*rmax) # for thin shear layer
            oo.set_initial_value(y0, 0)
            while oo.successful():
                xyz.append(oo.y[0:3])
                t.append(oo.t)
                if norm(oo.y[0:3]-x0)>rmax:
                    break
                oo.integrate(oo.t+dt)

        gs2 = gpos.shape[-1]
        gt = empty((gs2, mpos.shape[-1]))
        vv = self.ff.v
        NN = int(sqrt(self.N))
        for micnum, x0 in enumerate(mpos.T):
            xe = gpos.mean(1) # center of grid
            r = x0[:, newaxis]-gpos
            rmax = sqrt((r*r).sum(0).max()) # maximum distance
            nv = spiral_sphere(self.N, self.Om, b=xe-x0)
            rstep = rmax/sqrt(self.N)
            rmax += rstep
            tstep = rstep/c
            xyz = []
            t = []
            lastind = 0
            for i, n0 in enumerate(nv.T):
                fr(x0, n0, rmax, tstep, vv, xyz, t)
                if i and i % NN == 0:
                    if not lastind:
                        dd = ConvexHull(vstack((gpos.T, xyz)), incremental=True)
                    else:
                        dd.add_points(xyz[lastind:], restart=True)
                    lastind = len(xyz)
                    # ConvexHull includes grid if no grid points on hull
                    if dd.simplices.min()>=gs2:
                        break
            xyz = array(xyz)
            t = array(t)
            li = LinearNDInterpolator(xyz, t)
            gt[:, micnum] = li(gpos.T)
        if gt.shape[1] == 1:
            gt = gt[:, 0]
        return c*gt #return distance along ray