def CreatePenaltyEvaluator(config): penEval = EA.PenaltyEvaluator() objectiveSpecs = config["Objectives"] for spec in objectiveSpecs: name = spec["Name"] penaltyType = spec["Type"] weight = spec["Weight"] ref = spec["Reference"] exponent = spec["Exponent"] penalty = EA.Penalty(penaltyType, name, weight, ref, exponent=exponent) penEval.ObjectivePenalties.append(penalty) if not config.has_key("Constraints"): return penEval constraintSpecs = config["Constraints"] for spec in constraintSpecs: name = spec["Name"] penaltyType = spec["Type"] weight = spec["Weight"] ref = spec["Reference"] imposed = spec["Imposed"] exponent = spec["Exponent"] penalty = EA.Penalty(penaltyType, name, weight, ref, imposed=imposed, exponent=exponent) penEval.ConstraintPenalties.append(penalty) return penEval
def Start(self): pop = EA.Population() for i in range(5): indi = self.Chromosome.Clone() pop.append(indi) print pop self.ResEval.Evaluate(pop)
def __init__(self, spec): Method.__init__(self) self.Chromosome = Factory.Chromosome(Context.SimulationChain) self.ResEval = ChainResponseEvaluator() self.ResEval.Start() self.PenEval = Factory.CreatePenaltyEvaluator(spec) self.FitnessEval = EA.FitnessEvaluator() optiConfig = spec["OptimizerConfig"] popSize = optiConfig["PopulationSize"] self.OptimizerConfig = optiConfig postIterCallback = lambda gen, pop: self.PostIteration(gen, pop) self.BestExperiment = None if spec["Optimizer"] == "GA": self.Optimizer = EA.MonoObjectiveGA(self.Chromosome, self.ResEval, self.PenEval, self.FitnessEval, popSize, 2) self.Optimizer.PostIterationCallback = postIterCallback self.HistoryFile = open("%s.history.dat" % Context.Name, "w") elif spec["Optimizer"] == "SurrogateGA": Context.Database.Import(open(optiConfig["DOE"])) pop = Factory.PopulationFromPlan(self.Chromosome, Context.Database) self.Optimizer = EA.MonoObjectiveSurrogateGA( self.Chromosome, self.ResEval, self.PenEval, self.FitnessEval, pop) elif spec["Optimizer"] == "ParetoGA": self.Optimizer = EA.SPEA2(self.Chromosome, self.ResEval, self.PenEval, optiConfig["PopulationSize"], optiConfig["ArchiveSize"]) elif spec["Optimizer"] == "ParetoSurrogateGA": Context.Database.Import(open(optiConfig["DOE"])) pop = Factory.PopulationFromPlan(self.Chromosome, Context.Database) self.Optimizer = EA.ParetoSurrogateGA( self.Chromosome, self.ResEval, self.PenEval, pop, popSize, optiConfig["ArchiveSize"], optiConfig["Update"], optiConfig["UpdateTruncation"])
def Chromosome(chain): indi = EA.Individual() for param in chain.Parameters: name = param["Name"] valueType = param["Type"] minValue = param["Min"] maxValue = param["Max"] refValue = param["Ref"] p = EA.BoundedValue(name, valueType, refValue, refValue, minValue, maxValue) indi.DesignParameters.append(p) for resp in chain.Responses: name = resp["Name"] valueType = resp["Type"] r = EA.Value(name, valueType, 0.0) indi.Responses.append(r) return indi
Rules.fill_dict(users, train_user_label) train_user_collect = Rules.load_dict("train/topk2.txt") Rules.fill_dict(users, train_user_collect) train_item_reco = Rules.load_dict("train/item_based_user_dict.txt") train_user_reco = Rules.Similarity(train_similarity, train_user_item, train_topk4) train_item_action = Rules.load_dict("train/user_action.txt") Rules.fill_dict(users, train_item_action) train_user_matrix, train_pool_dict, train_right_dict = Rules.classification( train_notbuy, train_hottest, train_topview, train_user_reco, train_item_reco, users, train_user_label, train_user_item, train_item_action) train_weights = EA.calculateWeights(train_hottest, train_notbuy, train_topview, train_item_reco, train_user_reco, train_user_label) train_result = EA.EArecommend(train_hottest, train_notbuy, train_topview, train_item_reco, train_user_reco, train_weights) EA.judge(train_result, train_user_label) train_similarity = {} train_user_item = {} train_topk4 = {} train_notbuy = {} train_hottest = {} train_topview = {} train_user_reco = {} train_item_reco = {} #train_user_label = {}
# $Id: TestTorczonMeritFunction.py 87 2011-02-17 16:46:52Z kato $ import EA def Main(): case = EA.TestCases.Rosenbrock() chromosome = case.Chromosome() chromosome.Responses.append(EA.Value("TorczonMerit", "FLOAT")) resEval = EA.ResponseEvaluator(case) penaltyEval = EA.PenaltyEvaluator() penaltyEval.ObjectivePenalties = case.Objectives() penaltyEval.ConstraintPenalties = case.Constraints() fitnessEval = EA.FitnessEvaluator() print chromosome pop = EA.Population() for i in range(10): pop.append(chromosome.Clone()) pop.Randomize() resEval.Evaluate(pop) penaltyEval.Evaluate(pop) fitnessEval.Evaluate(pop) torczon = EA.TorczonMeritFunction(pop, chromosome, 1.0, "TorczonMerit")
def ind_test(): board = ea.erzeuge_individuum(numero) assert len(board.flat) == numero * numero
def PopulationFromPlan(chromosome, plan): pop = EA.Population() for exp in plan: pop.append(IndividualFromExperiment(chromosome, exp)) return pop
def main(): # pass ea = EA(POP_SIZE, GEN_NUM, GENE_LEN) best_window_size, best_num_units = ea.evo_algorithm() model, test_acc = train_model(best_window_size, best_num_units)
# $Id: TestDebConstraintFitness.py 87 2011-02-17 16:46:52Z kato $ import EA def Main(): case = EA.TestCases.RosenbrockUncomputable() resEval = EA.ResponseEvaluator(case) penaltyEval = EA.PenaltyEvaluator() penaltyEval.ObjectivePenalties = case.Objectives() penaltyEval.ConstraintPenalties = case.Constraints() fitnessEval = EA.FitnessEvaluator() pop = EA.Population() for i in range(10): pop.append(case.Chromosome()) pop.Randomize() resEval.Evaluate(pop) penaltyEval.Evaluate(pop) print pop fitnessEval.Evaluate(pop) pop.SortByFitness() print pop