예제 #1
0
파일: __init__.py 프로젝트: essxnce/nsga2
 def call(
     iproblem: IProblem, epochs: int, agent_count: int
 ) -> Generator[Tuple[np.ndarray, np.ndarray], None, None]:
     problem = Problem(iproblem)
     evolution = Evolution(problem, epochs, agent_count)
     for pop in evolution.evolve():
         xs = [x for x in map(lambda x: x.features, pop.fronts[0])]
         fs = [f for f in map(lambda x: x.objectives, pop.fronts[0])]
         # f_norms = [x for x in map(lambda x: x.normalized_objectives, pop.fronts[0])] # Originally here for debugging purposes
         yield np.array(xs), np.array(fs)
예제 #2
0
def Select(population, MSR, RV, BI_size, population_K ,nsol, new_msr, new_rv,new_bisize, new_K, generation,self_population,Zdt_definitions,PPlotter,PProblem,EEvolution,population_label,children_label):
    if (Zdt_definitions is None) and (PPlotter is None) and (PProblem is None) and (EEvolution is None):

        zdt_definitions = ZDT3Definitions()
        plotter = Plotter(zdt_definitions)
        problem = ZDT(zdt_definitions)
        evolution = Evolution(problem, 1, len(population))
        evolution.register_on_new_generation(plotter.plot_population_best_front)
        evolution.register_on_new_generation(collect_metrics)
    else:
        zdt_definitions=Zdt_definitions
        plotter=PPlotter
        problem=PProblem
        evolution=EEvolution


    new_pop,objectives,self_population,Final_label,K= evolution.evolve(population,MSR, RV, BI_size, population_K,nsol, new_msr, new_rv, new_bisize, new_K,generation,self_population,population_label,children_label)

    return new_pop,objectives,self_population,zdt_definitions,plotter,problem,evolution,Final_label,K
예제 #3
0
def optimizator(nGeneration=1000,
                nVariables=1,
                objectives=None,
                varRange=None,
                same_range=None,
                expand=False,
                nIndividuals=50):
    """Hàm tìm bộ tham số tối ưu để tối thiểu hóa các hàm `objectives`

    Keyword Arguments:
        nGeneration {int} -- Số lượng thế hệ muốn chạy tìm tối ưu (default: {1000})
        nVariables {int} -- Số lượng biến trong hàm tối ưu (default: {1})
        objectives {list} -- Danh sách các hàm cần tối ưu (default: {None})
        varRange {list} -- Danh sách các tuple là khoảng giá trị của các biến trong các hàm `objectives` (default: {None})

    Returns:
        list, list -- danh sách các giá trị của biến khiến các hàm `objectives` đạt tối thiểu, danh sách các giá trị tối thiểu của các hàm
        float -- time
    """

    ##############################
    # Định nghĩa problem cần minimize
    ##############################
    problem = Problem(num_of_variables=nVariables,
                      objectives=objectives,
                      variables_range=varRange,
                      expand=expand,
                      same_range=same_range)

    ##############################
    # Tiến hành minimize
    ##############################
    evo = Evolution(problem,
                    num_of_generations=nGeneration,
                    num_of_individuals=nIndividuals)

    start = time.time()

    evol = evo.evolve()

    end = time.time()

    return evol[0].features, evol[0].objectives, end - start
예제 #4
0

def f1(x):
    s = 0
    for i in range(len(x) - 1):
        s += -10 * math.exp(-0.2 * math.sqrt(x[i]**2 + x[i + 1]**2))
    return s


def f2(x):
    s = 0
    for i in range(len(x)):
        s += abs(x[i])**0.8 + 5 * math.sin(x[i]**3)
    return s


problem = Problem(num_of_variables=3,
                  objectives=[f1, f2],
                  variables_range=[(-5, 5)],
                  same_range=True,
                  expand=False)
evo = Evolution(problem, mutation_param=20)
func = [i.objectives for i in evo.evolve()]

function1 = [i[0] for i in func]
function2 = [i[1] for i in func]
plt.xlabel('Function 1', fontsize=15)
plt.ylabel('Function 2', fontsize=15)
plt.scatter(function1, function2)
plt.show()
예제 #5
0
    problem = Problem(num_of_variables=5,
                      objectives=[gmean_inv, dem_fpr],
                      variables_range=[
                          (min_range_criterion, max_range_criterion),
                          (min_range_max_depth, max_range_max_depth),
                          (min_range_samples_split, max_range_samples_split),
                          (min_range_leaf_nodes, max_range_leaf_nodes),
                          (min_range_class_weight, max_range_class_weight)
                      ],
                      individuals_df=pd.DataFrame(),
                      num_of_generations=generations,
                      num_of_individuals=individuals,
                      dataset_name=dataset,
                      variable_name=variable,
                      seed=set_seed)

    print("------------RUN:", run)

    evo = Evolution(problem,
                    evolutions_df=pd.DataFrame(),
                    dataset_name=dataset,
                    protected_variable=variable,
                    num_of_generations=generations,
                    num_of_individuals=individuals)
    pareto = evo.evolve()

    first = True
    for p in pareto:
        problem.test_and_save(p, first, problem.seed)
        first = False
예제 #6
0
    print("Generation: {}".format(generation_num + 1))


collected_metrics = {}


def collect_metrics(population, generation_num):
    pareto_front = population.fronts[0]
    metrics = ZDT3Metrics()
    hv = metrics.HV(pareto_front)
    hvr = metrics.HVR(pareto_front)
    collected_metrics[generation_num] = hv, hvr


pbr_props.show_info = True

pbr_definitions = PBRDefinitions()
plotter = Plotter(pbr_definitions)
problem = PBRCosts(pbr_definitions)
evolution = Evolution(problem, 50, 50)
evolution.register_on_new_generation(plotter.plot_population_best_front)
evolution.register_on_new_generation(print_generation)
evolution.register_on_new_generation(collect_metrics)
pareto_front = evolution.evolve()
plotter.write_report(pareto_front)

pbr_engine.close_aspen()

# plotter.plot_x_y(collected_metrics.keys(), map(lambda (hv, hvr): hvr, collected_metrics.values()), 'generation', 'HVR', 'HVR metric for PBR problem', 'hvr-pbr_problem')
print("--- Whole run: %s seconds ---" % (time.time() - start_time))
예제 #7
0
    def optimize(self,predictions,run_day,C=0,Q=0.6,Q_rfa=1,num_of_generations=250, num_of_individuals=200, num_of_tour_particips=10,tournament_prob=1, p_c=0.5,p_m=0.5,p_risked=0.8,full=True,policy='return'):
        
        """optimizes the portfolio following constraints, predictions and historical risk

            Parameters
            ----------
            predictions : array
                array of optimal portfolios
                
            run_day : datetime
                array of corresponfing portfolios risks
                
            C : int
                array of corresponfing portfolios returns
                
            Q : float
                maximal quantity to be invested in a risky asset
                
            Q_rfa : sloat
                maximal quantity to be invested in the risk free asset
                
            num_of_generations : int
                number of NSGAII generations 
                
            num_of_individuals : int
                NSGAII population 
                
            num_of_tour_particips : int
                NSGAII tournament participants 
                
            tournament_prob : float
                paramters in binary selection tournament (not used for now)
                
            p_c : float
                crossover probability 
                
            p_m : float
                mutaion probability
                
            p_risked : float
                probability of doing the crossover on the risky assets
                
            full : boolean
                True: cross over on the whole portfolio , False : cross over on a part of the portfolio
                
            policy : str
                selection policy


            Returns
            -------
            array
                the selected optimal portfolio
        """
        
        L=[]
        B=[]
        
        all_real_returns=self.get_all_real_returns()
        
        cov=all_real_returns[all_real_returns.index<=run_day].cov().values


        def f1(x):
            return self.risk_portfolio(x,cov)

        def f2(x):
            return self.return_portfolio(x,predictions)

        portfolio_size=self.N
        Q=Q
        Q_rfa=Q_rfa
        if(C==0):
            C=portfolio_size

        problem = Problem(portfolio_size=portfolio_size, objectives=[f1, f2],Q=Q,Q_rfa=Q_rfa,C=C)
        evo = Evolution(problem, num_of_generations, num_of_individuals, num_of_tour_particips,tournament_prob, p_c,p_m,p_risked,full=True)
        solutions=evo.evolve()
        func = [i.objectives for i in solutions]
        function1 = [i[0] for i in func]
        function2 = [i[1] for i in func]

        plt.scatter(function1,function2)
        plt.title('Paret Set of Optimar Portfolios')
        plt.xlabel('Risk')
        plt.ylabel('Return')

        plt.savefig(self.portfolios_path+'/front_'+str(run_day)+'.png')


        selected_portfolio=self.choose_portfolio(solutions,function1,function2,policy,L,B)
        
        with open(self.portfolios_path+'/portfolio_'+str(run_day)+'.p', 'wb') as f:
            pickle.dump(selected_portfolio, f)
        
        
        return selected_portfolio
예제 #8
0
from nsga2.problem import Problem
from nsga2.evolution import Evolution
import matplotlib.pyplot as plt


def f1(x):
    return x**2


def f2(x):
    return (x - 2)**2


problem = Problem(num_of_variables=1,
                  objectives=[f1, f2],
                  variables_range=[(-55, 55)])
evo = Evolution(problem)
evol = evo.evolve()
func = [i.objectives for i in evol]

function1 = [i[0] for i in func]
function2 = [i[1] for i in func]
plt.xlabel('Function 1', fontsize=15)
plt.ylabel('Function 2', fontsize=15)
plt.scatter(function1, function2)
plt.show()
예제 #9
0
파일: main.py 프로젝트: fedoracy/nsga2
def print_generation(population, generation_num):
    print("Generation: {}".format(generation_num))

def print_metrics(population, generation_num):
    pareto_front = population.fronts[0]
    metrics = ZDT3Metrics()
    hv = metrics.HV(pareto_front)
    hvr = metrics.HVR(pareto_front)
    print("HV: {}".format(hv))
    print("HVR: {}".format(hvr))

collected_metrics = {}
def collect_metrics(population, generation_num):
    pareto_front = population.fronts[0]
    metrics = ZDT3Metrics()
    hv = metrics.HV(pareto_front)
    hvr = metrics.HVR(pareto_front)
    collected_metrics[generation_num] = hv, hvr

zdt_definitions = ZDT3Definitions()
plotter = Plotter(zdt_definitions)
problem = ZDT(zdt_definitions)
evolution = Evolution(problem, 200, 200)
evolution.register_on_new_generation(plotter.plot_population_best_front)
evolution.register_on_new_generation(print_generation)
evolution.register_on_new_generation(print_metrics)
evolution.register_on_new_generation(collect_metrics)
pareto_front = evolution.evolve()

plotter.plot_x_y(collected_metrics.keys(), map(lambda (hv, hvr): hvr, collected_metrics.values()), 'generation', 'HVR', 'HVR metric for ZDT3 problem', 'hvr-zdt3')