All articles →

100 Exercises / Mathematical optimization / Mathematical Optimization 100 Exercises

Introduction to Convex Optimization in Manufacturing | 10 Practical Tips for Python to Balance Quality and Equipment Constraints

Study convex optimization in Python: feasible sets, duality, constrained molding settings, regularization, Lasso, and the limits of convex models.

Protecting quality and equipment constraints simultaneously: 10 exercises to determine molding conditions explainably through convex optimization

Exercises No.051–No.060 use a fictional molding process to study convex optimization. We model quality loss and equipment limits, then use optimality conditions and dual variables to explain the recommended settings.

About the example data This article adapts a training notebook used by Suri Kobo or its representative, Hiroshi Wayama, with permission from the client. All data are fictional and do not describe actual companies or factories.

Manufacturing problem

Molding quality does not necessarily improve simply by increasing resin temperature and holding pressure. Insufficient conditions cause filling defects, excessive conditions lead to thermal degradation and increased burrs, and it is also necessary to maintain facility capacity, power consumption, and the range of changes from standard conditions. In this article, we will convert these questions from “questions that test candidate conditions by intuition” to “problems that can be recalculated by clearly stating the purpose and constraints.”

Common planning difficulties

  • Each expert has different recommended conditions, and the evidence is only verbal.
  • KPIs for quality, cycle time, and power are managed separately, preventing overall optimization.
  • Even if conditions are set to the limit limit, it cannot explain the value of investing in limit relaxation.
  • As a result of increasing the number of sensors, the number of explanatory variables increases too much, making model operation unstable.

Why the decision is difficult

This is because there are multiple objectives, variables interact, and constraints must be met simultaneously. However, if the feasible domain is a convex set and the objective function is a convex function, the local solution becomes a global solution, allowing you to audit results using optimality conditions or dual variables. On the other hand, if the actual process includes setup or multiple stabilization regions, convexity is compromised. This article covers both the “range where convex optimization can be used” and the “signs that it can’t be used.”

Exercises in this article

No.ThemeJudgment in the manufacturing industry
051convex setIs it safe to average the conditions?
052convex functionCan you assess losses in the intermediate condition from above?
053The Importance of Convex OptimizationCan it be reproduced without relying on the initial value?
054first-order conditionCan we verify that the solution is optimal?
055Lagrange DualCan we gain both the lower bound and economic value from constraints?
056strong dualityDo the primal problem and dual problem coincide?
057Constrained Convex OptimizationCan conditions be set within the actual equipment range?
058regularizationCan you keep the amount and complexity of changes down?
059LassoCan you narrow down the key sensors?
060Limitations of Convex OptimizationAre you overlooking non-convex properties?

Preparing the Python environment

No external data is used. Fix the random number generator so that the same data and results can be reproduced. For optimization, use SciPy, for Lasso scikit-learn, and for visualization, Matplotlib.

Run each article in a fresh kernel, starting with [IN: 1] in order. Each [OUT: n] corresponds to the input with the same number. Recorded versions and timings describe the saved output; final digits and solver termination states may vary by environment.

Japanese plot labels use japanize_matplotlib.

Show environment setup
[IN: 1]
# Run this article from top to bottom in a fresh notebook kernel.
import platform
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import scipy
import sklearn
import japanize_matplotlib

from IPython.display import display
from scipy.optimize import minimize
from sklearn.linear_model import Lasso
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

SEED = 20260712
rng = np.random.default_rng(SEED)
plt.rcParams["figure.figsize"] = (7.2, 4.5)
plt.rcParams["axes.unicode_minus"] = False

pd.DataFrame(
    {
        "package": ["Python", "NumPy", "pandas", "SciPy", "scikit-learn", "Matplotlib"],
        "version": [
            platform.python_version(),
            np.__version__,
            pd.__version__,
            scipy.__version__,
            sklearn.__version__,
            matplotlib.__version__,
        ],
    }
)
[OUT: 1]
package version
0 Python 3.13.1
1 NumPy 2.5.1
2 pandas 3.0.3
3 SciPy 1.18.0
4 scikit-learn 1.9.0
5 Matplotlib 3.11.0

Creating the example data

For a molding process, convert temperature TT and holding pressure PP to dimensionless settings:

x1=T−19010,x2=P−8010x_1=\frac{T-190}{10},\qquad x_2=\frac{P-80}{10}

Define quality loss on this scaled coordinate system as

f(x)=2(x1−0.6)2+1.5(x2−0.4)2+0.6(x1−x2)2.\begin{aligned}f(x)={}&2(x_1-0.6)^2+1.5(x_2-0.4)^2\\&+0.6(x_1-x_2)^2.\end{aligned}

The quadratic has a positive definite Hessian, so the loss is strictly convex. We add measurement noise to the observations while retaining the explicit reference model for optimization. The noisy loss index can be negative; it is not a defect rate.

Show data preparation
[IN: 2]
n = 90
x1_obs = rng.uniform(-1.2, 1.5, n)
x2_obs = rng.uniform(-1.0, 1.4, n)


def quality_loss(x):
    x = np.asarray(x)
    return (
        2.0 * (x[..., 0] - 0.6) ** 2
        + 1.5 * (x[..., 1] - 0.4) ** 2
        + 0.6 * (x[..., 0] - x[..., 1]) ** 2
    )


X_obs = np.column_stack([x1_obs, x2_obs])
loss_obs = quality_loss(X_obs) + rng.normal(0, 0.18, n)
process_df = pd.DataFrame(
    {
        "resin_temperature_c": 190 + 10 * x1_obs,
        "holding_pressure_mpa": 80 + 10 * x2_obs,
        "quality_loss_index": loss_obs,
    }
)
display(process_df.head())
display(process_df.describe().round(2))
[OUT: 2]
resin_temperature_c holding_pressure_mpa quality_loss_index
0 196.697726 81.129631 -0.211684
1 191.024661 77.871133 0.990317
2 201.472181 90.085739 0.922645
3 189.287092 92.945395 3.540957
4 178.914627 91.178000 9.526144
resin_temperature_c holding_pressure_mpa quality_loss_index
count 90.00 90.00 90.00
mean 191.54 82.06 3.06
std 8.15 6.57 2.43
min 178.16 70.52 -0.21
25% 184.73 76.48 1.31
50% 191.38 82.21 2.18
75% 198.36 87.04 4.83
max 204.94 92.97 9.53

No.051: What is a Convex Set?

Meaning in Practice

If all conditions connecting two safe driving conditions are also safe, then the condition region is a convex set. This is important when gradually changing conditions on site or when considering the average load of multiple products.

Approach to Analysis and Modeling

A set CC is convex if θx+(1−θ)y∈C\theta x+(1-\theta)y\in C holds for any x,y∈Cx,y\in C and 0≤θ≤10\le\theta\le1. Here, the common area between the upper and lower limits of temperature and pressure and the total load x1+x2≤1.2x_1+x_2\le1.2 is considered the feasible range. The common part of linear inequalities is the convex set.

Check with Python

[IN: 3]
def feasible(x):
    a, b = np.asarray(x)
    return (-1 <= a <= 1.4) and (-1 <= b <= 1.3) and (a + b <= 1.2)


A, B = np.array([-0.8, 1.1]), np.array([1.1, -0.4])
theta = np.linspace(0, 1, 21)
segment = theta[:, None] * A + (1 - theta[:, None]) * B

g = np.linspace(-1.3, 1.6, 240)
G1, G2 = np.meshgrid(g, g)
mask = (G1 >= -1) & (G1 <= 1.4) & (G2 >= -1) & (G2 <= 1.3) & (G1 + G2 <= 1.2)
plt.contourf(
    190 + 10 * G1,
    80 + 10 * G2,
    mask.astype(int),
    levels=[0.5, 1.5],
    alpha=0.35,
    colors=["tab:blue"],
)
plt.plot(
    190 + 10 * segment[:, 0],
    80 + 10 * segment[:, 1],
    "o-",
    color="tab:orange",
    label="Segment between two settings",
)
plt.scatter(
    190 + 10 * np.array([A[0], B[0]]),
    80 + 10 * np.array([A[1], B[1]]),
    color="black",
    zorder=3,
    label="endpoint",
)
plt.title("Convex feasible region and interpolation")
plt.xlabel("resin_temperature [°C]")
plt.ylabel("holding_pressure [MPa]")
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
print(f"All sampled segment points are feasible: {all(map(feasible, segment))}")
[OUT: 3]

No.051: computed result (figure 1)

All sampled segment points are feasible: True

Reading the results

All orange line segments connecting two points within the blue region remain within the region. With this property, you can reduce the worry of sudden constraint violations caused by conditional interpolation. However, the ‘porous region’ where only the center of the temperature zone becomes unstable is non-convex. The definition of safety areas is verified not only by equipment specifications but also by the expertise of the quality assurance department.

No.052: What is a Convex Function?

Meaning in Practice

In a convex loss function, the loss when adopting the middle of extreme two conditions does not exceed the weighted average of losses at both ends. It makes it easier to assess the risks of changing conditions, and the direction of improvement is consistent.

Approach to Analysis and Modeling

A function ff is convex if f(θx+(1−θ)y)≤θf(x)+(1−θ)f(y)f(\theta x+(1-\theta)y)\le\theta f(x)+(1-\theta)f(y) holds. In smooth quadratic functions, if all Hessian eigenvalues are non-negative, it is convex. Numerically check the curvature of this model and visualize the margins of Jensen’s inequality.

Check with Python

[IN: 4]
H = np.array([[5.2, -1.2], [-1.2, 4.2]])
eig = np.linalg.eigvalsh(H)
lhs = quality_loss(segment)
rhs = theta * quality_loss(A) + (1 - theta) * quality_loss(B)
display(pd.DataFrame({"Hessianeigenvalue": eig}).round(3))
plt.plot(theta, lhs, marker="o", label="Actual losses under intermediate conditions")
plt.plot(theta, rhs, "--", label="Weighted mean of endpoint loss (upper bound)")
plt.title("Jensen's inequality for a convex loss")
plt.xlabel("endpointAMixing ratio θ")
plt.ylabel("Quality loss index")
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
print(f"Maximum violation of Jensen's inequality: {np.max(lhs-rhs):.3e}")
[OUT: 4]
Hessianeigenvalue
0 3.4
1 6.0

No.052: computed result (figure 1)

Maximum violation of Jensen's inequality: 0.000e+00

Reading the results

Hessian’s eigenvalues are both positive, and the actual loss is below the upper bound. Therefore, this model is strictly convex and has only one valley bottom. In practice, even if the estimated response surface appears convex, it is not guaranteed to be outside the observation range. Leave uncertainties in the model ledger regarding the range of application and curvature.

No.053: Why Convex Optimization Is Important

Meaning in Practice

If the recommended conditions can be reached even if the person in charge or initial conditions change, the repeatability and auditability of the condition table improve. In convex problems, the locally optimal solution is also the global optimum, greatly reducing dependence on the search starting point.

Approach to Analysis and Modeling

Minimize the same convex objective function from different initial values and check the variation between the solution and the objective value. Since calculation tolerance remains, the physical unit is evaluated not as a perfect match but as a sufficiently small difference.

Check with Python

[IN: 5]
starts = np.array([[-1, -1], [-1, 1], [1.3, -0.8], [1.2, 1.1], [0, 0]])
rows = []
for s in starts:
    result = minimize(quality_loss, s, method="BFGS", options={"gtol": 1e-10})
    rows.append([*s, *result.x, result.fun, result.nit, result.success])
convex_runs = pd.DataFrame(
    rows,
    columns=[
        "early stagex1",
        "early stagex2",
        "solutionx1",
        "solutionx2",
        "Final loss",
        "iterations",
        "Success",
    ],
)
convex_runs["optimal_temperature_c"] = 190 + 10 * convex_runs["solutionx1"]
convex_runs["optimal_pressure_mpa"] = 80 + 10 * convex_runs["solutionx2"]
display(convex_runs.round(6))
print(
    f"Maximum difference between solutions: {np.ptp(convex_runs[['solutionx1','solutionx2']].to_numpy(), axis=0).max():.2e}"
)
[OUT: 5]
early stagex1 early stagex2 solutionx1 solutionx2 Final loss iterations Success optimal_temperature_c optimal_pressure_mpa
0 -1.0 -1.0 0.564706 0.447059 0.014118 5 False 195.647059 84.470588
1 -1.0 1.0 0.564706 0.447059 0.014118 6 True 195.647059 84.470588
2 1.3 -0.8 0.564706 0.447059 0.014118 4 False 195.647059 84.470588
3 1.2 1.1 0.564706 0.447059 0.014118 4 True 195.647059 84.470588
4 0.0 0.0 0.564706 0.447059 0.014118 6 True 195.647059 84.470588
Maximum difference between solutions: 1.45e-10

Reading the results

The returned settings and objective values nearly coincide, but some displayed success flags are False. This run uses numerical differentiation with a stringent gradient tolerance of 10−1010^{-10}, so its termination messages and gradient residuals need checking. No.054 verifies optimality using an analytic gradient. Convexity does not guarantee successful termination under arbitrary numerical settings.

No.054: First-Order and Optimality Conditions

Meaning in Practice

We check that the recommended conditions are not just search results, but points that cannot be improved by minor changes to the conditions. In a condition change meeting, you can quantitatively respond to suggestions like “Wouldn’t it be better to raise the temperature a little?”

Approach to Analysis and Modeling

For differentiable convex functions, the necessary and sufficient condition for an unconstrained point x∗x^* to be optimal is ∇f(x∗)=0\nabla f(x^*)=0. Also, the support hyperplane of the first order

f(y)≥f(x)+∇f(x)⊤(y−x)f(y)\ge f(x)+\nabla f(x)^\top(y-x)

is valid in any x,yx,y. Check the gradient norm and the first-order lower bound.

Check with Python

[IN: 6]
def quality_grad(x):
    a, b = x
    return np.array([4 * (a - 0.6) + 1.2 * (a - b), 3 * (b - 0.4) - 1.2 * (a - b)])


x_star = minimize(quality_loss, [0, 0], jac=quality_grad, method="BFGS", tol=1e-12).x
x_base = np.array([-0.3, 0.8])
y_test = rng.uniform(-1, 1.3, size=(200, 2))
first_order_lower = quality_loss(x_base) + (y_test - x_base) @ quality_grad(x_base)
gap = quality_loss(y_test) - first_order_lower
display(
    pd.Series(
        {
            "optimal_x1": x_star[0],
            "optimal_x2": x_star[1],
            "gradient norm": np.linalg.norm(quality_grad(x_star)),
            "minimum_first_order_lower_bound_margin": gap.min(),
        }
    ).round(10)
)
[OUT: 6]
optimal_x1                                0.564706
optimal_x2                                0.447059
gradient norm                             0.000000
minimum_first_order_lower_bound_margin    0.024987
dtype: float64

Reading the results

The gradient norm for the optimal point is almost zero, and at all tested points, the first-order lower bound is less than the actual loss. If there is a constraint, the gradient may not reach zero, and in that case, the balance between the normal of the constraint and the target gradient, i.e., the KKT condition, is used to verify the calculation.

No.055: Duality and Lagrange Dual

Meaning in Practice

If the upper limit of the equipment load restricts optimal conditions, the value of slightly relaxing the limit can be expressed as an amount or loss index. This leads to prioritizing increased investment and bottleneck improvement.

Approach to Analysis and Modeling

To clarify the explanation, minimize the square distance to target condition c=(0.9,0.7)c=(0.9,0.7) and impose capability constraints x1+x2≤bx_1+x_2\le b.

min⁡x 12∥x−c∥2,x1+x2−b≤0\min_x\ \frac12\lVert x-c\rVert^2,\qquad x_1+x_2-b\le0

Minimizing Lagrangian L(x,λ)=12∥x−c∥2+λ(x1+x2−b)L(x,\lambda)=\frac12\lVert x-c\rVert^2+\lambda(x_1+x_2-b) with respect to xx, the dual function is g(λ)=−λ2+λ(c1+c2−b)g(\lambda)=-\lambda^2+\lambda(c_1+c_2-b). In any λ≥0\lambda\ge0, g(λ)g(\lambda) is the lower bound of the primal problem optimal value.

Check with Python

[IN: 7]
c = np.array([0.9, 0.7])
capacity = 1.2
dual_lambda = np.linspace(0, 0.8, 161)
dual_value = -(dual_lambda**2) + dual_lambda * (c.sum() - capacity)
lambda_star = (c.sum() - capacity) / 2
primal_x = c - lambda_star * np.ones(2)
primal_value = 0.5 * np.sum((primal_x - c) ** 2)
plt.plot(dual_lambda, dual_value, label="dual function g(λ)")
plt.axhline(
    primal_value,
    color="tab:red",
    linestyle="--",
    label="Primal optimal value",
)
plt.scatter([lambda_star], [dual_value.max()], color="black", zorder=3)
plt.title("Lower bound of the optimal value by dual functions")
plt.xlabel("dual variable λ")
plt.ylabel("Target value/lower world")
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
display(
    pd.Series(
        {
            "optimal_lambda": lambda_star,
            "optimal_x1": primal_x[0],
            "optimal_x2": primal_x[1],
            "primal_value": primal_value,
            "dual_maximum": dual_value.max(),
        }
    ).round(4)
)
[OUT: 7]

No.055: computed result (figure 1)

optimal_lambda    0.20
optimal_x1        0.70
optimal_x2        0.50
primal_value      0.04
dual_maximum      0.04
dtype: float64

Reading the results

The dual function does not exceed the primal problem value for any non-negative λ and matches at optimal λ. λ is the rate of improvement in the target value when the capability limit is slightly relaxed. However, the scale depends on the unit of the objective function. If you use it for investment decisions, convert the quality loss index to yen and verify the sign and size even for finite differences.

No.056: Strong Duality

Meaning in Practice

If the best value of the primal problem matches the lower bound of the dual problem, the quality of the solution can be numerically proven. Even for large-scale problems that cut off computation time, the dual gap serves as an indicator of “how much more room for improvement” remains.

Approach to Analysis and Modeling

For a convex problem, a condition such as Slater’s condition ensures strong duality. In this example, it requires a point strictly satisfying the inequality. In the previous question, (0,0)(0,0) satisfies x1+x2<1.2x_1+x_2<1.2, so this condition is met. Change the ability cap and compare the main value, dual value, and sensitivity.

Check with Python

[IN: 8]
records = []
for b in [0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 1.8]:
    lam = max(0.0, (c.sum() - b) / 2)
    x = c - lam * np.ones(2)
    pv = 0.5 * np.sum((x - c) ** 2)
    dv = -(lam**2) + lam * (c.sum() - b)
    records.append([b, *x, lam, pv, dv, pv - dv])
duality_df = pd.DataFrame(
    records,
    columns=[
        "Ability Ceilingb",
        "x1",
        "x2",
        "λ",
        "primal_value",
        "dual value",
        "Dual gap",
    ],
)
display(duality_df.round(8))
plt.plot(
    duality_df["Ability Ceilingb"],
    duality_df["primal_value"],
    "o-",
    label="Minimal loss",
)
plt.plot(
    duality_df["Ability Ceilingb"],
    duality_df["λ"],
    "s--",
    label="The Marginal Value of Constraint Relaxation λ",
)
plt.title("The relationship between facility capacity and losses and dual variables")
plt.xlabel("Capacity limit b")
plt.ylabel("Target value/dual variable")
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
[OUT: 8]
Ability Ceilingb x1 x2 λ primal_value dual value Dual gap
0 0.9 0.55 0.35 0.35 0.1225 0.1225 -0.0
1 1.0 0.60 0.40 0.30 0.0900 0.0900 0.0
2 1.1 0.65 0.45 0.25 0.0625 0.0625 0.0
3 1.2 0.70 0.50 0.20 0.0400 0.0400 0.0
4 1.3 0.75 0.55 0.15 0.0225 0.0225 0.0
5 1.4 0.80 0.60 0.10 0.0100 0.0100 0.0
6 1.6 0.90 0.70 0.00 0.0000 0.0000 0.0
7 1.8 0.90 0.70 0.00 0.0000 -0.0000 0.0

No.056: computed result (figure 1)

Reading the results

The duality gaps are within rounding error. Tighter capacities have larger λ. At b=1.6b=1.6, the constraint is active because equality holds, yet λ is zero. For b>1.6b>1.6, it has positive slack and becomes inactive. An active constraint need not have a positive multiplier.

No.057: Constrained Convex Optimization

Meaning in Practice

If the ideal conditions focused solely on quality are outside the equipment range, it cannot be implemented. Implementable conditions must be obtained that simultaneously satisfy the upper and lower limits of temperature and pressure, total load, and changes from standard conditions.

Approach to Analysis and Modeling

Combine linear inequality constraints with the convex objective function. We solve using SciPy’s SLSQP, but the results are checked not only by success flags but also by the margin for all constraints and comparison of target values with nearby candidates. Constraints are implemented in the form of g(x)≥0g(x)\ge0.

Check with Python

[IN: 9]
constraints = [
    {"type": "ineq", "fun": lambda x: x[0] + 1.0},
    {"type": "ineq", "fun": lambda x: 1.4 - x[0]},
    {"type": "ineq", "fun": lambda x: x[1] + 1.0},
    {"type": "ineq", "fun": lambda x: 1.3 - x[1]},
    {"type": "ineq", "fun": lambda x: 0.75 - x[0] - x[1]},
]
r57 = minimize(
    quality_loss,
    [0, 0],
    jac=quality_grad,
    method="SLSQP",
    constraints=constraints,
    options={"ftol": 1e-12, "maxiter": 500},
)
margins = np.array([con["fun"](r57.x) for con in constraints])
display(
    pd.Series(
        {
            "solver_success": r57.success,
            "optimal_temperature_c": 190 + 10 * r57.x[0],
            "optimal_pressure_mpa": 80 + 10 * r57.x[1],
            "quality_loss": r57.fun,
            "minimum_constraint_margin": margins.min(),
            "iterations": r57.nit,
        }
    )
)

Z = quality_loss(np.stack([G1, G2], axis=-1))
plt.contour(190 + 10 * G1, 80 + 10 * G2, Z, levels=12, cmap="viridis")
mask57 = (G1 >= -1) & (G1 <= 1.4) & (G2 >= -1) & (G2 <= 1.3) & (G1 + G2 <= 0.75)
plt.contourf(
    190 + 10 * G1,
    80 + 10 * G2,
    mask57.astype(int),
    levels=[0.5, 1.5],
    alpha=0.18,
    colors=["tab:blue"],
)
plt.scatter(
    190 + 10 * r57.x[0],
    80 + 10 * r57.x[1],
    marker="*",
    s=180,
    color="tab:red",
    label="Constrained optimal solution",
)
plt.title("Minimizing quality loss over the feasible region")
plt.xlabel("resin_temperature [°C]")
plt.ylabel("holding_pressure [MPa]")
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
[OUT: 9]
solver_success                     True
optimal_temperature_c        194.449153
optimal_pressure_mpa          83.050847
quality_loss                   0.073347
minimum_constraint_margin           0.0
iterations                            5
dtype: object

No.057: computed result (figure 1)

Reading the results

The optimal solution lies at the boundary of total load, and equipment capacity limits quality improvement. Since the minimum constraint margin is not negative, it is numerically feasible. However, considering control errors, you should not set boundary values as they are. In actual operation, safety margins are added according to temperature and pressure variations and further optimized under those conditions.

No.058: Regularization and Convex Optimization

Meaning in Practice

Proposals that drastically change conditions each time worsen setup load and site acceptance even if the quality is good. Regularization adjusts performance, “few changes,” and “simplicity of operation” using the same objective function.

Approach to Analysis and Modeling

Add L2 penalties to changes from the previous setting xprev=(−0.2,0.1)x_{prev}=(-0.2,0.1).

min⁡x f(x)+α∥x−xprev∥22\min_x\ f(x)+\alpha\lVert x-x_{prev}\rVert_2^2

The larger the α\alpha, the more priority is given to maintaining the status quo. Since both are convex functions, the sum is also convex as well. Compare the trade-offs between quality loss and change volume across multiple α\alpha.

Check with Python

[IN: 10]
x_prev = np.array([-0.2, 0.1])
reg_rows = []
for alpha in [0, 0.1, 0.3, 1, 3, 10]:
    objective = lambda x, a=alpha: quality_loss(x) + a * np.sum((x - x_prev) ** 2)
    r = minimize(objective, x_prev, method="BFGS")
    reg_rows.append(
        [alpha, *r.x, quality_loss(r.x), np.linalg.norm(r.x - x_prev), r.fun]
    )
reg_df = pd.DataFrame(
    reg_rows,
    columns=[
        "Regularization strengthα",
        "x1",
        "x2",
        "quality_loss",
        "Amount of setting changes",
        "Objective value including regularization",
    ],
)
display(reg_df.round(4))
plt.plot(reg_df["Amount of setting changes"], reg_df["quality_loss"], "o-")
for _, row in reg_df.iterrows():
    plt.annotate(
        f"α={row['Regularization strengthα']:g}",
        (row["Amount of setting changes"], row["quality_loss"]),
        xytext=(4, 4),
        textcoords="offset points",
    )
plt.title(
    "The trade-off between quality improvement and the amount of settings changed"
)
plt.xlabel("Distance from previous setting")
plt.ylabel("Quality loss index")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
[OUT: 10]
Regularization strengthα x1 x2 quality_loss Amount of setting changes Objective value including regularization
0 0.0 0.5647 0.4471 0.0141 0.8398 0.0141
1 0.1 0.5308 0.4220 0.0174 0.7986 0.0812
2 0.3 0.4718 0.3805 0.0384 0.7280 0.1974
3 1.0 0.3259 0.2889 0.1696 0.5588 0.4819
4 3.0 0.1277 0.1915 0.5139 0.3402 0.8611
5 10.0 -0.0573 0.1294 0.9949 0.1457 1.2071

No.058: computed result (figure 1)

Reading the results

Strengthening regularization reduces the amount of setting changes but increases quality loss. α are not management decisions that are automatically determined by mathematics. If possible, the downtime required for changing conditions, verification costs, and work risks will be converted into yen and agreed upon as an operating policy. A design that separates α between regular and emergency revisions is also effective.

No.059: Lasso and Convex Optimization

Meaning in Practice

When quality is explained from multiple sensors, unnecessary signals enter the model, increasing maintenance costs and false alarms. Lasso reduces prediction errors while reducing some coefficients to zero, creating candidates to narrow down the monitoring targets.

Approach to Analysis and Modeling

Lasso is

min⁡β0,β12n∥r∥22+α∥β∥1,r=y−β01−Xβ.\begin{aligned}\min_{\beta_0,\beta}\quad&\frac{1}{2n}\lVert r\rVert_2^2+\alpha\lVert\beta\rVert_1,\\r={}&y-\beta_0\mathbf{1}-X\beta.\end{aligned}

Both the squared error and the L1 norm in this objective are convex. To avoid unfairness due to unit differences, explanatory variables are standardized. Here, we generate fictitious data limited to four variables that truly affect the process: temperature, pressure, cooling time, and material moisture.

Check with Python

[IN: 11]
# Compare fitted and true coefficients on the same standardized scale.
n_lasso, p = 160, 12
sensor_names = [
    "resin_temperature",
    "holding_pressure",
    "Cooldown time",
    "Material moisture",
    "Mold temperature",
    "injection velocity",
    "Back pressure",
    "outside temperature",
    "oil temperature",
    "vibration",
    "standby time",
    "lot number",
]
X_sensor = rng.normal(size=(n_lasso, p))
true_beta = np.array([2.4, -1.8, 1.2, -0.9] + [0.0] * (p - 4))
y_quality = 10 + X_sensor @ true_beta + rng.normal(0, 0.9, n_lasso)

lasso_rows = []
coef_paths = []
alphas = np.logspace(-2.2, 0.2, 20)
for alpha in alphas:
    model = make_pipeline(StandardScaler(), Lasso(alpha=alpha, max_iter=20000))
    model.fit(X_sensor, y_quality)
    coef = model[-1].coef_
    pred = model.predict(X_sensor)
    lasso_rows.append(
        [
            alpha,
            np.count_nonzero(np.abs(coef) > 1e-8),
            np.sqrt(np.mean((y_quality - pred) ** 2)),
        ]
    )
    coef_paths.append(coef)
lasso_df = pd.DataFrame(
    lasso_rows, columns=["α", "Nonzero coefficient number", "LearningRMSE"]
)
selected_idx = np.argmin(np.abs(alphas - 0.18))
coef_table = pd.DataFrame(
    {
        "Sensors": sensor_names,
        "Lassocoefficient": coef_paths[selected_idx],
        "True coefficient (standardized)": true_beta * model[0].scale_,
    }
)
display(lasso_df.iloc[[0, 5, 10, 15, 19]].round(3))
display(
    coef_table.reindex(
        coef_table["Lassocoefficient"].abs().sort_values(ascending=False).index
    ).round(3)
)

coef_paths = np.asarray(coef_paths)
for j, name in enumerate(sensor_names):
    plt.plot(alphas, coef_paths[:, j], label=name)
plt.xscale("log")
plt.title("Lasso regularization path")
plt.xlabel("Regularization strength alpha (log scale)")
plt.ylabel("Standardized regression coefficient")
plt.grid(True, alpha=0.3)
plt.legend(bbox_to_anchor=(1.02, 1), loc="upper left", fontsize=8)
plt.tight_layout()
plt.show()
[OUT: 11]
α Nonzero coefficient number LearningRMSE
0 0.006 12 0.893
5 0.027 8 0.898
10 0.116 4 0.940
15 0.495 4 1.452
19 1.585 1 2.778
Sensors Lassocoefficient True coefficient (standardized)
0 resin_temperature 2.074 2.301
1 holding_pressure -1.513 -1.752
2 Cooldown time 1.043 1.176
3 Material moisture -0.766 -0.828
4 Mold temperature -0.000 0.000
5 injection velocity 0.000 0.000
6 Back pressure 0.000 0.000
7 outside temperature 0.000 0.000
8 oil temperature -0.000 0.000
9 vibration -0.000 0.000
10 standby time 0.000 0.000
11 lot number 0.000 0.000

No.059: computed result (figure 1)

Reading the results

In this dataset, larger α values leave fewer nonzero coefficients, and moderate α retains the four variables used to generate the response. The coefficient table compares estimates and true coefficients on the same standardized scale. Lasso selection does not establish causality, and support size need not decrease monotonically in general. Check predictive performance on unused data and retain monitoring required by process knowledge.

No.060: Limits of Convex Optimization

Meaning in Practice

While convex models are easy to solve and explain, if a process includes multiple stable operating zones, whether or not there is a setup, an integer equipment counts, or start/stop, it cannot fully represent reality. If you match the ‘solvable model’ to the actual site, it may look beautiful but may not be feasible.

Approach to Analysis and Modeling

Non-convex functions have multiple local solutions, and even if the first-order condition is met, it is not necessarily global optimal. For example, loss of a single variable

h(z)=(z2−1)2+0.18zh(z)=(z^2-1)^2+0.18z

Its second derivative h′′(z)=12z2−4h''(z)=12z^2-4 is negative near the origin and therefore nonconvex. Local optimization is performed from different initial values and compared to the best values of the global grid.

Check with Python

[IN: 12]
def nonconvex_loss(z):
    z = np.asarray(z)
    return (z**2 - 1) ** 2 + 0.18 * z


z_grid = np.linspace(-1.8, 1.8, 1200)
nc_rows = []
for start in [-1.6, -0.5, 0.2, 0.8, 1.6]:
    r = minimize(lambda q: float(nonconvex_loss(q[0])), [start], method="BFGS")
    nc_rows.append([start, r.x[0], r.fun, r.success])
nc_df = pd.DataFrame(
    nc_rows,
    columns=["initial value", "arrival point", "local objective value", "Success"],
)
global_i = np.argmin(nonconvex_loss(z_grid))
display(nc_df.round(5))
print(
    f"Best sampled grid point: z={z_grid[global_i]:.4f}, loss={nonconvex_loss(z_grid[global_i]):.4f}"
)
plt.plot(z_grid, nonconvex_loss(z_grid), label="Non-convex process losses")
plt.scatter(
    nc_df["arrival point"],
    nc_df["local objective value"],
    color="tab:red",
    zorder=3,
    label="Points from each initial value",
)
plt.title("In non-convex problems, the initial value changes the reached solution")
plt.xlabel("Standardized process conditions z")
plt.ylabel("loss index")
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
[OUT: 12]
initial value arrival point local objective value Success
0 -1.6 -1.02178 -0.18198 True
1 -0.5 -1.02178 -0.18198 True
2 0.2 0.97669 0.17793 True
3 0.8 0.97669 0.17793 True
4 1.6 0.97669 0.17793 True
Best sampled grid point: z=-1.0224, loss=-0.1820

No.060: computed result (figure 1)

Reading the results

Even if solvers “succeed” at each starting point, they will reach different valleys on the left and right, with different target values. If nonconvexity is suspected, consider multi-point initialization, global search, mixed integer optimization, simulation, and expert candidate area segmentation. When using convex approximation, the feasibility and optimality gap against the original constraint are evaluated separately.

Practical implications

  1. First, check the convexity: Checking the feasible domain and the structure of the objective function changes not only the solution method but also the explainability.
  2. Prioritize validation over optimal values: Gradients, constraint margins, dual gaps, and results from different initial values.
  3. Connecting dual variables to investment decisions: Convert the mitigated value of active constraints into yen and compare candidates for capacity enhancement.
  4. Designing Regularization as an Operational Cost: Explicitly incorporate settings for changes, sensor maintenance, and model complexity.
  5. Don’t hide the reality that isn’t convex: If there are planning, integer determination, multiple operating regions, or discontinuous quality judgments, you need a different model or solution.

Putting the model into practice

  • Agreement on objective functions: Defects, reprocessing, power consumption, downtime, and delivery delays are converted into common units and approved across departments.
  • Ledger of constraints: Manage the basis, responsible person, and update date regarding equipment limits, quality standards, safety margins, and possible change ranges.
  • Checking Data Quality: Audit calibration history, missing measurements, time synchronization, lot differences, and the scope of operation for condition changes.
  • Offline Verification: Testing reproduction with historical data, sensitivity analysis, extrapolation detection, and fail-safe during abnormal inputs
  • On-site Verification Tests: Gradually test recommended conditions and evaluate standard margins, including control variation.
  • Operational Monitoring: Record objective values, constraint margins, input distribution, forecast errors, and reasons for re-optimization, and reflect them with approval.

Conclusion

Convex optimization is not simply a method that solves quickly. It is a decision-making framework that can consistently explain safe condition areas, loss patterns, optimality conditions, and the value of constraints. In this article, we reviewed everything from convex sets and convex functions to strong duality, constrained optimization, Lasso, and key points for non-convex problems, all through the flow of molding condition design. In practice, it is important to separate and verify the convexity of the model from its real-world validity, and to translate the optimal solution into conditions that can be safely implemented.

Business inquiries

At Suri Kobo, we support everything from problem organization to PoC and production operation design, covering the integration of manufacturing condition optimization, production planning, equipment capability evaluation, and quality forecasting and optimization. You can consult with us starting from the stages of “how to define objective functions” and “how to model constraints and on-site rules.”

📩 Contact Us: www.surikobo.co.jp/contact Please feel free to consult us first.