Beispiel #1
0
    def hypervolume(self, ref_point):
        """Calculate hypervolume. Uses package pygmo. Add checks to prevent errors.

        Parameters
        ----------
        ref_point

        Returns
        -------

        """
        non_dom = self.non_dom
        if not isinstance(ref_point, (Sequence, np.ndarray)):
            num_obj = non_dom.shape[1]
            ref_point = [ref_point] * num_obj
        non_dom = non_dom[np.all(non_dom < ref_point, axis=1), :]
        hyp = hv(non_dom)
        self.hyp = hyp.compute(ref_point)
        return self.hyp
                        def parallel_execute(run, path_to_file):

                            if metric is 'IGD':
                                path_to_file = path_to_file + '/Run_' + str(run) + '_NDS'
                                infile = open(path_to_file, 'rb')

                                results_data = pickle.load(infile)
                                infile.close()
                                non_dom_front = results_data["actual_objectives_nds"]
                                non_dom_surr = results_data["surrogate_objectives_nds"]
                                # print(np.shape(non_dom_surr))
                                # print((np.max(non_dom_front,axis=0)))
                                solution_ratio = np.shape(non_dom_front)[0] / np.shape(non_dom_surr)[0]
                                return [igd(pareto_front, non_dom_front), solution_ratio]

                            else:
                                if problem_testbench is 'DDMOPP':
                                    soln_all = []
                                    if mode != 0:
                                        path_to_file1 = path_to_file + '/Run_' + str(run + 1) + '_surr_all'
                                        with open(path_to_file1, 'r') as f:
                                            reader = csv.reader(f)
                                            for line in reader: soln_all.append(line)
                                    else:
                                        path_to_file1 = path_to_file + '/Run_' + str(run) + '_SOLN'
                                        infile = open(path_to_file1, 'rb')
                                        results_data = pickle.load(infile)
                                        infile.close()
                                        soln_all = results_data["actual_objectives_nds"]
                                        print(np.shape(soln_all))
                                        soln_all = np.tile(soln_all, (400, 1))

                                    soln_all = np.array(soln_all, dtype=np.float32)
                                    r0=np.ones(obj)
                                    r1=np.ones(obj)*-1
                                    dx=distance.euclidean(r0,r1)
                                    ref=np.ones(obj)*dx
                                    #ref = [2] * obj
                                    print(ref)

                                else:
                                    """
                                    path_to_file1 = path_to_file + '/Run_' + str(run + 1) + '_soln'
                                    soln_all = []
                                    with open(path_to_file1, 'r') as f:
                                        reader = csv.reader(f)
                                        for line in reader: soln_all.append(line)
                                    soln_all = np.array(soln_all, dtype=np.float32)
                                    """
                                    soln_all = []
                                    if mode != 0:
                                        path_to_file1 = path_to_file + '/Run_' + str(run + 1) + '_surr_all'
                                        with open(path_to_file1, 'r') as f:
                                            reader = csv.reader(f)
                                            for line in reader: soln_all.append(line)
                                    else:
                                        path_to_file1 = path_to_file + '/Run_' + str(run) + '_NDS'
                                        infile = open(path_to_file1, 'rb')
                                        results_data = pickle.load(infile)
                                        infile.close()
                                        soln_all = results_data["actual_objectives_nds"]
                                        print(np.shape(soln_all))
                                        soln_all = np.tile(soln_all, (400, 1))

                                    soln_all = np.array(soln_all, dtype=np.float32)
                                    ref = hv_ref[prob][str(obj)]

                                # print(actual_objectives_nds)
                                hv_iter = []
                                print(np.shape(soln_all))
                                max_iter = np.min((np.shape(soln_all)[0], int(f_eval_limit)))
                                f_evals_per = int(np.shape(soln_all)[0] / f_iters)
                                # for f_iter in range(int(max_iter / f_evals_per)):
                                for f_iter in range(f_iters):
                                    # soln_all_temp = soln_all[f_iter * f_evals_per:(f_iter + 1) * f_evals_per, :]
                                    if f_iter == 0:
                                        soln_all_temp = soln_all[0:100, :]
                                    else:
                                        soln_all_temp = soln_all[f_iter * f_evals_per:(f_iter + 1) * f_evals_per, :]
                                    if np.shape(soln_all_temp)[0] > 1:
                                        non_dom_front = ndx(soln_all_temp)
                                        soln_iter = soln_all_temp[non_dom_front[0][0]]
                                    else:
                                        soln_iter = soln_all_temp.reshape(1, obj)
                                    hyp = hv(soln_iter)
                                    hyp_temp = hyp.compute(ref)
                                    """
                                    if f_iter == 0 and mode == 0:
                                        hyp_start[run] = hyp_temp
                                        print(hyp_start[run])
                                    if f_iter == 0 and mode == 9:
                                        hyp_temp = hyp_start[run]
                                        print(hyp_temp)
                                    print(hyp_start)
                                    """
                                    #    hv_iter = hyp_temp
                                    # else:
                                    #   hv_iter = np.vstack((hv_iter, hyp_temp))
                                    hv_iter = np.append(hv_iter, hyp_temp)
                                # print(hv_iter)
                                return hv_iter
Beispiel #3
0
                        def parallel_execute(run, path_to_file):

                            if metric == 'IGD':
                                path_to_file = path_to_file + '/Run_' + str(
                                    run) + '_NDS'
                                infile = open(path_to_file, 'rb')
                                results_data = pickle.load(infile)
                                infile.close()
                                non_dom_front = results_data[
                                    "actual_objectives_nds"]
                                non_dom_surr = results_data[
                                    "surrogate_objectives_nds"]
                                #print(np.shape(non_dom_surr))
                                #print((np.max(non_dom_front,axis=0)))
                                solution_ratio = np.shape(non_dom_front)[
                                    0] / np.shape(non_dom_surr)[0]
                                return [
                                    igd(pareto_front, non_dom_front),
                                    solution_ratio
                                ]

                            else:
                                if problem_testbench == 'DDMOPP':
                                    if mode == 9:
                                        path_to_file = path_to_file + '/Run_' + str(
                                            run + 1) + '_soln'
                                        actual_objectives_nds = []
                                        with open(path_to_file, 'r') as f:
                                            reader = csv.reader(f)
                                            for line in reader:
                                                actual_objectives_nds.append(
                                                    line)

                                    else:
                                        path_to_file = path_to_file + '/Run_' + str(
                                            run) + '_SOLN'
                                        infile = open(path_to_file, 'rb')
                                        results_data = pickle.load(infile)
                                        infile.close()
                                        if mode == 0:
                                            actual_objectives_nds = results_data[
                                                "actual_objectives_nds"]
                                        else:
                                            actual_objectives_nds = results_data[
                                                "obj_solns"]
                                    actual_objectives_nds = np.array(
                                        actual_objectives_nds,
                                        dtype=np.float32)
                                    r0 = np.ones(obj)
                                    r1 = np.ones(obj) * -1
                                    dx = distance.euclidean(r0, r1)
                                    ref = np.ones(obj) * dx
                                    #ref = [2]*obj

                                else:
                                    if mode == 9:
                                        path_to_file = path_to_file + '/Run_' + str(
                                            run + 1) + '_soln'
                                        actual_objectives_nds = []
                                        with open(path_to_file, 'r') as f:
                                            reader = csv.reader(f)
                                            for line in reader:
                                                actual_objectives_nds.append(
                                                    line)
                                        actual_objectives_nds = np.array(
                                            actual_objectives_nds,
                                            dtype=np.float32)
                                    else:
                                        path_to_file = path_to_file + '/Run_' + str(
                                            run) + '_NDS'
                                        infile = open(path_to_file, 'rb')
                                        results_data = pickle.load(infile)
                                        infile.close()
                                        actual_objectives_nds = results_data[
                                            "actual_objectives_nds"]
                                        #non_dom_surr = results_data["surrogate_objectives_nds"]

                                    ref = hv_ref[prob][str(obj)]

                                #print(actual_objectives_nds)
                                if np.shape(actual_objectives_nds)[0] > 1:
                                    non_dom_front = ndx(actual_objectives_nds)
                                    actual_objectives_nds = actual_objectives_nds[
                                        non_dom_front[0][0]]
                                else:
                                    actual_objectives_nds = actual_objectives_nds.reshape(
                                        1, obj)
                                print(np.shape(actual_objectives_nds))
                                solution_ratio = 0
                                hyp = hv(actual_objectives_nds)
                                hv_x = hyp.compute(ref)
                                #print(np.amax(actual_objectives_nds,axis=0))

                                #ax.scatter(actual_objectives_nds[:, 0], actual_objectives_nds[:, 1],actual_objectives_nds[:, 2])
                                #ax.scatter(actual_objectives_nds[:, 0], actual_objectives_nds[:, 1])

                                return [hv_x, solution_ratio]