Skip to content

Commit

Permalink
Merge pull request #38 from electronsandstuff/objectives-constraints-…
Browse files Browse the repository at this point in the history
…options

More General Options for Objectives / Constraints in Container Objects
  • Loading branch information
electronsandstuff authored Jan 26, 2025
2 parents bc05cd2 + 01a09ee commit 140051e
Show file tree
Hide file tree
Showing 13 changed files with 477 additions and 66 deletions.
59 changes: 59 additions & 0 deletions dev_notebooks/save_new_legacy_file.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Prepare a New \"Legacy Test File\"\n",
"This notebook loads the data from the first legacy test file and saves it using a new version of the library in the latest save file format. This will ensure a record of the file format exists for the purpose of regression tests."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from paretobench import Experiment"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"exp = Experiment.load(\"../tests/legacy_file_formats/paretobench_file_format_v1.0.0.h5\")\n",
"exp.save(\"../tests/legacy_file_formats/paretobench_file_format_v1.1.0.h5\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "paretobench",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
3 changes: 3 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
name: paretobench
dependencies:
- pytest
- pip
- pip:
- pre-commit
105 changes: 87 additions & 18 deletions example_notebooks/container_objects.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import os\n",
"import paretobench as pb\n",
"import tempfile"
Expand All @@ -36,17 +37,22 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Population(size=32, vars=10, objs=2, cons=0, fevals=0)\n",
"Population(size=32, vars=10, objs=[--], cons=[], fevals=32)\n",
"Decision variables: (32, 10)\n",
"Objectives: (32, 2)\n",
"Constraints: (32, 0)\n",
"Function evaluations: 0\n"
"Function evaluations: 32\n"
]
}
],
"source": [
"# Create an example population\n",
"pop = pb.Population.from_random(n_objectives=2, n_decision_vars=10, n_constraints=0, pop_size=32)\n",
"pop = pb.Population(\n",
" x=np.random.random((32, 10)), # 32 individuals worth of 10 decision vars\n",
" f=np.random.random((32, 2)), # 2 objectives\n",
" # Not specifying an array (constraints here) will cause it to populate empty based on other's shape\n",
" fevals=32,\n",
")\n",
"print(pop)\n",
"\n",
"# Examine some of the parameters\n",
Expand All @@ -67,12 +73,66 @@
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"Population(size=32, vars=10, objs=[--], cons=[>0.0e+00], fevals=32)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a population with some names\n",
"pop = pb.Population(\n",
" x=np.random.random((32, 10)),\n",
" f=np.random.random((32, 2)),\n",
" g=np.random.random((32, 1)), # Add a single constraint this time\n",
" names_x=[f\"Decision var {idx}\" for idx in range(pop.x.shape[1])],\n",
" names_f=[\"Objective_1\", \"Objective_2\"],\n",
" names_g=[\"Important_Constraint\"],\n",
")\n",
"pop"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Populations default to representing minimization problems with constraints that are satisfied when each g >= 0. This can be modified as in the following code block. Configuring these settings is important when using container methods to find feasible individuals and in calculation of domination."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Population(size=32, vars=10, objs=[+-+], cons=[>3.3e-01,<6.6e-01], fevals=32)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Set naems of the decision variables, objectives, and constraints\n",
"pop.names_x = [f\"Decision var {idx}\" for idx in range(pop.x.shape[1])]\n",
"pop.names_f = [\"Objective 1\", \"Objective 2\"]\n",
"pop.names_g = []"
"# Create a population and specify the objective / constraint directions and target\n",
"pop = pb.Population(\n",
" x=np.random.random((32, 10)),\n",
" f=np.random.random((32, 3)),\n",
" g=np.random.random((32, 2)),\n",
" obj_directions=\"+-+\", # \"+\" indicates maximization, \"-\" inidicates minimization\n",
" constraint_directions=\"><\", # \"<\" means satisifed when g<target and \">\" means satsified when g>target\n",
" constraint_targets=np.array([0.33, 0.66]), # >0.33 constraint and <0.66 constraint\n",
")\n",
"\n",
"pop"
]
},
{
Expand All @@ -85,23 +145,32 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"History(problem='WFG1 (n=10)', reports=15, vars=10, objs=2, cons=0)\n",
"Problem: WFG1 (n=10)\n",
"Number of reports: 15\n"
"History(problem='WFG1', reports=10, vars=30, objs=[--], cons=[])\n",
"Problem: WFG1\n",
"Number of reports: 10\n"
]
}
],
"source": [
"# Create an example history object\n",
"hist = pb.History.from_random(n_populations=15, n_objectives=2, n_decision_vars=10, n_constraints=0, pop_size=32)\n",
"hist.problem = \"WFG1 (n=10)\"\n",
"# Create some population objects which will go into the history. We will use the random generation helper function here.\n",
"n_reports = 10\n",
"reports = [\n",
" pb.Population.from_random(n_objectives=2, n_decision_vars=30, n_constraints=0, pop_size=50)\n",
" for _ in range(n_reports)\n",
"]\n",
"\n",
"# Construct the history object\n",
"hist = pb.History(\n",
" reports=reports,\n",
" problem=\"WFG1\", # Use ParetoBench single line format for maximum compatibility with plotting, etc\n",
")\n",
"print(hist)\n",
"\n",
"# Print some of the properties\n",
Expand All @@ -119,17 +188,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experiment(name='NSGA-II (default params)', created='2024-12-10', author='The author of ParetoBench', software='ParetoBench example notebook 1.0.0', runs=32)\n",
"Experiment(name='NSGA-II (default params)', created='2025-01-26', author='The author of ParetoBench', software='ParetoBench example notebook 1.0.0', runs=32)\n",
"Number of histories: 32\n",
"Name: NSGA-II (default params)\n",
"Creation time: 2024-12-10 05:21:51.993640+00:00\n"
"Creation time: 2025-01-26 06:36:29.299300+00:00\n"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "paretobench"
version = "0.3.0"
version = "0.4.0"
authors = [
{ name="Christopher M. Pierce", email="contact@chris-pierce.com" },
]
Expand Down
Loading

0 comments on commit 140051e

Please sign in to comment.