Skip to content

Commit

Permalink
Merge pull request #97 from ChristopherMayes/cnsga_write_offspring
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherMayes authored Mar 12, 2023
2 parents ea17104 + 149dda7 commit dc95a98
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 251 deletions.
459 changes: 210 additions & 249 deletions docs/examples/cnsga/cnsga_tnk.ipynb

Large diffs are not rendered by default.

27 changes: 25 additions & 2 deletions xopt/generators/ga/cnsga.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,14 @@ def add_data(self, new_data: pd.DataFrame):
self.population = cnsga_select(
candidates, self.n_pop, self.vocs, self.toolbox
)
self.children = [] # reset children
self.offspring = None # reset offspring

if self.options.output_path is not None:
self.write_offspring()
self.write_population()

self.children = [] # reset children
self.offspring = None # reset offspring

def generate(self, n_candidates) -> List[Dict]:
"""
generate `n_candidates` candidates
Expand All @@ -143,10 +145,31 @@ def generate(self, n_candidates) -> List[Dict]:

return [self.children.pop() for _ in range(n_candidates)]

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:
filename = f"{self.alias}_offspring_{xopt.utils.isotime(include_microseconds=True)}.csv"
filename = os.path.join(self.options.output_path, filename)

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

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:
filename = f"{self.alias}_population_{xopt.utils.isotime(include_microseconds=True)}.csv"
Expand Down
21 changes: 21 additions & 0 deletions xopt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,24 @@ def safe_call(func, *args, **kwargs):
def format_option_descriptions(options_object):
options_dict = get_descriptions_defaults(options_object)
return "\n\nGenerator Options\n" + yaml.dump(options_dict)


def read_xopt_csv(*files):
"""
Read several Xopt-style CSV files into data
Parameters
----------
file1, file2, ...: path-like
One or more Xopt csv files
Returns
-------
pd.DataFrame
DataFrame with xopt_index as the index column
"""
dfs = []
for file in files:
df = pd.read_csv(file, index_col="xopt_index")
dfs.append(df)
return pd.concat(dfs)

0 comments on commit dc95a98

Please sign in to comment.