# Troubleshoot Optimization Failures

There are several issues for you to consider when troubleshooting an optimization failure.

## Examine the log file for the optimization iteration history

A portion of the four-bar log file is reproduced below.

Iteration #   Cost #    Objective       Mag (Slope)
--------------------------------------------------
1             1      1.1368e+04      5.9417e+02
2             3      2.1944e+03      7.9392e+01

...
...
...

29            39      2.3447e+00      1.9977e-01
30            40      2.3199e+00      4.2502e-02
31            41      2.3041e+00      3.9178e-02


The objective, its slope, and the maximum constraint violation must all decrease with the iteration count. A decrease indicates progress.

If the cost function, the slope, and maximum constrain violation are decreasing, but have not reached their specified accuracies, increase maxit, the number of iterations that are allowed for optimization as shown below for the four-bar example. The default value of maxit is 50.

def optimize (self):
obj = [self.a2x, self.a2y, self.a2psi]
wt  = [1,1,1]
opt = Optimizer (
label     = "Optimize RMS2",  # Label
objective = obj,              # Objectives
weights   = wt,               # Weights
type      = "KINEMATICS",     # Simulation Type
end       = 2,                # End Time
dtout     = 0.01,             # No. of steps
plot      = True,             # Display plots
)

# Now do the optimization
x = opt.optimize (maxit=100, accuracy=1e-3)  # Was maxit=50

return x

If the above is not true, you can try to relax the solution tolerance. A looser tolerance will help the optimizer converge.

# Now do the optimization
x = opt.optimize (maxit=100, accuracy=1e-2)    # Was accuracy=1e-3


## Check your cost and constraint functions

Make sure that the cost function and constraint functions are correct. The debug feature for Responses will help with this task.
• Evaluate the cost function at a few points to make sure it is returning the correct value.
• Check the sign of each inequality constraint.
• If you are performing a maximization, make sure to negate the cost.

## The solution is not robust

For nonlinear problems, the size and range of the design space can play an important role in stabilizing the solution. Optimizers run most reliably when each of the design variables has about the same effect on the objective and constraint functions.

The solution for this issue is to redefine the design variables, such that they have roughly the same impact on the various cost and constraint functions. You can use the scaling features in the Optimizer. Automatic scaling could help if your own strategy doesn’t work. See Scaling Optimization Problem for more details.

## Optimizer reaches an infeasible point

Often, the optimizer may go into an infeasible domain. Essentially, in the vicinity of the chosen design, one or more constraints or design limits are violated. MotionSolve has built-in methods to recover from such situations. It will instruct the optimizer to go back to the last feasible design and choose a smaller design step. Usually, this works quite well, but you may encounter a situation where MotionSolve is unable to recover.

If the optimizer started at a feasible point, but converged to an infeasible point, try the following techniques:
• Tighten the bounds for some of the design variables. Increase the lower bound bU and decrease the upper bound bL. This prevents the optimizer from wandering off into an infeasible region.
• Change the initial design so that the optimizer is starting out from a different part of the design space. Since you are dealing with a nonlinear problem, changing the initial design can help the optimizer find a solution.
If none of the above interventions work, you can try the following strategy to get back to a feasible region:
• Set the cost function to zero.
• Run the optimizer – it will change the design so that all the constraints are satisfied.
• Use this new design as your starting point.