<commit_before>import { Subject } from "rx";

import { IEvaluation } from "../interfaces/evaluation";
import { IGenomeOptions } from "../options/genome-options";
import { IPopulationOptions } from "../options/population-options";
import { Organism } from "./organism";

export abstract class Population<
    GenType extends IGenomeOptions,
    PopType extends IPopulationOptions,
    DataType, PhenoType, EnvStateType> {

    public organisms: Array<Organism<GenType, PopType, DataType, PhenoType, EnvStateType>>;
    public toEvaluate: Subject<Organism<GenType, PopType, DataType, PhenoType, EnvStateType>>;
    public evaluations: Subject<IEvaluation<Organism<GenType, PopType, DataType, PhenoType, EnvStateType>, PhenoType>>;

    constructor(
        public genOptions: GenType,
        public popOptions: PopType) {
        this.toEvaluate = new Subject();
        this.evaluations = new Subject();

        this.evaluateData();
    }

    public abstract evaluate(
        organism: Organism<GenType, PopType, DataType, PhenoType, EnvStateType>,
    ): IEvaluation<Organism<GenType, PopType, DataType, PhenoType, EnvStateType>, PhenoType>;

    public abstract createOrganism(): Organism<GenType, PopType, DataType, PhenoType, EnvStateType>;

    private evaluateData() {
        this.toEvaluate
            .subscribe((org) => {
                this.evaluations.onNext(this.evaluate(org));
            });
    }

    private updateGenotype() {
        this.evaluations
            .subscribe((e) => {
                // mutate genome
                // send genome to organism<commit_msg>implemented genotype update logic<commit_after>import { IDisposable, Subject } from "rx";
import { Genome } from "../genotypes/genome";

import { IEvaluation } from "../interfaces/evaluation";
import { IGenomeOptions } from "../options/genome-options";
import { IPopulationOptions } from "../options/population-options";
import { ReactiveProperty } from "../reactive-property";
import { Organism } from "./organism";

export abstract class Population<
    GenType extends IGenomeOptions,
    PopType extends IPopulationOptions,
    DataType, PhenoType, EnvStateType> {

    public organisms: Array<Organism<GenType, PopType, DataType, PhenoType, EnvStateType>>;
    public toEvaluate: Subject<Organism<GenType, PopType, DataType, PhenoType, EnvStateType>>;
    public evaluations: Subject<IEvaluation<Organism<GenType, PopType, DataType, PhenoType, EnvStateType>, PhenoType>>;
    public avgFitness: ReactiveProperty<number>;

    constructor(
        public genOptions: GenType,
        public popOptions: PopType) {
        this.toEvaluate = new Subject();
        this.evaluations = new Subject();
        this.avgFitness = new ReactiveProperty();
        this.evaluateData();
    }

    // evaluate organism's performance based on the data it collected.
    public abstract evaluate(
        organism: Organism<GenType, PopType, DataType, PhenoType, EnvStateType>,
    ): IEvaluation<Organism<GenType, PopType, DataType, PhenoType, EnvStateType>, PhenoType>;

    // create an organism to inject into environment.
    public abstract createOrganism(): Organism<GenType, PopType, DataType, PhenoType, EnvStateType>;

    // mutate the organism based on evaluation
    public abstract mutateOrganism(
        evaluation: IEvaluation<
            Organism<GenType, PopType, DataType, PhenoType, EnvStateType>,
            PhenoType>): Genome<GenType>;

    // evaluate data as it comes in
    private evaluateData() {
        this.toEvaluate
            .subscribe((org) => {
                this.evaluations.onNext(this.evaluate(org));
            });
    }

    // update genotypes as they are evaluated
    private updateGenotype() {
        this.evaluations
            .subscribe((e) => {
                this.avgFitness.value = e.fitness;
                e.organism.genotype.value = this.mutateOrganism(e);
{'fraction_non_alphanumeric': 0.10593654042988741, 'fraction_numerical': 0.0, 'mean_word_length': 3.2721311475409838, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7366926', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1048, 'n_words': 275}