# MV-3022: Optimize a 4-Bar Model

In this tutorial you will setup an optimization problem using MotionSolve's Optimization Wizard for a 4-bar model.

You will learn about the following:
• Defining point coordinates as design variables
• Defining a response type 'Root Mean Square Deviation' for matching curves
• Using the responses as objectives
• Running the optimization and post-processing the results
• [Optional] Infeasible Designs in MotionSolve optimization
Introduction
In this tutorial, the locations of the joints of a 4-bar mechanism are optimized to obtain a desired motion of the coupler. MotionSolve's DSA (Design Sensitivity Analysis) capability is used to calculate sensitivities. The entire model is parameterized in terms of these four design points: A, B, C and D. Since the model operates in 2D space (X-Y plane), this leads to 8 Design Variables. The initial design is listed as in Table 1:
Table 1.
Point X Y
A -45 45
B 65 260
C 300 500
D 515 -85

## Review the Model

In this step, you will review the 4-bar model in MotionSolve.

Before you begin, copy the file mv_3022_initial_4bar_opt.mdl located in the mbd_modeling\motionsolve\optimization\MV-3022 into your <working directory>.
1. Open the file mv_3022_initial_4bar_opt.mdl in MotionSolve.
2. Review the entities in the model.
1. Verify the presence of curves Desired Coupler Path DX and Desired Coupler Path DY, which define the desired motion in the X and Y directions.
2. Verify the presence of output Desired Coupler Trajectory, which uses the curves to measure the desired motion.
3. Verify the presence of output Actual Coupler Trajectory, which measures the displacement of the coupler body at the CM.

1. In the Project Browser, right-click on Model and select Optimization Wizard from the context menu.
2. In the Design Variables page, click on the Points tab.
The points are listed in Figure 3:
3. For points A, B, C, and D, make the x and y coordinates design variables. Click the (Filter datamembers) icon.
4. In the Filter dialog, click to turn off the Z check box.
5. Click OK.
6. In the Model Tree, select Point A, Point B, Point C, and Point D. Then click Add.
7. Modify the upper and lower bounds of the design variables according to Table 2:
Table 2.
DV Lower Bound Upper Bound
Point A(DV) - X -50 50
Point A(DV) - Y -50 50
Point B(DV) - X 20 80
Point B(DV) - Y 180 280
Point C(DV) - X 240 380
Point C(DV) - Y 400 620
Point D(DV) - X 180 520
Point D(DV) - Y -100 20
You have finished the process of setting design variables.

Now you will add response variables to the optimization.

The objective of this optimization is to make the coupler move along a path. To achieve this, you will add two responses:
• Trajectory of Coupler CM – DX
• Trajectory of Coupler CM – DY
The above curves of the coupler motion must be matched with their respective target curves using a response variable of type Root Mean Square Deviation (RMS2). The details of this response can be obtained from the Multibody Optimization User’s Guide and the MotionView User’s Guide.
1. Click on the Responses page.
2. Click on the button.
3. Change the Label to 'rms2_dx'.
4. Click OK to create a response variable.
5. Once the response variable is created, under Response Type, choose Root Mean Square Deviation.
6. Double-click the Curve collector and choose Desired Coupler Path DX for the Desired Curve user input. This is the target curve for Couple-DX.
7. For Response Expression, type the expression DX({b_bc.cm.id}) (DX of CM coupler body).
8. Follow the steps again to create one more Root Mean Square Deviation response. Use Table 3 to fill in the details.
Table 3.
RV Desired Curve Response Expression
rms2_dx Desired Coupler Path DX DX({b_bc.cm.id})
rms2_dy Desired Coupler Path DY DY({b_bc.cm.id})
The completed Responses page should appear as shown in Figure 5:

In this step you will add two objectives to the problem.

The objectives of the problem are as follows: the coupler DX and DY motions should match with target curves at the end of optimization. You can use the responses that were created in the previous section as objectives.

1. Navigate to the Goals page.
2. Under Objectives, click .
This will add an objective with the response rv_rms2_dx.
3. For Weight choose 1.0 retain the Type as min (you want to minimize the response).
4. Repeat these steps to add another objective. Choose rv_rms2_dy for the second objective.
5. For Weight choose 1.0 retain the Type as min.
There are no constraints in this problem, so you have now finished model setup. The model is ready to solve.

## Run the Optimization

Now you will run the optimization.

1. Navigate to the Solutions page.
2. Activate the Plot Optimization Summary check box.
3. Click on Save & Optimize to start the optimization.
Once the optimization process is complete, the text window displays the optimized design variables values, final value of the responses and optimized cost function. The expected values of design variables are provided in Table 4:
Table 4.
DV Expected From Optimizer
p_0.x 0 -0.0660
p_0.y 0 -0.2796
p_1.x 40 39.97
p_1.y 200 199.71
p_2.x 360 360.07
p_2.y 600 600.26
p_3.x 400 400.44
p_3.y 0 -0.0406

## Post-Process

In this step you will post-process the results of the optimization run.

1. Once the solution is finished, navigate to the Review Results page.
The Summary tab lists the history of design variables, responses and objective functions in a tabular format. In this tutorial, the optimized design variables are from iteration 27.
2. Click the Plot tab. Plot Overall Objective versus Iteration variable.
The Plot tab helps to visualize variation of design variables, response variables and cost function using graphs. You can choose to plot any number of variables along the y-axis with respect to a variable along the x-axis.
Note: You can also see how a particular design variable varied across an iteration. For example, select p_b.x (Point B-X) in the Y list and click Apply.
3. Animate the configuration generated during any iteration.
1. In the Animation tab, from the Iteration drop-down menu choose the iteration number you want to animate (for this tutorial, choose Iteration 27.
2. Click Load Result to load the animation corresponding to the iteration you chose.
From this tab, you also have the option to export an archive of the model from any configuration.
4. Load the results from Iteration 27.
5. Use the Archive Model location browser to choose a file path.
6. Click Export.
7. Observe how the path of the coupler for the optimized result compares with the desired target path.
1. Close the Optimization Wizard.
3. Change the client in the new page to HyperGraph.
4. Open the .abf file in the last iteration folder of the optimization run.
Note: The optimization run directory is automatically created during the solution and follows the naming convention py_name_timestamp where py_name is the name of the Python file given in the Solution page and timestamp is the time when the solution was initiated.
5. For the X and Y axis, select the following Type, Request, and Component:
Table 5.
Axis Type Request Component
X Expressions REQ/70000000 Desired Coupler Trajectory F2
Y Expressions REQ/70000000 Desired Coupler Trajectory F3
6. Click Apply.
7. Plot the actual trajectory of the coupler CM using the following X and Y axis specifications:
Table 6.
Axis Type Request Component
X Marker Displacement REQ/70000003 Actual Coupler Trajectory - (on Coupler) (on 'Coupler') X
Y Marker Displacement REQ/70000003 Actual Coupler Trajectory - (on Coupler) (on 'Coupler') Y
The resulting plots will overlay each other.

## Identify Infeasible Designs/Possible Recovery (Optional)

In this step, you will use a powerful feature in MotionSolve to obtain the optimal design by recovering from a failed iteration.

It is not uncommon for a mechanical system to be limited by some sort of physical constraints. In this four bar example, the Grashof constraints have to be satisfied in order to make the input link a crank. When optimizer chooses a new design, those constraints are likely to be violated regardless of whether they are added as optimization constraints or not. The optimizer in MotionSolve has a recovery mode to help recover from such infeasible design and let optimization proceed. To demonstrate how the optimizer recovers in this tutorial, you will change the initial design and see how it works.

1. Return to the Design Variables page and change the nominal value, upper and lower bounds as shown in Figure 15:
The four bar mechanism should look like the example in Figure 16:
2. Retain the Responses, Objectives and Constraints you defined earlier in the tutorial.
3. Run an optimization with a different Output file in the Solutions page as 4bar_opt_for_recover.py.
The number of iterations will change due to a different initial design.
Important: Pay attention to the last column ‘Reduced Step’ in the solver output:
In this example, the optimizer has found infeasible design in iteration 2-7; it is then told to reduce the step size until a feasible design is found.
4. Check the output files. In most cases, the output files could tell you why solver fails at a specific point and how you can avoid it if possible.
When the optimizer recovers from an infeasible design, an additional folder ‘Infeasible Points’ is created; each subfolder contains the following information:
• dvValues.txt: Value of design variables
• 4bar_opt_for_recover.abf: MotionSolve output file
• 4bar_opt_for_recover.h3d: Animation file (provided only when available)
• 4bar_opt_for_recover.log: MotionSolve log file
• 4bar_opt_for_recover.mrf: MotionSolve output file
• 4bar_opt_for_recover.xml: MotionSolve xml input file
You have found that the h3d file and log file are useful in most cases. You will load this h3d file in HyperView and see what is going wrong.
5. Load 4bar_opt_for_recover.h3d in folder Infeasible Point 1 -- iter-2.
You can see that the system enters a lock-up position before the solver crashes:
6. Check the result.

The result you see in the example is very close to what you get in STEP 5 and the cost function is reduced significantly. This shows you can get the optimal design even with infeasible design encountered in the intermediate step. However, the optimality is not guaranteed in some cases. When that happens, you can improve the result by adding proper constraints based on your investigation in previous steps. For four bar mechanism, you can add Grashof constraints. An example of how to do this in MotionSolve is included.