예제 #1
0
def generate_composite_front(*fronts):

    _fronts = np.vstack(fronts)

    cf = _fronts[nds(_fronts)[0][0]]

    return cf
예제 #2
0
 def non_dominated(self):
     """Fix this. check if nd2 and nds mean the same thing"""
     obj = self.objectives
     num_obj = obj.shape[1]
     if num_obj == 2:
         non_dom_front = nd2(obj)
     else:
         non_dom_front = nds(obj)
     if isinstance(non_dom_front, tuple):
         self.non_dom = self.objectives[non_dom_front[0][0]]
     elif isinstance(non_dom_front, np.ndarray):
         self.non_dom = self.objectives[non_dom_front]
     else:
         print("Non Dom error Line 285 in population.py")
     return non_dom_front
예제 #3
0
    def select(self, population, max_rank=20) -> list:
        """Of the population, individuals lower than max_rank are selected.
        Return indices of selected individuals.

        Parameters
        ----------
        population : Population
            Contains the current population and problem
            information.
        max_rank : int
            Select only individuals lower than max_rank

        Returns
        -------
        list
            List of indices of individuals to be selected.
        """
        # Calculating fronts and ranks
        _, _, _, rank = nds(population.fitness)
        selection = np.nonzero(rank > max_rank)
        return selection[0]
    def do(self, pop: Population, vectors: ReferenceVectors,
           reference_point: np.ndarray) -> List[int]:
        """Select individuals for mating for NSGA-III.

        Parameters
        ----------
        pop : Population
            The current population.
        vectors : ReferenceVectors
            Class instance containing reference vectors.

        Returns
        -------
        List[int]
            List of indices of the selected individuals
        """
        ref_dirs = vectors.values_planar
        fitness = np.asarray([
            scalar(pop.fitness, reference_point)
            for scalar in self.scalarization_methods
        ]).T
        # Calculating fronts and ranks
        fronts, dl, dc, rank = nds(fitness)
        non_dominated = fronts[0]
        fmin = np.amin(fitness, axis=0)
        self.ideal = np.amin(np.vstack((self.ideal, fmin)), axis=0)

        # Calculating worst points
        self.worst_fitness = np.amax(np.vstack((self.worst_fitness, fitness)),
                                     axis=0)
        worst_of_population = np.amax(fitness, axis=0)
        worst_of_front = np.max(fitness[non_dominated, :], axis=0)
        self.extreme_points = self.get_extreme_points_c(
            fitness[non_dominated, :],
            self.ideal,
            extreme_points=self.extreme_points)
        nadir_point = self.get_nadir_point(
            self.extreme_points,
            self.ideal,
            self.worst_fitness,
            worst_of_population,
            worst_of_front,
        )

        # Finding individuals in first 'n' fronts
        selection = np.asarray([], dtype=int)
        for front_id in range(len(fronts)):
            if len(np.concatenate(fronts[:front_id + 1])) < self.n_survive:
                continue
            else:
                fronts = fronts[:front_id + 1]
                selection = np.concatenate(fronts)
                break

        F = fitness[selection]

        last_front = fronts[-1]

        # Selecting individuals from the last acceptable front.
        if len(selection) > self.n_survive:
            niche_of_individuals, dist_to_niche = self.associate_to_niches(
                F, ref_dirs, self.ideal, nadir_point)
            # if there is only one front
            if len(fronts) == 1:
                n_remaining = self.n_survive
                until_last_front = np.array([], dtype=np.int)
                niche_count = np.zeros(len(ref_dirs), dtype=np.int)

            # if some individuals already survived
            else:
                until_last_front = np.concatenate(fronts[:-1])
                id_until_last_front = list(range(len(until_last_front)))
                niche_count = self.calc_niche_count(
                    len(ref_dirs), niche_of_individuals[id_until_last_front])
                n_remaining = self.n_survive - len(until_last_front)

            last_front_selection_id = list(
                range(len(until_last_front), len(selection)))
            if np.any(selection[last_front_selection_id] != last_front):
                print("error!!!")
            selected_from_last_front = self.niching(
                fitness[last_front, :],
                n_remaining,
                niche_count,
                niche_of_individuals[last_front_selection_id],
                dist_to_niche[last_front_selection_id],
            )
            final_selection = np.concatenate(
                (until_last_front, last_front[selected_from_last_front]))
            if self.extreme_points is None:
                print("Error")
            if final_selection is None:
                print("Error")
        else:
            final_selection = selection
        return final_selection.astype(int)
def NSGAIII_select(
    fitness: list,
    ref_dirs: "ReferenceVectors",
    ideal_point: list = None,
    worst_point: list = None,
    extreme_points: list = None,
    n_survive: int = None,
):
    # Calculating fronts and ranks
    fronts, dl, dc, rank = nds(fitness)
    non_dominated = fronts[0]

    # Calculating worst points
    worst_of_population = np.amax(fitness, axis=0)
    worst_of_front = np.max(fitness[non_dominated, :], axis=0)
    extreme_points = get_extreme_points_c(
        fitness[non_dominated, :], ideal_point, extreme_points=extreme_points
    )
    nadir_point = get_nadir_point(
        extreme_points, ideal_point, worst_point, worst_of_population, worst_of_front
    )

    # Finding individuals in first 'n' fronts
    selection = np.asarray([], dtype=int)
    for front_id in range(len(fronts)):
        if len(np.concatenate(fronts[: front_id + 1])) < n_survive:
            continue
        else:
            fronts = fronts[: front_id + 1]
            selection = np.concatenate(fronts)
            break

    F = fitness[selection]

    last_front = fronts[-1]

    # Selecting individuals from the last acceptable front.
    if len(selection) > n_survive:
        niche_of_individuals, dist_to_niche = associate_to_niches(
            F, ref_dirs, ideal_point, nadir_point
        )
        # if there is only one front
        if len(fronts) == 1:
            n_remaining = n_survive
            until_last_front = np.array([], dtype=np.int)
            niche_count = np.zeros(len(ref_dirs), dtype=np.int)

        # if some individuals already survived
        else:
            until_last_front = np.concatenate(fronts[:-1])
            id_until_last_front = list(range(len(until_last_front)))
            niche_count = calc_niche_count(
                len(ref_dirs), niche_of_individuals[id_until_last_front]
            )
            n_remaining = n_survive - len(until_last_front)

        last_front_selection_id = list(range(len(until_last_front), len(selection)))
        if np.any(selection[last_front_selection_id] != last_front):
            print("error!!!")
        selected_from_last_front = niching(
            fitness[last_front, :],
            n_remaining,
            niche_count,
            niche_of_individuals[last_front_selection_id],
            dist_to_niche[last_front_selection_id],
        )
        final_selection = np.concatenate(
            (until_last_front, last_front[selected_from_last_front])
        )
        if extreme_points is None:
            print('Error')
        if final_selection is None:
            print('Error')
    else:
        final_selection = selection
    return(final_selection.astype(int), extreme_points)
예제 #6
0
import dash_bootstrap_components as dbc

import utils.dash_reusable_components as drc
import dash_table
import plotly.express as ex
import plotly.graph_objects as go
import pandas as pd
import numpy as np
from pygmo import fast_non_dominated_sorting as nds

data = pd.read_csv("./data/car_data_processed.csv", header=0)

maxi = data.loc[0]
data = data.loc[1:]

front = data.loc[nds(data.values * maxi.values)[0][2]].reset_index(drop=True)
print(len(front))

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.FLATLY])

app.layout = html.Div(
    children=[
        # .container class is fixed, .container.scalable is scalable
        dbc.Row([dbc.Col(html.H1(children="Optimization Group"))]),
        dbc.Row([
            dbc.Col(
                children=html.Div([
                    dbc.Card([
                        html.H4(
                            "Researcher's Night Event",
                            className="card-title",
예제 #7
0
data = pd.read_csv("./data/Phone_dataset.csv", header=0)
details = pd.read_csv("./data/Phone_details.csv", header=0)

names = details.loc[0]

data = data.rename(columns=names)
details = details.rename(columns=names)

maxi = details.loc[1].astype(int)
details_on_card = details.loc[2].astype(int)

details_on_card = details.columns[details_on_card == 1]

sort_columns = details.columns[maxi != 0]
sort_data = data[sort_columns].values * maxi[sort_columns].values
front = data.loc[nds(sort_data)[0][0]].reset_index(drop=True)

numeric_cols = [
    attr for attr in data
    if data.dtypes[attr] == "int64" or data.dtypes[attr] == "float64"
]

other_cols = [attr for attr in data if data.dtypes[attr] == "object"]

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.LITERA])

app.layout = html.Div(
    children=[
        # .container class is fixed, .container.scalable is scalable
        dbc.Row([
            dbc.Col(
예제 #8
0
def ndx(data):
    return nds(data)