pyMachDM 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pymachdm/ANN_weights/1.Weld_Beam_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/10.DTLZ7_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/11.DTLZ7_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/12.MW8_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/13.MW8_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/14.WAVY_3Obj_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/15.FLOW_3Obj_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/16.DPF4_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/17.DPF4_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/18.River_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/19.River_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/2.OSY_2Obj_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/20.C2DTLZ2_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/21.C2DTLZ2_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/22.Knee_5Obj_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/23.WATER_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/24.WATER_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/25.Clutch_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/26.Clutch_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/27.NIMBUS_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/28.NIMBUS_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/29.DTLZ2_8Obj_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/3.ZDT2_2Obj_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/30.GAA_10Obj_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/4.Crash_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/4.ZDT3_2Obj_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/4a.ZDT3_2Obj_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/5.WAVY_2Obj_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/6.WAVY_2Obj_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/7.Carside_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/8.Carside_MachineDM.joblib +0 -0
- pymachdm/ANN_weights/9.FLOWER_3Obj_MachineDM.joblib +0 -0
- pymachdm/__init__.py +71 -0
- pymachdm/ann/__init__.py +16 -0
- pymachdm/ann/multioutput_class.py +115 -0
- pymachdm/ann/multioutput_reg.py +110 -0
- pymachdm/imcdm_methods/__init__.py +25 -0
- pymachdm/imcdm_methods/guess_start.py +46 -0
- pymachdm/imcdm_methods/guess_sub.py +49 -0
- pymachdm/imcdm_methods/mod_guess_start.py +47 -0
- pymachdm/imcdm_methods/mod_guess_sub.py +47 -0
- pymachdm/imcdm_methods/nimbus_inter.py +64 -0
- pymachdm/imcdm_methods/nimbus_start.py +48 -0
- pymachdm/imcdm_methods/nimbus_sub.py +56 -0
- pymachdm/imcdm_methods/stem_start.py +47 -0
- pymachdm/imcdm_methods/stem_sub.py +51 -0
- pymachdm/imcdm_methods/stom_start.py +46 -0
- pymachdm/imcdm_methods/stom_sub.py +47 -0
- pymachdm/machdm_imcdm/__init__.py +4 -0
- pymachdm/machdm_imcdm/machdm_all.py +771 -0
- pymachdm/machdm_imcdm/machine_dm.py +124 -0
- pymachdm/pareto/__init__.py +3 -0
- pymachdm/pareto/pareto_analyser.py +162 -0
- pymachdm/plots/__init__.py +7 -0
- pymachdm/plots/plot_2d_scatter.py +57 -0
- pymachdm/plots/plot_3d_scatter.py +91 -0
- pymachdm/plots/plot_pcp.py +93 -0
- pymachdm/plots/plot_radviz.py +496 -0
- pymachdm/plots/plot_tsne.py +369 -0
- pymachdm/problems/__init__.py +4 -0
- pymachdm/problems/problem_factory.py +879 -0
- pymachdm/problems/symbolic_problem_factory.py +696 -0
- pymachdm/run_machdm.py +232 -0
- pymachdm/soo_aasf/__init__.py +7 -0
- pymachdm/soo_aasf/guess_asf.py +99 -0
- pymachdm/soo_aasf/mod_guess_asf.py +112 -0
- pymachdm/soo_aasf/nimbus_asf.py +104 -0
- pymachdm/soo_aasf/stem_asf.py +99 -0
- pymachdm/soo_aasf/stom_asf.py +81 -0
- pymachdm/utils/__init__.py +21 -0
- pymachdm/utils/auto_tradeoff_stom.py +34 -0
- pymachdm/utils/compute_lambda.py +71 -0
- pymachdm/utils/compute_lambda_with_g.py +85 -0
- pymachdm/utils/cos_sim.py +8 -0
- pymachdm/utils/generate_ref_dirs.py +6 -0
- pymachdm/utils/predict_best.py +45 -0
- pymachdm/utils/predict_best2.py +62 -0
- pymachdm/utils/predict_prob.py +67 -0
- pymachdm/utils/predict_rank.py +47 -0
- pymachdm-0.1.0.dist-info/LICENSE +21 -0
- pymachdm-0.1.0.dist-info/METADATA +223 -0
- pymachdm-0.1.0.dist-info/RECORD +84 -0
- pymachdm-0.1.0.dist-info/WHEEL +5 -0
- pymachdm-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from pymoo.core.problem import ElementwiseProblem
|
|
3
|
+
|
|
4
|
+
class STOM_Problem(ElementwiseProblem):
|
|
5
|
+
def __init__(self, problem=None, start_F=None, eps=None, i1=None, i2=None, i3=None, i4=None, mode=None,
|
|
6
|
+
ideal_point=None, nadir_point=None):
|
|
7
|
+
|
|
8
|
+
self.problem = problem
|
|
9
|
+
self.start_F = np.array(start_F)
|
|
10
|
+
self.eps = eps
|
|
11
|
+
self.i1 = np.array(i1) if i1 is not None else np.array([])
|
|
12
|
+
self.i2 = np.array(i2) if i2 is not None else np.array([])
|
|
13
|
+
self.i3 = np.array(i3) if i3 is not None else np.array([])
|
|
14
|
+
self.i4 = np.array(i4) if i4 is not None else np.array([])
|
|
15
|
+
self.mode = mode
|
|
16
|
+
self.n_ieq = len(self.i1) + len(self.i2) + len(self.i4)
|
|
17
|
+
|
|
18
|
+
self.ideal_point = np.array(ideal_point)
|
|
19
|
+
self.nadir_point = np.array(nadir_point)
|
|
20
|
+
self.rho = 0.001
|
|
21
|
+
# print("Ideal, Nadir:", ideal_point, nadir_point)
|
|
22
|
+
# Count constraints from the base problem
|
|
23
|
+
n_ieq_constr_base = getattr(problem, "n_ieq_constr", 0)
|
|
24
|
+
n_eq_constr_base = getattr(problem, "n_eq_constr", 0)
|
|
25
|
+
|
|
26
|
+
# For NIMBUS 'sub' mode, include additional constraints
|
|
27
|
+
n_ieq_constr_total = n_ieq_constr_base
|
|
28
|
+
n_eq_constr_total = n_eq_constr_base
|
|
29
|
+
|
|
30
|
+
super().__init__(
|
|
31
|
+
n_var=self.problem.n_var,
|
|
32
|
+
n_obj=1,
|
|
33
|
+
n_ieq_constr=n_ieq_constr_total,
|
|
34
|
+
n_eq_constr=n_eq_constr_total,
|
|
35
|
+
xl=self.problem.xl,
|
|
36
|
+
xu=self.problem.xu
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
def _evaluate(self, x, out, *args, **kwargs):
|
|
40
|
+
# Evaluate original problem
|
|
41
|
+
base_out = {}
|
|
42
|
+
self.problem._evaluate(x, base_out)
|
|
43
|
+
|
|
44
|
+
f = np.array(base_out.get("F"))
|
|
45
|
+
G_base = np.array(base_out.get("G", [])) # inequality constraints (<=0)
|
|
46
|
+
H_base = np.array(base_out.get("H", [])) # equality constraints (=0)
|
|
47
|
+
|
|
48
|
+
# Handle empty constraints gracefully
|
|
49
|
+
if G_base.size == 0:
|
|
50
|
+
G_base = np.empty(0)
|
|
51
|
+
if H_base.size == 0:
|
|
52
|
+
H_base = np.empty(0)
|
|
53
|
+
|
|
54
|
+
# Combined index set
|
|
55
|
+
if len(self.i1) == 0 and len(self.i2) == 0 and len(self.i3) == 0 and len(self.i4) == 0:
|
|
56
|
+
i1234 = np.arange(len(f))
|
|
57
|
+
else:
|
|
58
|
+
i1234 = np.concatenate([self.i1, self.i2, self.i3, self.i4])
|
|
59
|
+
|
|
60
|
+
# --- Start mode ---
|
|
61
|
+
if self.mode == 'start':
|
|
62
|
+
|
|
63
|
+
F = np.max((f[i1234]-self.start_F[i1234])/(self.nadir_point[i1234]-self.ideal_point[i1234]) +
|
|
64
|
+
self.rho*np.sum((f[i1234])/(self.nadir_point[i1234]-self.ideal_point[i1234])))
|
|
65
|
+
|
|
66
|
+
out["F"] = [F]
|
|
67
|
+
out["G"] = G_base
|
|
68
|
+
out["H"] = H_base
|
|
69
|
+
|
|
70
|
+
# --- Subproblem mode ---
|
|
71
|
+
elif self.mode == 'sub':
|
|
72
|
+
|
|
73
|
+
F = np.max((f[i1234]-self.ideal_point[i1234])/(self.start_F[i1234]-self.ideal_point[i1234]+0.001) +
|
|
74
|
+
self.rho*np.sum((f[i1234])/(self.start_F[i1234]-self.ideal_point[i1234]+0.001)))
|
|
75
|
+
# Combine with original problem constraints
|
|
76
|
+
out["F"] = [F]
|
|
77
|
+
out["G"] = G_base
|
|
78
|
+
out["H"] = H_base
|
|
79
|
+
|
|
80
|
+
else:
|
|
81
|
+
raise ValueError("mode must be 'start', or 'sub'")
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from .auto_tradeoff_stom import param_i4
|
|
2
|
+
from .compute_lambda import compute_lambda
|
|
3
|
+
from .compute_lambda_with_g import compute_lambda_const
|
|
4
|
+
from .cos_sim import cos_sim_classes
|
|
5
|
+
from .generate_ref_dirs import ref_point_generator
|
|
6
|
+
from .predict_best import predict_best_class_params
|
|
7
|
+
from .predict_best2 import predict_class_parameters
|
|
8
|
+
from .predict_prob import predict_class_parameters_probabilistic
|
|
9
|
+
from .predict_rank import predict_best_params_rank
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"param_i4",
|
|
13
|
+
"compute_lambda",
|
|
14
|
+
"compute_lambda_const",
|
|
15
|
+
"cos_sim_classes",
|
|
16
|
+
"ref_point_generator",
|
|
17
|
+
"predict_best_class_params",
|
|
18
|
+
"predict_class_parameters",
|
|
19
|
+
"predict_class_parameters_probabilistic",
|
|
20
|
+
"predict_best_params_rank"
|
|
21
|
+
]
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
def param_i4(i1, i2, i4, fh, fh1, ideal_point, lamb):
|
|
4
|
+
# Convert all to numpy arrays
|
|
5
|
+
i1 = np.array(i1, dtype=int)
|
|
6
|
+
i2 = np.array(i2, dtype=int)
|
|
7
|
+
i4 = np.array(i4, dtype=int)
|
|
8
|
+
|
|
9
|
+
# VALID INDICES only
|
|
10
|
+
max_idx = len(fh) - 1
|
|
11
|
+
i1 = i1[i1 <= max_idx]
|
|
12
|
+
i2 = i2[i2 <= max_idx]
|
|
13
|
+
i4 = i4[i4 <= max_idx]
|
|
14
|
+
|
|
15
|
+
j = np.concatenate([i1, i2])
|
|
16
|
+
# j = i1
|
|
17
|
+
N = len(i4)
|
|
18
|
+
rho = 0.00001
|
|
19
|
+
|
|
20
|
+
# Avoid division by zero
|
|
21
|
+
w = 1 / (fh - ideal_point + 1e-4)
|
|
22
|
+
|
|
23
|
+
# Compute Term1
|
|
24
|
+
Term1 = 0
|
|
25
|
+
for t in j:
|
|
26
|
+
Term1 += ((lamb[t] + rho) * w[t]) * (fh[t] - fh1[t])
|
|
27
|
+
|
|
28
|
+
# Update fh1 for i4 indices only
|
|
29
|
+
for idx in i4:
|
|
30
|
+
denom = N * (lamb[idx] + rho) * w[idx]
|
|
31
|
+
fh1[idx] = fh[idx] + (1 / denom) * Term1
|
|
32
|
+
|
|
33
|
+
# Return the updated values for valid i4
|
|
34
|
+
return fh1[i4]
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import sympy as sp
|
|
3
|
+
from scipy.optimize import nnls
|
|
4
|
+
|
|
5
|
+
def compute_lambda(problem, point):
|
|
6
|
+
"""
|
|
7
|
+
Compute λ ≥ 0, sum λ = 1 using NNLS projection.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
n_var = problem.n_var
|
|
11
|
+
n_obj = problem.n_obj
|
|
12
|
+
|
|
13
|
+
# Symbolic variables
|
|
14
|
+
x = sp.symbols(f'x0:{n_var}')
|
|
15
|
+
|
|
16
|
+
# Get symbolic objectives
|
|
17
|
+
try:
|
|
18
|
+
f_sym = problem.evaluate_symbolic(x)
|
|
19
|
+
except AttributeError:
|
|
20
|
+
raise AttributeError(
|
|
21
|
+
"Problem must define evaluate_symbolic(x) returning sympy objective expressions."
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
# Jacobian
|
|
25
|
+
J_sym = sp.Matrix(f_sym).jacobian(x)
|
|
26
|
+
|
|
27
|
+
# Lambdify
|
|
28
|
+
J_func = sp.lambdify(x, J_sym, "numpy")
|
|
29
|
+
|
|
30
|
+
# Numeric evaluation
|
|
31
|
+
try:
|
|
32
|
+
J_numeric = np.array(J_func(*point), dtype=float)
|
|
33
|
+
|
|
34
|
+
# Guard against NaNs / infs
|
|
35
|
+
if not np.all(np.isfinite(J_numeric)):
|
|
36
|
+
raise ValueError("Invalid Jacobian")
|
|
37
|
+
|
|
38
|
+
# Gram matrix eigenvalues
|
|
39
|
+
gram = J_numeric @ J_numeric.T
|
|
40
|
+
eigvals = np.linalg.eigvals(gram)
|
|
41
|
+
|
|
42
|
+
if not np.all(np.isfinite(eigvals)):
|
|
43
|
+
raise ValueError("Invalid eigenvalues")
|
|
44
|
+
|
|
45
|
+
except Exception:
|
|
46
|
+
# ---- EXIT IMMEDIATELY (break-equivalent) ----
|
|
47
|
+
lambda_sol = 0.0
|
|
48
|
+
eigvals = 0.0
|
|
49
|
+
return lambda_sol, eigvals
|
|
50
|
+
|
|
51
|
+
# ---------------------------------------------------------
|
|
52
|
+
# Solve J^T λ = 0 using NNLS
|
|
53
|
+
# ---------------------------------------------------------
|
|
54
|
+
A = J_numeric.T # shape: (n_var, n_obj)
|
|
55
|
+
b = np.zeros(n_var)
|
|
56
|
+
|
|
57
|
+
lam_nnls, _ = nnls(A, b)
|
|
58
|
+
print("Lambdas:", lam_nnls)
|
|
59
|
+
|
|
60
|
+
# If solution is all zero (rank deficiency)
|
|
61
|
+
if np.allclose(lam_nnls, 0):
|
|
62
|
+
lam_nnls = np.ones(n_obj)
|
|
63
|
+
|
|
64
|
+
# Normalize: sum λ = 1
|
|
65
|
+
lambda_sol = lam_nnls / np.sum(lam_nnls)
|
|
66
|
+
|
|
67
|
+
# Projection (numerical safety)
|
|
68
|
+
lambda_sol = np.maximum(lambda_sol, 0)
|
|
69
|
+
lambda_sol = lambda_sol / np.sum(lambda_sol)
|
|
70
|
+
|
|
71
|
+
return lambda_sol, eigvals
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import sympy as sp
|
|
3
|
+
from scipy.optimize import nnls
|
|
4
|
+
|
|
5
|
+
def compute_lambda_const(problem, point):
|
|
6
|
+
"""
|
|
7
|
+
Compute non-negative multipliers λ for objectives and constraints:
|
|
8
|
+
|
|
9
|
+
minimize ||A λ|| subject to λ >= 0, sum λ = 1
|
|
10
|
+
|
|
11
|
+
where A = [J_obj; J_con]^T is the stacked Jacobian matrix.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
n_var = problem.n_var
|
|
15
|
+
n_obj = problem.n_obj
|
|
16
|
+
|
|
17
|
+
# ---------------------------------------------------------
|
|
18
|
+
# 1. Symbolic variables
|
|
19
|
+
# ---------------------------------------------------------
|
|
20
|
+
x = sp.symbols(f'x0:{n_var}')
|
|
21
|
+
|
|
22
|
+
# ---------------------------------------------------------
|
|
23
|
+
# 2. Get symbolic objectives
|
|
24
|
+
# ---------------------------------------------------------
|
|
25
|
+
try:
|
|
26
|
+
f_sym = problem.evaluate_symbolic(x)
|
|
27
|
+
except AttributeError:
|
|
28
|
+
raise AttributeError("Problem must define evaluate_symbolic(x) returning sympy objective expressions.")
|
|
29
|
+
|
|
30
|
+
# ---------------------------------------------------------
|
|
31
|
+
# 3. Get symbolic constraints (if any)
|
|
32
|
+
# ---------------------------------------------------------
|
|
33
|
+
try:
|
|
34
|
+
g_sym = problem.constraint_symbolic(x) # user must implement this
|
|
35
|
+
except AttributeError:
|
|
36
|
+
g_sym = [] # no constraints
|
|
37
|
+
|
|
38
|
+
# convert to list
|
|
39
|
+
if not isinstance(g_sym, (list, tuple)):
|
|
40
|
+
g_sym = [g_sym]
|
|
41
|
+
|
|
42
|
+
n_con = len(g_sym)
|
|
43
|
+
|
|
44
|
+
# ---------------------------------------------------------
|
|
45
|
+
# 4. Build combined vector of objectives + constraints
|
|
46
|
+
# ---------------------------------------------------------
|
|
47
|
+
F_sym = list(f_sym) + list(g_sym) # total m = n_obj + n_con
|
|
48
|
+
|
|
49
|
+
# ---------------------------------------------------------
|
|
50
|
+
# 5. Jacobian of all functions
|
|
51
|
+
# ---------------------------------------------------------
|
|
52
|
+
J_sym = sp.Matrix(F_sym).jacobian(x)
|
|
53
|
+
|
|
54
|
+
# lambdify functions
|
|
55
|
+
J_func = sp.lambdify(x, J_sym, "numpy")
|
|
56
|
+
|
|
57
|
+
# numeric eval
|
|
58
|
+
J_numeric = np.array(J_func(*point), dtype=float) # shape (m × n_var)
|
|
59
|
+
|
|
60
|
+
# ---------------------------------------------------------
|
|
61
|
+
# 6. Gram matrix eigenvalues (for conditioning)
|
|
62
|
+
# ---------------------------------------------------------
|
|
63
|
+
gram = J_numeric @ J_numeric.T
|
|
64
|
+
eigvals = np.linalg.eigvals(gram)
|
|
65
|
+
|
|
66
|
+
# ---------------------------------------------------------
|
|
67
|
+
# 7. Solve J^T λ = 0 using NNLS
|
|
68
|
+
# ---------------------------------------------------------
|
|
69
|
+
m = n_obj + n_con # total number of multipliers
|
|
70
|
+
|
|
71
|
+
A = J_numeric.T # shape (n_var × m)
|
|
72
|
+
b = np.zeros(n_var)
|
|
73
|
+
|
|
74
|
+
# Solve min ||A λ - b|| s.t λ ≥ 0
|
|
75
|
+
lambda_raw, _ = nnls(A, b)
|
|
76
|
+
|
|
77
|
+
# fallback if all-zero (degenerate)
|
|
78
|
+
if np.allclose(lambda_raw, 0):
|
|
79
|
+
lambda_raw = np.ones(m)
|
|
80
|
+
|
|
81
|
+
# normalize: ∑ λ = 1
|
|
82
|
+
lambda_sol = np.maximum(lambda_raw, 0)
|
|
83
|
+
lambda_sol /= np.sum(lambda_sol)
|
|
84
|
+
|
|
85
|
+
return lambda_sol, eigvals
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
def predict_best_class_params(X_input, ANN_1, ANN_2):
|
|
2
|
+
"""
|
|
3
|
+
For a given input X_input, predict:
|
|
4
|
+
- best and next best class (from ANN_1)
|
|
5
|
+
- indices corresponding to these classes
|
|
6
|
+
- parameters predicted from ANN_2 (with zeros for class 1 and 3)
|
|
7
|
+
|
|
8
|
+
Args:
|
|
9
|
+
X_input : array-like, shape (n_features,)
|
|
10
|
+
ANN_1 : trained classification model
|
|
11
|
+
ANN_2 : trained regression model
|
|
12
|
+
|
|
13
|
+
Returns:
|
|
14
|
+
best_class, best_indices, best_parameters,
|
|
15
|
+
next_best_class, next_best_indices, next_best_parameters
|
|
16
|
+
"""
|
|
17
|
+
# Step 1: Predict classes with probabilities
|
|
18
|
+
pred_probs = ANN_1.predict_with_prob(np.array([X_input]))
|
|
19
|
+
pred_original = ANN_1.inverse_transform_predictions(pred_probs)
|
|
20
|
+
|
|
21
|
+
# Extract probabilities and predicted class
|
|
22
|
+
classes = np.hstack([d['predicted_class_original'] for d in pred_original])
|
|
23
|
+
probs = np.vstack([d['probabilities'] for d in pred_original]).squeeze() # shape (n_outputs, n_classes)
|
|
24
|
+
|
|
25
|
+
# Step 2: Get best and next best class for each output
|
|
26
|
+
best_class = np.argmax(probs, axis=1) + 1 # +1 if classes start from 1
|
|
27
|
+
|
|
28
|
+
# Step 3: Get indices of each class
|
|
29
|
+
def get_class_indices(classes_array):
|
|
30
|
+
return {cl: np.where(classes_array == cl)[0] for cl in range(1, 5)}
|
|
31
|
+
|
|
32
|
+
I_best = get_class_indices(best_class)
|
|
33
|
+
|
|
34
|
+
best_indices = np.concatenate(list(I_best.values()))
|
|
35
|
+
|
|
36
|
+
# Step 4: Predict parameters using ANN_2
|
|
37
|
+
params = ANN_2.predict(np.array([X_input])).flatten()
|
|
38
|
+
|
|
39
|
+
for idx in best_indices:
|
|
40
|
+
if best_class[idx] in [1, 3]:
|
|
41
|
+
params[idx] = 0.0
|
|
42
|
+
best_parameters = params.copy()
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
return best_class, I_best, best_parameters
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
def predict_class_parameters(X_input, ANN_1, ANN_2):
|
|
4
|
+
"""
|
|
5
|
+
For a given input X_input, predict:
|
|
6
|
+
- best and next-best class (next-best differs from best by flipping one objective)
|
|
7
|
+
- indices corresponding to these classes
|
|
8
|
+
- parameters predicted from ANN_2 (zeros for class 1 and 3)
|
|
9
|
+
|
|
10
|
+
Args:
|
|
11
|
+
X_input : array-like, shape (n_features,)
|
|
12
|
+
ANN_1 : trained classification model
|
|
13
|
+
ANN_2 : trained regression model
|
|
14
|
+
|
|
15
|
+
Returns:
|
|
16
|
+
best_class, I_best, best_parameters,
|
|
17
|
+
next_best_class, I_next, next_best_parameters
|
|
18
|
+
"""
|
|
19
|
+
# Step 1: Predict class probabilities using ANN_1
|
|
20
|
+
pred_probs = ANN_1.predict_with_prob(np.array([X_input]))
|
|
21
|
+
pred_original = ANN_1.inverse_transform_predictions(pred_probs)
|
|
22
|
+
|
|
23
|
+
# Extract probabilities and predicted class
|
|
24
|
+
probs = np.vstack([d['probabilities'] for d in pred_original]).squeeze() # shape (n_obj, n_classes)
|
|
25
|
+
|
|
26
|
+
# Step 2: Identify top-2 classes and their probabilities
|
|
27
|
+
top2_idx = np.argsort(probs, axis=1)[:, -2:] # last two indices per objective
|
|
28
|
+
top2_probs = np.take_along_axis(probs, top2_idx, axis=1)
|
|
29
|
+
|
|
30
|
+
best_class = top2_idx[:, 1] + 1 # +1 if classes start from 1
|
|
31
|
+
second_best_class = top2_idx[:, 0] + 1
|
|
32
|
+
|
|
33
|
+
# Step 3: Compute probability gaps and choose one objective to flip
|
|
34
|
+
prob_gap = top2_probs[:, 1] - top2_probs[:, 0]
|
|
35
|
+
flip_idx = np.argmin(prob_gap) # flip the most uncertain objective
|
|
36
|
+
|
|
37
|
+
next_best_class = best_class.copy()
|
|
38
|
+
next_best_class[flip_idx] = second_best_class[flip_idx]
|
|
39
|
+
|
|
40
|
+
# Step 4: Get indices for each class
|
|
41
|
+
def get_class_indices(classes_array):
|
|
42
|
+
return {cl: np.where(classes_array == cl)[0] for cl in range(1, 5)}
|
|
43
|
+
|
|
44
|
+
I_best = get_class_indices(best_class)
|
|
45
|
+
I_next = get_class_indices(next_best_class)
|
|
46
|
+
|
|
47
|
+
best_indices = np.concatenate(list(I_best.values()))
|
|
48
|
+
next_best_indices = np.concatenate(list(I_next.values()))
|
|
49
|
+
|
|
50
|
+
# Step 5: Predict parameters using ANN_2
|
|
51
|
+
params_best = ANN_2.predict(np.array([X_input])).flatten()
|
|
52
|
+
params_next = ANN_2.predict(np.array([X_input])).flatten()
|
|
53
|
+
|
|
54
|
+
# Set parameters to zero for classes 1 and 3
|
|
55
|
+
for idx in best_indices:
|
|
56
|
+
if best_class[idx] in [1, 3]:
|
|
57
|
+
params_best[idx] = 0.0
|
|
58
|
+
for idx in next_best_indices:
|
|
59
|
+
if next_best_class[idx] in [1, 3]:
|
|
60
|
+
params_next[idx] = 0.0
|
|
61
|
+
|
|
62
|
+
return best_class, I_best, params_best, next_best_class, I_next, params_next
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
def predict_class_parameters_probabilistic(X_input, ANN_1, ANN_2):
|
|
4
|
+
"""
|
|
5
|
+
For a given input X_input, predict:
|
|
6
|
+
- sampled class for each objective based on predicted probabilities
|
|
7
|
+
- indices corresponding to these classes
|
|
8
|
+
- parameters predicted from ANN_2 (zeros for class 1 and 3)
|
|
9
|
+
- next-best class obtained by flipping the most uncertain objective
|
|
10
|
+
|
|
11
|
+
Args:
|
|
12
|
+
X_input : array-like, shape (n_features,)
|
|
13
|
+
ANN_1 : trained classification model
|
|
14
|
+
ANN_2 : trained regression model
|
|
15
|
+
|
|
16
|
+
Returns:
|
|
17
|
+
sampled_class, I_sampled, params_sampled,
|
|
18
|
+
next_best_class, I_next, params_next
|
|
19
|
+
"""
|
|
20
|
+
# Step 1: Predict class probabilities using ANN_1
|
|
21
|
+
pred_probs = ANN_1.predict_with_prob(np.array([X_input]))
|
|
22
|
+
pred_original = ANN_1.inverse_transform_predictions(pred_probs)
|
|
23
|
+
|
|
24
|
+
# Extract probabilities
|
|
25
|
+
probs = np.vstack([d['probabilities'] for d in pred_original]).squeeze() # shape (n_obj, n_classes)
|
|
26
|
+
n_obj, n_classes = probs.shape
|
|
27
|
+
|
|
28
|
+
# Step 2: Sample classes using CDF
|
|
29
|
+
sampled_class = []
|
|
30
|
+
for prob_vector in probs:
|
|
31
|
+
cdf = np.cumsum(prob_vector)
|
|
32
|
+
r = np.random.rand()
|
|
33
|
+
sampled_class.append(np.searchsorted(cdf, r) + 1) # +1 if classes start from 1
|
|
34
|
+
sampled_class = np.array(sampled_class)
|
|
35
|
+
|
|
36
|
+
# Step 3: Get indices for each class
|
|
37
|
+
def get_class_indices(classes_array):
|
|
38
|
+
return {cl: np.where(classes_array == cl)[0] for cl in range(1, n_classes + 1)}
|
|
39
|
+
|
|
40
|
+
I_sampled = get_class_indices(sampled_class)
|
|
41
|
+
sampled_indices = np.concatenate(list(I_sampled.values()))
|
|
42
|
+
|
|
43
|
+
# Step 4: Predict parameters using ANN_2
|
|
44
|
+
params_sampled = ANN_2.predict(np.array([X_input])).flatten()
|
|
45
|
+
for idx in sampled_indices:
|
|
46
|
+
if sampled_class[idx] in [1, 3]:
|
|
47
|
+
params_sampled[idx] = 0.0
|
|
48
|
+
|
|
49
|
+
# Step 5: Generate next-best class by flipping the most uncertain objective
|
|
50
|
+
# Compute uncertainty as difference between top-2 probabilities
|
|
51
|
+
top2_idx = np.argsort(probs, axis=1)[:, -2:]
|
|
52
|
+
top2_probs = np.take_along_axis(probs, top2_idx, axis=1)
|
|
53
|
+
prob_gap = top2_probs[:, 1] - top2_probs[:, 0]
|
|
54
|
+
flip_idx = np.argmin(prob_gap) # flip the most uncertain objective
|
|
55
|
+
|
|
56
|
+
second_best_class = top2_idx[:, 0] + 1
|
|
57
|
+
next_best_class = sampled_class.copy()
|
|
58
|
+
next_best_class[flip_idx] = second_best_class[flip_idx]
|
|
59
|
+
|
|
60
|
+
I_next = get_class_indices(next_best_class)
|
|
61
|
+
next_indices = np.concatenate(list(I_next.values()))
|
|
62
|
+
params_next = ANN_2.predict(np.array([X_input])).flatten()
|
|
63
|
+
for idx in next_indices:
|
|
64
|
+
if next_best_class[idx] in [1, 3]:
|
|
65
|
+
params_next[idx] = 0.0
|
|
66
|
+
|
|
67
|
+
return sampled_class, I_sampled, params_sampled, next_best_class, I_next, params_next
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
def predict_best_params_rank(X_input, ANN_1, ANN_2):
|
|
4
|
+
"""
|
|
5
|
+
For a given input X_input, predict:
|
|
6
|
+
- best and next best class (from ANN_1)
|
|
7
|
+
- indices corresponding to these classes
|
|
8
|
+
- parameters predicted from ANN_2 (with zeros for class 1 and 3)
|
|
9
|
+
|
|
10
|
+
Args:
|
|
11
|
+
X_input : array-like, shape (n_features,)
|
|
12
|
+
ANN_1 : trained classification model
|
|
13
|
+
ANN_2 : trained regression model
|
|
14
|
+
|
|
15
|
+
Returns:
|
|
16
|
+
best_class, best_indices, best_parameters,
|
|
17
|
+
next_best_class, next_best_indices, next_best_parameters
|
|
18
|
+
"""
|
|
19
|
+
# Step 1: Predict classes with probabilities
|
|
20
|
+
pred_probs = ANN_1.predict_with_prob(np.array([X_input]))
|
|
21
|
+
pred_original = ANN_1.inverse_transform_predictions(pred_probs)
|
|
22
|
+
|
|
23
|
+
# Extract probabilities and predicted class
|
|
24
|
+
classes = np.hstack([d['predicted_class_original'] for d in pred_original])
|
|
25
|
+
probs = np.vstack([d['probabilities'] for d in pred_original]).squeeze() # shape (n_outputs, n_classes)
|
|
26
|
+
|
|
27
|
+
# Step 2: Get best and next best class for each output
|
|
28
|
+
best_class = np.argmax(probs, axis=1) + 1 # +1 if classes start from 1
|
|
29
|
+
|
|
30
|
+
# Step 3: Get indices of each class
|
|
31
|
+
def get_class_indices(classes_array):
|
|
32
|
+
return {cl: np.where(classes_array == cl)[0] for cl in range(1, 5)}
|
|
33
|
+
|
|
34
|
+
I_best = get_class_indices(best_class)
|
|
35
|
+
|
|
36
|
+
best_indices = np.concatenate(list(I_best.values()))
|
|
37
|
+
|
|
38
|
+
# Step 4: Predict parameters using ANN_2
|
|
39
|
+
params = ANN_2.predict(np.array([X_input])).flatten()
|
|
40
|
+
|
|
41
|
+
# for idx in best_indices:
|
|
42
|
+
# if best_class[idx] in [1, 3]:
|
|
43
|
+
# params[idx] = 0.0
|
|
44
|
+
best_parameters = params.copy()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
return best_class, best_parameters
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Deepanshu Yadav
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|