コード例 #1
0
ファイル: slowRVEAtests.py プロジェクト: ppouyaa/pyRVEA
def smoothEvolve(problem, orig_point, first_ref, second_ref):
    """Evolves using RVEA with abrupt change of reference vectors."""
    pop = Population(problem, assign_type="empty", plotting=False)
    try:
        pop.evolve(slowRVEA, {
            "generations_per_iteration": 200,
            "iterations": 15
        })
    except IndexError:
        return pop.archive
    try:
        pop.evolve(
            slowRVEA,
            {
                "generations_per_iteration": 10,
                "iterations": 20,
                "old_point": orig_point,
                "ref_point": first_ref,
            },
        )
    except IndexError:
        return pop.archive
    try:
        pop.evolve(
            slowRVEA,
            {
                "generations_per_iteration": 10,
                "iterations": 20,
                "old_point": first_ref,
                "ref_point": second_ref,
            },
        )
    except IndexError:
        return pop.archive
    return pop.archive
コード例 #2
0
    def train(self):
        """Create a random population, evolve it and select a model based on selection."""
        pop = Population(
            self,
            assign_type="EvoDN2",
            pop_size=self.params["pop_size"],
            recombination_type=self.params["recombination_type"],
            crossover_type=self.params["crossover_type"],
            mutation_type=self.params["mutation_type"],
            plotting=self.params["plotting"],
        )

        pop.evolve(EA=self.params["training_algorithm"],
                   ea_parameters=self.ea_params)

        non_dom_front = pop.non_dominated()
        self.subnets, self.fitness = self.select(pop, non_dom_front,
                                                 self.params["selection"])
        self.non_linear_layer, _ = self.activation(self.subnets)
        self.linear_layer, *_ = self.calculate_linear(self.non_linear_layer)
コード例 #3
0
    def train(self):
        """Trains the networks and selects the best model from the non dominated front.

        """
        pop = Population(
            self,
            assign_type="EvoNN",
            pop_size=self.params["pop_size"],
            plotting=self.params["plotting"],
            recombination_type=self.params["recombination_type"],
            crossover_type=self.params["crossover_type"],
            mutation_type=self.params["mutation_type"],
        )
        pop.evolve(EA=self.params["training_algorithm"],
                   ea_parameters=self.ea_params)

        non_dom_front = pop.non_dominated()
        self.non_linear_layer, self.fitness = self.select(
            pop, non_dom_front, self.params["selection"])

        activated_layer, _ = self.activation(self.non_linear_layer)
        self.linear_layer, *_ = self.calculate_linear(activated_layer)
コード例 #4
0
    def train(self):
        """Trains the networks and selects the best model from the non dominated front.

        """

        # Minimize only error for first n generations before switching to bi-objective
        ea_params = {
            "generations_per_iteration": 1,
            "iterations": self.params["single_obj_generations"],
        }
        self.minimize = [True, False]

        print("Minimizing error for " +
              str(self.params["single_obj_generations"]) + " generations...")

        pop = Population(
            self,
            assign_type="BioGP",
            pop_size=self.params["pop_size"],
            plotting=self.params["plotting"],
            recombination_type=self.params["recombination_type"],
            crossover_type=self.params["crossover_type"],
            mutation_type=self.params["mutation_type"],
        )

        pop.evolve(EA=TournamentEA, ea_parameters=ea_params)

        # Switch to bi-objective (error, complexity)
        self.minimize = [True, True]
        pop.update_fitness()

        print("Switching to bi-objective mode")

        pop.evolve(EA=self.params["training_algorithm"],
                   ea_parameters=self.ea_params)

        non_dom_front = pop.non_dominated()
        self.linear_node, self.fitness = self.select(pop, non_dom_front,
                                                     self.params["selection"])
コード例 #5
0
                ANOVAMOPtest1SubpSurrogate.MaxIntOrder = MaxIntOrder,
                
                # Check if we also need to send Input, as the initial sample, to the solver
                
                name = "ANOVAMOPtest1SubpSurrogate"
                #k = 10
                numobj = len(ANOVAMOPtest1SubpSurrogate.SubProblemObjectiveIndices)
                numconst = 0
                numvar = len(ANOVAMOPtest1SubpSurrogate.SubProblemVariablesIndices)

                problem = ANOVAMOPtest1SubpSurrogate(name, numvar, numobj, numconst, )

                lattice_resolution = 4
                population_size = 105

                pop = Population(problem)

                # You can choose the solver (RVEA or NSGAIII)
                pop.evolve(NSGAIII)

                non_dom_index = pop.non_dominated() 
                
                xParetoTemp = pop.individuals[non_dom_index[0]]
                xsize = np.shape(xParetoTemp)
                
                fParetoTemp = pop.fitness[non_dom_index[0]]  # it seems that the folowing objective calculations are not necessary as they can be returned here
                xsize = np.shape(xParetoTemp)
                
                p1m[0] = xParetoTemp # p1m = p1 ... pm  (SubProblemsDecisionSpacePareto)
                f1m[0] = fParetoTemp   # f1m = f1 ... fm  (SubProblemsObjectiveSpacePareto)   
                
コード例 #6
0
ファイル: draft.py プロジェクト: bshavazipour/pyANOVAMOP
    def __init__(
        self,
        name=None,
        num_of_variables=None,
        num_of_objectives=None,
        num_of_constraints=0,
        upper_limits=1,
        lower_limits=0,
    ):
        """Pydocstring is ruthless.
        Args:
            name:
            num_of_variables:
            num_of_objectives:
            num_of_constraints:
            upper_limits:
            lower_limits:
        """
        super(testProblem, self).__init__(
            name,
            num_of_variables,
            num_of_objectives,
            num_of_constraints,
            upper_limits,
            lower_limits,
        )
        if name == "ZDT1":
            self.obj_func = zdt.ZDT1()
            self.lower_limits = self.obj_func.min_bounds
            self.upper_limits = self.obj_func.max_bounds
        elif name == "ZDT2":
            self.obj_func = zdt.ZDT2()
            self.lower_limits = self.obj_func.min_bounds
            self.upper_limits = self.obj_func.max_bounds
        elif name == "ZDT3":
            self.obj_func = zdt.ZDT3()
            self.lower_limits = self.obj_func.min_bounds
            self.upper_limits = self.obj_func.max_bounds
        elif name == "ZDT4":
            self.obj_func = zdt.ZDT4()
            self.lower_limits = self.obj_func.min_bounds
            self.upper_limits = self.obj_func.max_bounds
        elif name == "ZDT5":
            self.obj_func = zdt.ZDT5()
            self.lower_limits = self.obj_func.min_bounds
            self.upper_limits = self.obj_func.max_bounds
        elif name == "ZDT6":
            self.obj_func = zdt.ZDT6()
            self.lower_limits = self.obj_func.min_bounds
            self.upper_limits = self.obj_func.max_bounds
        elif name == "DTLZ1":
            self.obj_func = dtlz.DTLZ1(num_of_objectives, num_of_variables)
            self.lower_limits = self.obj_func.min_bounds
            self.upper_limits = self.obj_func.max_bounds
        elif name == "DTLZ2":
            self.obj_func = dtlz.DTLZ2(num_of_objectives, num_of_variables)
            self.lower_limits = self.obj_func.min_bounds
            self.upper_limits = self.obj_func.max_bounds
        elif name == "DTLZ3":
            self.obj_func = dtlz.DTLZ3(num_of_objectives, num_of_variables)
            self.lower_limits = 0
            self.upper_limits = 1
        elif name == "DTLZ4":
            self.obj_func = dtlz.DTLZ4(num_of_objectives, num_of_variables)
            self.lower_limits = self.obj_func.min_bounds
            self.upper_limits = self.obj_func.max_bounds
        elif name == "DTLZ5":
            self.obj_func = dtlz.DTLZ5(num_of_objectives, num_of_variables)
            self.lower_limits = self.obj_func.min_bounds
            self.upper_limits = self.obj_func.max_bounds
        elif name == "DTLZ6":
            self.obj_func = dtlz.DTLZ6(num_of_objectives, num_of_variables)
            self.lower_limits = self.obj_func.min_bounds
            self.upper_limits = self.obj_func.max_bounds
        elif name == "DTLZ7":
            self.obj_func = dtlz.DTLZ7(num_of_objectives, num_of_variables)
            self.lower_limits = self.obj_func.min_bounds
            self.upper_limits = self.obj_func.max_bounds
            
        elif name == "BS1":
            self.obj_func = P_BS1(Operation,Input)
            self.lower_limits = self.obj_func.min_bounds
            self.upper_limits = self.obj_func.max_bounds
            
        elif name == "Surrogate":
            self.obj_func = P_Surrogate((Operation,M,Input, SubProblemObjectiveIndices,SubProblemVariablesIndices,NumVar,Bounds,lb,ub,FixedIndices, FixedValues, model):
            self.lower_limits = self.obj_func.min_bounds
            self.upper_limits = self.obj_func.max_bounds
        

    def objectives(self, decision_variables) -> list:
        """Use this method to calculate objective functions.
        Args:
            decision_variables:
        """
        return self.obj_func(decision_variables)

    def constraints(self, decision_variables, objective_variables):
        """Calculate constraint violation.
        Args:
            decision_variables:
            objective_variables:
        """
        print("Error: Constraints not supported yet.")    
        
#--------------------------------------------------------------------------------    

from pyrvea.Population.Population import Population
from pyrvea.Problem.baseProblem import baseProblem
from pyrvea.EAs.RVEA import RVEA
from pyrvea.EAs.NSGAIII import NSGAIII
from optproblems import dtlz


class newProblem(baseProblem):
    """New problem description."""
    
    #def __init__(
        #self,
        #name = None, # problem name
        #num_of_objectives[float] = 0, # M
        #data = [], # Sample, Input
        #SubProblemObjectiveIndices = [], 
        #SubProblemVariablesIndices = [],
        #num_of_variables = None, # NumVar
        #Sub_problem_lb = [],
        #Sub_problem_ub = [],
        #lower_limits = [],
        #upper_limits = [],
        #FixedIndices = [],
        #FixedValues = [],
        #model = [], # SurrogateDataInfo
        #num_of_constraints = 0, # Not supported yet
             
    #):
        """
        Args:
        name :
        num_of_objectives :
        Input :
        SubProblemObjectiveIndices : 
        SubProblemVariablesIndices :
        num_of_variables :
        Sub_problem_lb :
        Sub_problem_ub :
        lower_limits :
        upper_limits :
        FixedIndices :
        FixedValues :
        model :
        num_of_constraints : Not supported yet
        """
        
        #super().__init__() # call the baseProblem __init__ function
        
        
        #Sub_problem_lb = Bounds[0,:]
        #Sub_problem_ub = Bounds[1,:]
                
        #if name == "P_ALBERTO":
            #self.obj_func = P_ALBERTO(Input)
            #self.lower_limits = self.obj_func.min_bounds
            #self.upper_limits = self.obj_func.max_bounds
            
        #elif name == "P_Surrogate":
            #self.obj_func = P_Surrogate(M,Input, SubProblemObjectiveIndices,SubProblemVariablesIndices,NumVar,Bounds,lb,ub,FixedIndices, FixedValues, model):
           # self.lower_limits = self.obj_func.min_bounds
           # self.upper_limits = self.obj_func.max_bounds
            
        #else:
         #   raise Exception(Problem,'Not Exist'.format(x))
            

    
    
    def objectives(self, decision_variables) -> list:
        """Use this method to calculate objective functions.
        Args:
            decision_variables: a sample  
        """
        
        x = decision_variables
        
        numSample, self.num_of_variables = np.shape(np.matrix(x))
        
        epsilon = 0.1
                
        P1 = np.array([1, 1, 1])
        P2 = np.array([1, -1, -1])
        P3 = np.array([1, 1, -1])
        P4 = np.array([1, -1])
        P5 = np.array([-1, 1])
        
        Phi1 = ((x[0:3] - np.ones((numSample,1)) * P1) ** 2).sum(axis=1)
        Phi2 = ((x[0:3] - np.ones((numSample,1)) * P2) ** 2).sum(axis=1)
        Phi3 = ((x[0:3] - np.ones((numSample,1)) * P3) ** 2).sum(axis=1)
        Phi4 = ((x[3:5] - np.ones((numSample,1)) * P4) ** 2).sum(axis=1)
        Phi5 = ((x[3:5] - np.ones((numSample,1)) * P5) ** 2).sum(axis=1)
        
        Output = np.empty((numSample,5))
        Output[:,0] = Phi1 + epsilon * Phi4
        Output[:,1] = Phi2 + epsilon * Phi5
        Output[:,2] = Phi3 + epsilon * (Phi4 + Phi5)
        Output[:,3] = Phi4 + epsilon * Phi1
        Output[:,4] = Phi5 + epsilon * (Phi1 + Phi2)
        
                
        return Output #objective_values
  
name = "P_ALBERTO"
#k = 10
numobj = 5
numconst = 0
numvar = 5
problem = newProblem(name, numvar, numobj, numconst, )

lattice_resolution = 4
population_size = 105

pop = Population(problem)

pop.evolve(RVEA)

pop.non_dominated()

refpoint = 2
volume = 2 ** numobj
#print(pop.hypervolume(refpoint) / volume)



#------------------------------


    def P_ALBERTO(self, decision_variables):

        """    
            An example in section 4.1 of the ANOVA-MOP paper (p 3280)
        """
       
        x = decision_variables
        
        numSample, self.num_of_variables = np.shape(decision_variables)
        
        epsilon = 0.007 #0.1
        
        P1 = np.array([1, 1, 1])
        P2 = np.array([1, -1, -1])
        P3 = np.array([1, 1, -1])
        P4 = np.array([1, -1])
        P5 = np.array([-1, 1])
        
        Phi1 = ((x[:,0:3] - np.ones((numSample,1)) * P1) ** 2).sum(axis=1)
        Phi2 = ((x[:,0:3] - np.ones((numSample,1)) * P2) ** 2).sum(axis=1)
        Phi3 = ((x[:,0:3] - np.ones((numSample,1)) * P3) ** 2).sum(axis=1)
        Phi4 = ((x[:,3:5] - np.ones((numSample,1)) * P4) ** 2).sum(axis=1)
        Phi5 = ((x[:,3:5] - np.ones((numSample,1)) * P5) ** 2).sum(axis=1)
        
        Output = np.empty((numSample,5))
        Output[:,0] = Phi1 + epsilon * Phi4
        Output[:,4] = Phi2 + epsilon * Phi5
        Output[:,2] = Phi3 + epsilon * (Phi4 + Phi5)
        Output[:,3] = Phi4 + epsilon * Phi1
        Output[:,1] = Phi5 + epsilon * (Phi1 + Phi2)

        return Output



    def P_Surrogate(self, M,Input, SubProblemObjectiveIndices,SubProblemVariablesIndices,NumVar,Bounds,lb,ub,FixedIndices, FixedValues, model):
    
        """
        Surrogate problem, use in solving the subproblems via RVEA
        """
                           
        # MaxValue   = Bounds[0,:]
        # MinValue   = Bounds[1,:]
             
        NumPop = Input.shape[0]
        InputTemp = np.zeros((NumPop,len(SubProblemVariablesIndices) + len(FixedIndices)))
        InputTemp[:,FixedIndices] = np.matlib.repmat(FixedValues,NumPop,1)
        InputTemp[:,SubProblemVariablesIndices] = Input
        Input = MapSamples(InputTemp, np.vstack((-np.ones((1,len(lb))), np.ones((1,len(lb))))), np.vstack((lb,ub)))
        Output = np.zeros((NumPop,M))
        
        for objective in range(M):
            Output[:,objective] = SurrogatePrediction(Input,SurrogateDataInfo[SubProblemObjectiveIndices[objective]])

        return Output
    
    
    
    def SurrogatePrediction(x0, model): # model stored as Data includes e.g. md, check3, P, MaxIntOrder
        """
        Evaluate the objective functions for the given solution x0
        """
        md = model.md
        check3 = model.check3
        P = model.P
        MaxIntOrder = model.MaxIntOrder
        x0 = MultivariateLegendre2(x0,P,MaxIntOrder)
        x0 = x0[:,check3]
        Pred = np.matmul(x0,md) # check if the right product has been used here
    
        return Pred


    
    
    
    
    
    
    
    



    


#------------------------
    def objectives(self, decision_variables):
        """Objectives function to use in optimization.
        Parameters
        ----------
        decision_variables : ndarray
            The decision variables
        Returns
        -------
        objectives : ndarray
            The objective values
        """
        objectives = []
        for obj in self.y:
            objectives.append(
                self.models[obj][0].predict(decision_variables.reshape(1, -1))[0]
            )

        return objectives


"""
------------------------------------------------------------------------

draft from SubProblem
"""



def SubProblem_objFun(x0,SubProblemObjectiveIndices,SubProblemVariablesIndices,FixedIndices,FixedValues,VariableBounds,model):
    """
    #x=x0
    """
    
    numPop = x0.shape[0]
    frange = VariableBounds[1,:] - VariableBounds[0,:] # ub - lb
    np.delete(frange, FixedIndices,0) # remove a column FixedIndices
    x0 = ((x0+1) * np.matlib.repmat(frange,numPop,1)) / 2 + np.matlib.repmat(VariableBounds[0,SubProblemVariablesIndices],numPop,1)
    numVar = len(SubProblemVariablesIndices) #+len(FixedIndices)
    x = np.zeros((numPop,numVar))
    x[:,SubProblemVariablesIndices] = x0
    x[:,FixedIndices] = FixedValues
    numObj = len(SubProblemObjectiveIndices)
    y = np.zeros(1,numObj)
    cons = [] # check if it should not be a list
    
    for objective in range(numObj):
        y[objective] = SurrogatePrediction(x,model(SubProblemObjectiveIndices[objective]))


    return (y, cons)


"""
----------------------------------------------------------------------------

from CheckDecomposability

"""




#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat May 18 11:43:00 2019

@author: alberto

Hi Babooshka, 

if I am correct, you need the determination of the rows and columns of a matrix that can form a diagonal block after reordering the rows and columns. 

This seems that can be done with an algorithm finding CONNECTED COMPONENTS or CLUSTERS in a graph. 
The graph is a bipartite graph where the two sets of nodes are inputs and outputs. 

There is a package called PYTHON-IGRAPH that should do both things. It should suffice to rewrite the incidence matrix for the scope and then extract the components. 
If you like I can try if it works for our purposes. 

-----------

I implemented the solution with a simpler package (scipy) without extra installations. 

Here it is. 
Should be self explanatory. Refer to incidence matrices as in figure 1 of anovamop paper. 

Let me know if it is understandable and working as you expected. 

"""

from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import connected_components

bgraph = [ 
    [ 0, 1 , 0 ],
    [ 1, 0 , 1 ]
    ]

print("\n\nincidence matrix: \n")
for row in bgraph :
    print(row)
print("\n(rows: output variables, columns: input variables)\n")

nout = len(bgraph) # Number of columns
nin = len(bgraph[0]) # Number of rows
print("in: ",nin,"  out:",nout,"\n")

#  transformation in a suitable graph  with ninput + noutput nodes 

graph = []
for rigax in range(nin) : 
    row = []
    for x in range(nin) : row.append(0) 
    for y in range(nout) : row.append(bgraph[y][rigax])
    graph.append(row)
for rigay in range(nout) : 
    row = []
    for x in range(nin) : row.append(bgraph[rigay][x]) 
    for y in range(nout) : row.append(0)
    graph.append(row)



graph = csr_matrix(graph)
#print(graph)

n_components, labels = connected_components(csgraph=graph, directed=False, return_labels=True)
print("number of components found: ",n_components)
print("components legend : ",labels,"\n")

components = []
for com in range(n_components) : 
    invarscomp = []
    outvarscomp =[]
    for inv in range(nin) : 
        if labels[inv] == com : invarscomp.append(inv)
    for outv in range(nout) : 
        if labels[nin+outv] == com : outvarscomp.append(outv)
    components.append([invarscomp,outvarscomp])
    print(com+1,"° component, input vars: ",invarscomp,"  output vars: ",outvarscomp,"\n" ) 
print("components summary: ",components)



"""
コード例 #7
0

class newProblem(BaseProblem):
    """New problem description."""
    def objectives(self, decision_variables):
        return dtlz.DTLZ3(self.num_of_objectives,
                          self.num_of_variables)(decision_variables)


name = "DTLZ3"
k = 10
numobj = 3
numconst = 0
numvar = numobj + k - 1
problem = newProblem(name, numvar, numobj, numconst)

lattice_resolution = 4
population_size = 105

pop = Population(problem,
                 crossover_type="simulated_binary_crossover",
                 mutation_type="bounded_polynomial_mutation",
                 plotting=True)

pop.evolve(RVEA)

pop.non_dominated()
refpoint = 2
volume = 2**numobj
print(pop.hypervolume(refpoint) / volume)
コード例 #8
0
        Output[:, 4] = Phi5 + epsilon * (Phi1 + Phi2)

        return Output  #objective_values


name = "ANOVAMOPtest1"
#k = 10
numobj = 5
numconst = 0
numvar = 5
problem = ANOVAMOPtest1(name, numvar, numobj, numconst)

lattice_resolution = 4
population_size = 105

pop = Population(problem)
#pop = Population(
#    problem,
#    crossover_type="simulated_binary_crossover",
#    mutation_type="bounded_polynomial_mutation",
#)

pop.evolve(RVEA)

non_dom_index = pop.non_dominated()

xParetoTemp = pop.individuals[non_dom_index[0]]
xsize = np.shape(xParetoTemp)

fParetoTemp = pop.fitness[non_dom_index[0]]
xsize = np.shape(xParetoTemp)
#
# )

y_pred = problem.surrogates_predict(problem.data[problem.x])

problem.models["f1"][0].plot(y_pred[:, 0],
                             problem.data["f1"],
                             name="ZDT1_1000" + "f1")

problem.models["f2"][0].plot(y_pred[:, 1],
                             problem.data["f2"],
                             name="ZDT1_1000" + "f2")

# Optimize
# PPGA
pop_ppga = Population(problem)

ppga_parameters = {
    "prob_prey_move": 0.5,
    "prob_mutation": 0.5,
    "target_pop_size": 100,
    "kill_interval": 4,
    "iterations": 10,
    "predator_pop_size": 60,
    "generations_per_iteration": 10,
    "neighbourhood_radius": 5,
}

pop_ppga.evolve(EA=PPGA, ea_parameters=ppga_parameters)

pop_ppga.plot_pareto(name="Tests/" +