Skip to content

Genetic generators

xopt.generators.ga.cnsga.CNSGAGenerator

CNSGAGenerator(**kwargs)

Bases: Generator

Source code in xopt/generators/ga/cnsga.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def __init__(self, **kwargs):
    super().__init__(**kwargs)

    self._loaded_population = (
        None  # use these to generate children until the first pop is made
    )

    # DEAP toolbox (internal)
    self._toolbox = cnsga_toolbox(self.vocs, selection="auto")

    if self.population_file is not None:
        self.load_population_csv(self.population_file)

    if self.output_path is not None:
        assert os.path.isdir(self.output_path), "Output directory does not exist"

Attributes

xopt.generators.ga.cnsga.CNSGAGenerator.n_pop property
n_pop

Convenience name for options.population_size

Functions

xopt.generators.ga.cnsga.CNSGAGenerator.generate
generate(n_candidates)

generate n_candidates candidates

Source code in xopt/generators/ga/cnsga.py
103
104
105
106
107
108
109
110
111
112
113
def generate(self, n_candidates) -> list[dict]:
    """
    generate `n_candidates` candidates

    """

    # Make sure we have enough children to fulfill the request
    while len(self._children) < n_candidates:
        self._children.extend(self.create_children())

    return [self._children.pop() for _ in range(n_candidates)]
xopt.generators.ga.cnsga.CNSGAGenerator.load_population_csv
load_population_csv(filename)

Read a population from a CSV file. These will be reverted back to children for re-evaluation.

Source code in xopt/generators/ga/cnsga.py
149
150
151
152
153
154
155
156
157
158
159
160
def load_population_csv(self, filename):
    """
    Read a population from a CSV file.
    These will be reverted back to children for re-evaluation.
    """
    pop = pd.read_csv(filename, index_col="xopt_index")
    self._loaded_population = pop
    # This is a list of dicts
    self._children = self.vocs.convert_dataframe_to_inputs(
        pop[self.vocs.variable_names], include_constants=False
    ).to_dict(orient="records")
    logger.info(f"Loaded population of len {len(pop)} from file: {filename}")
xopt.generators.ga.cnsga.CNSGAGenerator.write_offspring
write_offspring(filename=None)

Write the current offspring to a CSV file.

Similar to write_population

Source code in xopt/generators/ga/cnsga.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def write_offspring(self, filename=None):
    """
    Write the current offspring to a CSV file.

    Similar to write_population
    """
    if self._offspring is None:
        logger.warning("No offspring to write")
        return

    if filename is None:
        timestamp = xopt.utils.isotime(include_microseconds=True).replace(":", "_")
        filename = f"{self.name}_offspring_{timestamp}.csv"
        filename = os.path.join(self.output_path, filename)

    self._offspring.to_csv(filename, index_label="xopt_index")
xopt.generators.ga.cnsga.CNSGAGenerator.write_population
write_population(filename=None)

Write the current population to a CSV file.

Similar to write_offspring

Source code in xopt/generators/ga/cnsga.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def write_population(self, filename=None):
    """
    Write the current population to a CSV file.

    Similar to write_offspring
    """
    if self.population is None:
        logger.warning("No population to write")
        return

    if filename is None:
        timestamp = xopt.utils.isotime(include_microseconds=True).replace(":", "_")
        filename = f"{self.name}_population_{timestamp}.csv"
        filename = os.path.join(self.output_path, filename)

    self.population.to_csv(filename, index_label="xopt_index")