public void DeleteSubpopulation() { var init = new DatastoreInitializer(); init.Subpops = new CruiseDAL.V3.Models.SubPopulation[0]; var db = init.CreateDatabase(); var ds = new SubpopulationDataservice(db, init.CruiseID, init.DeviceID); var sg = init.SampleGroups[0]; var newSubpop = new Subpopulation { SubpopulationID = Guid.NewGuid().ToString(), SampleGroupCode = sg.SampleGroupCode, StratumCode = sg.StratumCode, SpeciesCode = init.Species[0], LiveDead = "L", }; ds.AddSubpopulation(newSubpop); ds.DeleteSubpopulation(newSubpop); var subPops = ds.GetSubpopulations(sg.StratumCode, sg.SampleGroupCode); subPops.Should().BeEmpty(); }
public void UpdateSubpopulation(Subpopulation subpopulation) { Database.Execute2( @"UPDATE Subpopulation SET LiveDead = @LiveDead WHERE SubpopulationID = @SubpopulationID;", subpopulation); }
public void AddSubpopulation(Subpopulation subpopulation) { subpopulation.SubpopulationID ??= Guid.NewGuid().ToString(); Database.Execute2( @"INSERT OR IGNORE INTO Species (SpeciesCode, CruiseID) VALUES (@SpeciesCode, @CruiseID); INSERT INTO Subpopulation ( CruiseID, SubpopulationID, StratumCode, SampleGroupCode, SpeciesCode, LiveDead, CreatedBy ) VALUES ( @CruiseID, @SubpopulationID, @StratumCode, @SampleGroupCode, @SpeciesCode, @LiveDead, @DeviceID );", new { CruiseID, subpopulation.SubpopulationID, subpopulation.StratumCode, subpopulation.SampleGroupCode, subpopulation.SpeciesCode, subpopulation.LiveDead, DeviceID, }); }
/** Updates the CMA-ES distribution given the current population, then * replaces the population with new samples generated from the distribution. * Returns the revised population. */ public override Population BreedPopulation(IEvolutionState state) { Population pop = state.Population; for (int i = 0; i < pop.Subpops.Count; i++) { Subpopulation subpop = pop.Subpops[i]; if (!(subpop.Species is CMAESSpecies)) // uh oh { state.Output.Fatal("To use CMAESBreeder, subpopulation " + i + " must contain a CMAESSpecies. But it contains a " + subpop.Species); } CMAESSpecies species = (CMAESSpecies)subpop.Species; // update distribution[i] for subpop species.UpdateDistribution(state, subpop); // overwrite individuals IList <Individual> inds = subpop.Individuals; for (int j = 0; j < inds.Count; j++) { inds[j] = species.NewIndividual(state, 0); } } return(pop); }
public void BreedSubpop(IEvolutionState state, Subpopulation subpop, int index) { BreedingSource bp = (BreedingSource)subpop.Species.Pipe_Prototype; if (!StubsFilled[index]) { bp.FillStubs(state, null); } StubsFilled[index] = true; bp.PrepareToProduce(state, index, 0); // maybe resize? IList <Individual> newIndividuals = null; int newlen = subpop.Individuals.Count; if (ExpandedSubpopSize[index] != V_SUBPOP_NOT_RESIZED) { newlen = ExpandedSubpopSize[index]; } newIndividuals = new List <Individual>(); IList <Individual> individuals = subpop.Individuals; int len = individuals.Count; if (Elite[index]) { // We need to do some elitism: we put the BEST individual in the first slot Individual best = individuals[0]; for (int i = 1; i < len; i++) { Individual ind = individuals[i]; if (ind.Fitness.BetterThan(best.Fitness)) { best = ind; } } newIndividuals.Add(best); } // start breedin'! while (newIndividuals.Count < newlen) { // we don't allocate a hash table every time, so we pass in null bp.Produce(1, newlen - newIndividuals.Count, index, newIndividuals, state, 0, null); } subpop.Individuals = newIndividuals; bp.FinishProducing(state, index, 0); }
public void UpsertFixCNTTallyPopulation(Subpopulation subpopulation) { Database.Execute2( @"INSERT INTO FixCNTTallyPopulation ( CruiseID, StratumCode, SampleGroupCode, SpeciesCode, LiveDead, IntervalSize, Min, Max, CreatedBy ) VALUES ( @CruiseID, @StratumCode, @SampleGroupCode, @SpeciesCode, @LiveDead, @IntervalSize, @Min, @Max, @DeviceID ) ON CONFLICT (CruiseID, StratumCode, SampleGroupCode, SpeciesCode, LiveDead) DO UPDATE SET IntervalSize = @IntervalSize, Min = @Min, Max = @Max, ModifiedBy = @DeviceID WHERE CruiseID = @CruiseID AND StratumCode = @StratumCode AND SampleGroupCode = @SampleGroupCode AND SpeciesCode = @SpeciesCode AND LiveDead = @LiveDead;", new { CruiseID, subpopulation.StratumCode, subpopulation.SampleGroupCode, subpopulation.SpeciesCode, subpopulation.LiveDead, subpopulation.IntervalSize, subpopulation.Min, subpopulation.Max, DeviceID, }); }
/** * This method have three major part, first identify the best indiviudal, * and then call updateMostPromisingArea(...) to construct a hyperbox around * this individual. At last, sampled a new population from the hyperbox and * take the none redundant samples and return it. */ public override Population BreedPopulation(IEvolutionState state) { Population pop = state.Population; for (int i = 0; i < pop.Subpops.Count; i++) { Subpopulation subpop = pop.Subpops[i]; if (!(subpop.Species is DOVSSpecies)) // uh oh { state.Output.Fatal("To use DOVSBreeder, subpopulation " + i + " must contain a DOVSSpecies. But it contains a " + subpop.Species); } DOVSSpecies species = (DOVSSpecies)(subpop.Species); // we assume backTrackingTest is always false. // Thus we combine activeSolution and Sk (individuals) to // identify the optimal species.FindBestSample(state, subpop); // Right now activeSolutions only has A_{k-1}, need to combine S_k for (int j = 0; j < subpop.Individuals.Count; j++) { species.ActiveSolutions.Add(subpop.Individuals[i]); } // Ak and bk will have all the constraints, including original // problem formulation and MPR // A b are original problem formulation constraints // activeSolutions will then have the indices for those solutions // already visited and define MPR // excluding current best solution // update MPA species.UpdateMostPromisingArea(state); // sample from MPA IList <Individual> candidates = species.MostPromisingAreaSamples(state, subpop.InitialSize); // get Sk for evaluation IList <Individual> Sk = species.UniqueSamples(state, candidates); // update the individuals subpop.Individuals = Sk; } return(pop); }
/** * To find the best sample for each generation, we need to go through each * individual in the current population, and also best individual and * individuals in actionSolutions. These three type of individuals are * exactly the individuals evaluated in DOVSEvaluator. */ public void FindBestSample(IEvolutionState state, Subpopulation subpop) { // clear Ek Ek.Clear(); IList <Individual> individuals = subpop.Individuals; foreach (Individual i in individuals) { Ek.Add(i); } foreach (Individual i in ActiveSolutions) { Ek.Add(i); } Ek.Add(Visited[OptimalIndex]); OptimalIndex = FindOptimalIndividual(Ek); }
void PossiblyRestart(IEvolutionState state) { Countdown--; Subpopulation currentSubp = null; // time to restart! if (Countdown == 0) { state.Output.Message("Restarting the population prior to evaluating generation " + state.Generation); // for each subpopulation foreach (var t in state.Population.Subpops) { currentSubp = t; bool temp = currentSubp.LoadInds; // disable LoadInds so we generate candidates randomly currentSubp.LoadInds = false; currentSubp.Populate(state, 0); currentSubp.LoadInds = temp; } ResetClock(state); } }
/** * This method simply call breedNewPopulation method in NEATSpecies,where * all the critical work in done. */ public override Population BreedPopulation(IEvolutionState state) { Population pop = state.Population; for (int i = 0; i < pop.Subpops.Count; i++) { Subpopulation subpop = pop.Subpops[i]; if (!(subpop.Species is NEATSpecies)) // uh oh { state.Output.Fatal("To use NEATSpecies, subpopulation " + i + " must contain a NEATSpecies. But it contains a " + subpop.Species); } NEATSpecies species = (NEATSpecies)subpop.Species; species.BreedNewPopulation(state, i, 0); } return(pop); }
/** Revises the CMA-ES distribution to reflect the current fitness results in the provided subpopulation. */ public void UpdateDistribution(IEvolutionState state, Subpopulation subpop) { // % Sort by fitness and compute weighted mean into xmean // [arfitness, arindex] = sort(arfitness); % minimization // xmean = arx(:,arindex(1:mu))*weights; % recombination % Eq.39 // counteval += lambda; // only need partial sort? ((List <Individual>)subpop.Individuals).Sort(); SimpleMatrixD artmp = new SimpleMatrixD(GenomeSize, mu); SimpleMatrixD xold = xmean; xmean = new SimpleMatrixD(GenomeSize, 1); for (int i = 0; i < mu; i++) { DoubleVectorIndividual dvind = (DoubleVectorIndividual)subpop.Individuals[i]; // won't modify the genome SimpleMatrixD arz = new SimpleMatrixD(GenomeSize, 1, true, dvind.genome); arz = (arz.minus(xold).divide(sigma)); for (int j = 0; j < GenomeSize; j++) { xmean.set(j, 0, xmean.get(j, 0) + weights[i] * dvind.genome[j]); artmp.set(j, i, arz.get(j, 0)); } } // % Cumulation: Update evolution paths SimpleMatrixD y = xmean.minus(xold).divide(sigma); SimpleMatrixD bz = invsqrtC.mult(y); SimpleMatrixD bz_scaled = bz.scale(Math.Sqrt(cs * (2.0 - cs) * mueff)); ps = ps.scale(1.0 - cs).plus(bz_scaled); double h_sigma_value = ((ps.dot(ps) / (1.0 - Math.Pow(1.0 - cs, 2.0 * (state.Generation + 1)))) / GenomeSize); int hsig = (h_sigma_value < (2.0 + (4.0 / (GenomeSize + 1)))) ? 1 : 0; SimpleMatrixD y_scaled = y.scale(hsig * Math.Sqrt(cc * (2.0 - cc) * mueff)); pc = pc.scale(1.0 - cc).plus(y_scaled); // % Adapt covariance matrix C c = c.scale(1.0 - c1 - cmu); c = c.plus(pc.mult(pc.transpose()).plus(c.scale((1.0 - hsig) * cc * (2.0 - cc))).scale(c1)); c = c.plus((artmp.mult(SimpleMatrixD.diag(weights).mult(artmp.transpose()))).scale(cmu)); // % Adapt step-size sigma sigma = sigma * Math.Exp((cs / damps) * (ps.normF() / chiN - 1.0)); // % Update B and D from C if ((state.Generation - lastEigenDecompositionGeneration) > 1.0 / ((c1 + cmu) * GenomeSize * 10.0)) { lastEigenDecompositionGeneration = state.Generation; // make sure the matrix is symmetric (it should be already) // not sure if this is necessary for (int i = 0; i < GenomeSize; i++) { for (int j = 0; j < i; j++) { c.set(j, i, c.get(i, j)); } } // this copy gets modified by the decomposition DMatrixRMaj copy = c.copy().getMatrix(); EigenDecomposition <DMatrixRMaj> eig = DecompositionFactory_DDRM.eig(GenomeSize, true, true); if (eig.decompose(copy)) { SimpleMatrixD dinv = new SimpleMatrixD(GenomeSize, GenomeSize); for (int i = 0; i < GenomeSize; i++) { double eigrt = Math.Sqrt(eig.getEigenValue(i).real); d.set(i, i, eigrt); dinv.set(i, i, 1 / eigrt); CommonOps_DDRM.insert(eig.getEigenVector(i), b.getMatrix(), 0, i); } invsqrtC = b.mult(dinv.mult(b.transpose())); CommonOps_DDRM.mult(b.getMatrix(), d.getMatrix(), bd); } else { state.Output.Fatal("CMA-ES eigendecomposition failed. "); } } CommonOps_DDRM.scale(sigma, bd, sbd); // % Break, if fitness is good enough or condition exceeds 1e14, better termination methods are advisable // if arfitness(1) <= stopfitness || max(D) > 1e7 * min(D) // break; // end if (useAltTermination && CommonOps_DDRM.elementMax(d.diag().getMatrix()) > 1e7 * CommonOps_DDRM.elementMin(d.diag().getMatrix())) { state.Evaluator.SetRunCompleted("CMAESSpecies: Stopped because matrix condition exceeded limit."); } }
public void DeleteSubpopulation(Subpopulation subpopulation) { Database.Execute("DELETE FROM Subpopulation WHERE SubpopulationID = @p1;", subpopulation.SubpopulationID); }