Optiscope Classification Analysis¶
This notebook demonstrates the solution classification methods in optiscope, including:
- Classification by Best/Worst Objective
- Physical Programming (PRUF) Classification
In [1]:
Copied!
import numpy as np
import pandas as pd
from optiscope import OptimizationResult, ProblemMetadata
from optiscope.analysis.classification import classify_result
from optiscope.core import Objective, OptimizationDirection
from optiscope.plotting import plot_parallel_coordinates, plot_scatter_matrix
import numpy as np
import pandas as pd
from optiscope import OptimizationResult, ProblemMetadata
from optiscope.analysis.classification import classify_result
from optiscope.core import Objective, OptimizationDirection
from optiscope.plotting import plot_parallel_coordinates, plot_scatter_matrix
Data Generation¶
We generate a synthetic multi-objective optimization problem.
In [2]:
Copied!
def generate_test_problem(n_points: int = 200, seed: int = 42):
"""Generate synthetic multi-objective optimization problem."""
np.random.seed(seed)
# Design variables
x1 = np.random.uniform(0, 10, n_points)
x2 = np.random.uniform(0, 10, n_points)
x3 = np.random.uniform(0, 10, n_points)
# Objectives
f1 = x1
f2 = (x2 + 1) / (x1 + 0.1)
f3 = x3 * x1
# Create result
result = OptimizationResult(
problem_metadata=ProblemMetadata(
name="Classification Test",
solver="Random",
n_design_variables=3,
n_objectives=3,
),
design_variables=pd.DataFrame({"x1": x1, "x2": x2, "x3": x3}),
objectives=pd.DataFrame({"f1": f1, "f2": f2, "f3": f3}),
)
result.add_variable_metadata(Objective(name="f1", direction=OptimizationDirection.MINIMIZE))
result.add_variable_metadata(Objective(name="f2", direction=OptimizationDirection.MINIMIZE))
result.add_variable_metadata(Objective(name="f3", direction=OptimizationDirection.MINIMIZE))
return result
print("Generating test problem...")
result = generate_test_problem(n_points=200)
print(f"Created problem: {result.problem_metadata.name}")
def generate_test_problem(n_points: int = 200, seed: int = 42):
"""Generate synthetic multi-objective optimization problem."""
np.random.seed(seed)
# Design variables
x1 = np.random.uniform(0, 10, n_points)
x2 = np.random.uniform(0, 10, n_points)
x3 = np.random.uniform(0, 10, n_points)
# Objectives
f1 = x1
f2 = (x2 + 1) / (x1 + 0.1)
f3 = x3 * x1
# Create result
result = OptimizationResult(
problem_metadata=ProblemMetadata(
name="Classification Test",
solver="Random",
n_design_variables=3,
n_objectives=3,
),
design_variables=pd.DataFrame({"x1": x1, "x2": x2, "x3": x3}),
objectives=pd.DataFrame({"f1": f1, "f2": f2, "f3": f3}),
)
result.add_variable_metadata(Objective(name="f1", direction=OptimizationDirection.MINIMIZE))
result.add_variable_metadata(Objective(name="f2", direction=OptimizationDirection.MINIMIZE))
result.add_variable_metadata(Objective(name="f3", direction=OptimizationDirection.MINIMIZE))
return result
print("Generating test problem...")
result = generate_test_problem(n_points=200)
print(f"Created problem: {result.problem_metadata.name}")
Generating test problem... Created problem: Classification Test
1. Classification by Objective Performance¶
Identify which objective is the best or worst performing for each solution.
In [3]:
Copied!
# Classify by best objective
best_obj_class = classify_result(
result, method="best_obj", add_to_result=True, new_column_name="best_objective"
)
print("Classification by Best Objective (first 10):")
print(best_obj_class[:10])
# Classify by worst objective
worst_obj_class = classify_result(
result, method="worst_obj", add_to_result=True, new_column_name="worst_objective"
)
print("\nClassification by Worst Objective (first 10):")
print(worst_obj_class[:10])
# Classify by best objective
best_obj_class = classify_result(
result, method="best_obj", add_to_result=True, new_column_name="best_objective"
)
print("Classification by Best Objective (first 10):")
print(best_obj_class[:10])
# Classify by worst objective
worst_obj_class = classify_result(
result, method="worst_obj", add_to_result=True, new_column_name="worst_objective"
)
print("\nClassification by Worst Objective (first 10):")
print(worst_obj_class[:10])
Classification by Best Objective (first 10): ['f2' 'f2' 'f2' 'f2' 'f3' 'f2' 'f3' 'f3' 'f2' 'f2'] Classification by Worst Objective (first 10): ['f1' 'f1' 'f1' 'f1' 'f1' 'f1' 'f1' 'f1' 'f1' 'f1']
2. Physical Programming (PRUF) Classification¶
Classify solutions into desirability categories (Highly Desirable to Highly Undesirable).
In [4]:
Copied!
# PRUF Worst (Pessimistic)
pruf_worst = classify_result(
result, method="pruf_worst", add_to_result=True, new_column_name="pruf_class"
)
print("PRUF Classification (Pessimistic) - first 10:")
print(pruf_worst[:10])
# Count categories
unique, counts = np.unique(pruf_worst, return_counts=True)
print("\nCategory Counts:")
for cat, count in zip(unique, counts):
print(f" {cat}: {count}")
# PRUF Worst (Pessimistic)
pruf_worst = classify_result(
result, method="pruf_worst", add_to_result=True, new_column_name="pruf_class"
)
print("PRUF Classification (Pessimistic) - first 10:")
print(pruf_worst[:10])
# Count categories
unique, counts = np.unique(pruf_worst, return_counts=True)
print("\nCategory Counts:")
for cat, count in zip(unique, counts):
print(f" {cat}: {count}")
PRUF Classification (Pessimistic) - first 10: ['Desirable' 'Highly Undesirable' 'Undesirable' 'Undesirable' 'Highly Desirable' 'Highly Desirable' 'Highly Desirable' 'Highly Undesirable' 'Undesirable' 'Undesirable'] Category Counts: Desirable: 50 Highly Desirable: 38 Highly Undesirable: 43 Tolerable: 31 Undesirable: 38
3. Visualization¶
Visualize the results colored by their classification.
In [5]:
Copied!
# Parallel Coordinates colored by PRUF class
fig = plot_parallel_coordinates(
result,
include_objectives=True,
color_by="pruf_class",
title="Parallel Coordinates (Colored by PRUF Class)",
)
fig.show()
# Parallel Coordinates colored by PRUF class
fig = plot_parallel_coordinates(
result,
include_objectives=True,
color_by="pruf_class",
title="Parallel Coordinates (Colored by PRUF Class)",
)
fig.show()
In [6]:
Copied!
# Scatter Matrix colored by Best Objective
fig2 = plot_scatter_matrix(
result,
include_objectives=True,
color_by="best_objective",
title="Scatter Matrix (Colored by Best Objective)",
)
fig2.show()
# Scatter Matrix colored by Best Objective
fig2 = plot_scatter_matrix(
result,
include_objectives=True,
color_by="best_objective",
title="Scatter Matrix (Colored by Best Objective)",
)
fig2.show()