# Optimization Basics¶

This tutorial shows the basics of creating optimization workflows in pSeven, using a very simple optimization problem with a single objective and a single constraint as an example. Its purpose is to introduce the *Optimizer* block and the optimization cycle concept, leaving advanced questions out of consideration.

Sections

## Before You Begin¶

This tutorial requires an existing **Tutorials** project. If you have not created this project yet, see *Tutorial Project* first.

- Open the
**Tutorials**project. - Create a new empty workflow. If you have never created a workflow before, see the
*Simple Workflow*tutorial. - Save the workflow as
**OptimizationBasics**. - Switch to
*Workspace*and verify that the workflow file (`OptimizationBasics.p7wf`) is added to the project. You can see it in the*Project*pane. - Switch to
*Edit*and select the workflow you have just created to continue with the tutorial.

## Task¶

The problem to be solved in this tutorial is the following:

A cylindrical tank is to hold at least \(20 \pi\) cubic meters. The material for the top and bottom costs $10 per square meter, and material for the side costs $8 per square meter. Find the radius \(r\) and height \(h\) of the most economical tank.

Note

For reference, the solution to this problem is \(r = 2\), \(h = 5\), and \(C = 240 \pi\) (roughly $754) is the least possible cost of construction.

## Solution¶

Solving an optimization problem in pSeven can be divided into the following general steps:

- Given an object or process, develop its (mathematical) model suitable for optimization.
- Implement this model in a workflow as a block or a group of blocks.
- Formulate an optimization problem. Define its general properties: the number of variables, objectives and constraints; variable and constraint bounds. This data is needed to configure the optimization block (
*Optimizer*). - Configure
*Optimizer*and connect it to model, creating an optimization cycle in the workflow. This cycle is handled by*Optimizer*and represents the optimization process. When you run the workflow, it executes the cycle until an optimal solution is found and output by the*Optimizer*block.

### Model¶

Our problem considers an object — a tank that is to hold a certain volume (first requirement). The second requirement is its shape (a cylinder). The key point is that cylinder dimensions are not fixed, so there is an infinite number of potential solutions: since volume \(V = ( \pi r^2 ) h\), any cylinder with \(h = \frac {V}{\pi r^2} >= \frac {20 \pi}{\pi r^2}\) satisfies both requirements. We have to find the best possible solution out of all alternatives, where “best” means “the one with the minimum cost”. Thus the construction cost is the solution quality measure.

A model suitable for optimization should allow to control certain variables in order to study changes in observed characteristics. To develop such a model, you have to answer the following questions:

- What are the observed characteristics?
- What variables can be controlled?
- What is the relationship between variables and characteristics?

For example, the characteristics we consider here are volume \(V\) and cost \(C\). Tank radius \(r\) and height \(h\) are controllable by condition, so they are variables. The dependency is:

- \(V = S_b h\), where \(S_b = \pi r^2\) is the area of base.
- \(C = 2 S_b \cdot 10 + S_s \cdot 8\), where \(S_b\) is the area of base (twice the cost since the same material is used for top) and \(S_s = 2 \pi r h\) is the area of side.

Basically, these two functions \(V(r, h)\) and \(C(r, h)\) are the model. We have identified the controllable variables, selected the characteristics to study, and defined their relationship. Note this model is complete for the given task: both characteristics are strictly determined by variables \(r\) and \(h\).

### Model Implementation¶

Now the model described above has to be implemented in pSeven. In this tutorial, the model is simple enough to be implemented as a single *Formula* block. The variables we selected become model inputs (input ports of the block). Characteristics become model outputs (output ports).

- Add a
*Formula*block. Name it`Model`.

Open the Model block configuration. This block allows to define simple functions and maps variable and function names to input and output ports.

- To add a function, input its name and definition in the
*Formula Designer*pane. After input, hit Enter or click anywhere outside the formula field so that the next name-formula pair appears. - You can use intermediate calculations here. For example, add base area \(S_b\) and side area \(S_s\) first:
- Name:
`S_b`, formula:`pi*r*r`. - Name:
`S_s`, formula:`2*pi*r*h`.

- Name:
- Now volume and cost can be defined as follows:
- Name:
`vol`, formula:`S_b*h`. - Name:
`cost`, formula:`2*S_b*10 + S_s*8`.

- Name:

The block automatically finds the names of variables and functions (responses) when you input formulas. Corresponding input and output ports are also created automatically (see the *Ports* tab).

- Added input ports: h, r.
- Added output ports: cost, vol, S_b, S_s.

Optionally, you can cancel adding outputs for intermediate calculations to avoid creating unnecessary output ports (clear the *Map* checkbox for “S_b” and “S_s”).

The Model block is configured. This block will:

- receive model input values from
*Optimizer*, - evaluate model outputs, and
- send evaluation results back to
*Optimizer*.

### Problem Formulation¶

Note that section *Model* just enumerates the variables to decide on the number of model inputs. Problem formulation often adds specific properties to variables. For example, in this tutorial we want both \(r\) and \(h\) as problem variables, and for obvious reasons both of them are limited to \((0, +\infty)\).

Observed model characteristics become problem objectives or constraints, depending on the original task. For example, the tutorial task sets requirement for the tank volume, so \(V\) is a constraint. The construction cost \(C\) has to be minimized, so it becomes an objective. Thus, the task turns to a single-objective constrained problem in 2D:

- Two variables, \(r\) and \(h\), bound to \((0, +\infty)\).
- One objective, \(C\) (minimize).
- One constraint, \(V\). Its lower bound is \(20 \pi \approx 62.831853\) — the minimum volume that satisfies the requirement.

### Problem Definition¶

The problem to be solved is defined in *Optimizer* block configuration.

- Add an
*Optimizer*block. Name it`Optimizer`.

Open the Optimizer block configuration. Variables, objectives and constraints are added on the *Problem Definition* tab (opens by default). This tab also contains various *Option presets*, one of which must be set for further work.

Select the *Analytical problem* preset.

Add the radius variable.

- Specify the name:
`r`. - Specify the lower bound:
`0`. - Leave other settings default and click .

The variable appears in the *Variables* pane. Note that once you add a variable, Optimizer automatically creates a port that will output the variable value when Optimizer wants to get evaluation results from the model.

Similarly, add the height variable.

- Name:
`h`. - Lower bound:
`0`. - Other settings are default.

Add the cost objective in the *Objectives* pane.

- Specify the name:
`cost`. - Leave other settings default and click .

The objective appears in the *Objectives* pane. For each objective, Optimizer automatically creates an input port.

Note

*Optimizer* always seeks to minimize the objective, so there is no option to select minimum or maximum mode. Maximum search requires to change the sign of the model function that is the problem objective.

Add the volume constraint in the *Constraints* pane.

- Specify the name:
`vol`. - Specify the lower bound (required volume):
`62.831853`. - Leave other settings default and click .

The constraint appears in the *Constraints* pane. Similarly to objectives, for each constraint an input port is automatically created. When this port receives an evaluation result from the model, Optimizer will check it against the bound you specified.

- Verify the problem definition. In particular, note that variable, objective and constraint names (hence their port names) are the same as those used when configuring Model. This is not required in general, but makes linking Optimizer and Model easier thanks to autolink.

Finally, link Optimizer and Model. If you have specified names as described above, the

*Autolink*dialog pops up when you create the link.

Accept all links (default) and click . Note that this dialog should show exactly 4 links. If there are extra or missing links, or you get a detailed *Links* dialog instead, it means that some of the names in Model and Optimizer configuration do not match. In this case, you can edit names as required, or set up the link manually.

## Workflow¶

Finished workflow is a typical optimization cycle.

The Optimizer and Model blocks are essential parts of this cycle. Optimizer only defines the problem — that is, it knows the valid range of model inputs and requirements for model outputs, but does not contain the model itself and cannot evaluate model functions. Model, in its turn, handles all evaluations, but has no task-specific settings.

Optimization is an iterative process. At each iteration, Optimizer generates a set of variable values (\(r,h\) pair) and sends it to Model inputs. Then Model evaluates and outputs a set of characteristics (\(C,V\) pair) that it sends back to Optimizer.

After receiving the set of characteristics, Optimizer analyzes it — for example, checks whether it satisfies the problem constraint and whether it improves the objective compared to its value at the previous step. Based on this analysis, Optimizer generates a next set of variables. This process continues until Optimizer finds an optimal solution that is finally output to its result ports.

Thus, each iteration produces a complete design (the \(r,h,C,V\) set). To save the design data, pSeven automatically monitors Optimizer inputs and outputs for variables, objectives and constraints. Produced designs form the evaluated set, another valuable result of the optimization process.

You can verify monitoring settings in *Run* on the *Configuration* tab.

Monitored ports are:

- Optimizer.cost, Optimizer.vol, Optimizer.h, Optimizer.r — the evaluated set data.
- Optimizer.optimal_c, Optimizer.optimal_f, Optimizer.optimal_x — optimal solution: constraint, objective and variable values, respectively.
- Optimizer.info and Optimizer.status — additional information.

These monitors provide all needed data, so the workflow is ready to run and does not require additional configuration.

- Save the workflow, run and switch to
*Analyse*to view the results.

## Results¶

All data gathered from the workflow is stored to the project database.

Here you can see the optimal solution: values of variables in *Optimizer.optimal_x* and the optimal volume and cost in *Optimizer.optimal_c*, *Optimizer.optimal_f*, respectively.

Note

In the *Optimizer.optimal_x* record, variables come in order of the *Variables* list in Optimizer configuration (\(r\), \(h\) in this tutorial). Same goes for objectives in multi-objective problems, and for constraints.

One way to visualize the results of this workflow in more detail is to plot optimization trajectories in variable and function space.

- In
*Analyze*, create a new report and open the*Data Series*pane to add report data.

- The warning will appear. Click
*Add anyway*button to add report data.

- Select the following records in the project database:
*Optimizer.cost*,*Optimizer.h*,*Optimizer.optimal_c*,*Optimizer.optimal_f*,*Optimizer.optimal_x*,*Optimizer.r*and*Optimizer.vol*. - Drag them to the
*Data Series*pane. Note that there are 7 records but they form 8 data series because*Optimizer.optimal_x*is 2-dimensional, and data series are always 1-dimensional.

It is recommended to rename data series to make plot configuration easier.

- Rename data series as follows:
- “r” to “radius”.
- “h” to “height”.
- “vol” to “volume”.
- “optimal_x[0]” to “optimal radius”.
- “optimal_x[1]” to “optimal height”.
- “optimal_f” to “optimal cost”.
- “optimal_c” to “optimal volume”.

Now you can add two 2D plots (variable and function space) with 2 datasets each: the first dataset is evaluation history (trajectory), the second is found optimum (a single point). For more details on how to add a 2D plot, see *Results and Reports* tutorial.

Evaluation history in variable space:

- First dataset (trajectory): radius as X, height as Y.
- Second dataset (optimum): optimal radius as X, optimal height as Y.

Evaluation history in function space:

- First dataset (trajectory): cost as X, volume as Y.
- Second dataset (optimum): optimal cost as X, optimal volume as Y.

## Conclusion¶

This tutorial includes only the most essential explanations about optimization in pSeven and does not discuss advanced problem solving. It is also strongly recommended to complete the next tutorial (*Multi-Objective Optimization Basics*) before starting other optimization tutorials.

- Save the workflow you have created in this tutorial: it will be required further. The report you have created when viewing the results is no longer needed, but you can also save it, if you wish.

Note that the *Multi-Objective Optimization Basics* tutorial continues with the same workflow, changing the problem definition (Optimizer configuration) to transform the task into a toy multi-objective problem.