Search Theoretical Basis

1. The search algorithm is a neighborhood algorithm, usually a local optimum

2. The optimal step size improves the target value to the greatest extent along the feasible improvement direction, and maintains a feasible distance

3. Gradient information provides an algebraic test method for improving the direction

4. Convex feasible set and linear objective guarantee the global optimal point

5.3A algorithm is the most basic search algorithm

In the previous article, we introduced the core idea of ​​" how to build an operations research model " and introduced the classification of operations research models. But there is no mention of the method of solving the model. Today we will give an overview of " how to solve the operations research model ", and then we will introduce the specific solution method for specific models in depth.

Portal: The Beginning of Operations Research

1

DClub location problem

We who have a foundation in mathematical analysis know that some explicit equations can be solved accurately, which we call analytical methods; but in reality, most of the equations that cannot be solved accurately, we need to use numerical methods at this time . The operations research model is essentially an optimization problem - to find the most valued model, so the method of numerical search is often used to solve it.

Numerical search , English is numerical search, the idea is to try different decision variable values ​​(that is, feasible solutions) in turn until a satisfactory result is obtained. We call the iterative process of the next group of feasible solutions that improve the target value in the neighborhood of the current feasible solution as the search algorithm (improving search). In other words, the search algorithm is based on the current feasible solution, by checking its neighborhood to find a better solution than the current one, the so-called better solution is a solution that improves the target, and repeats this process until there is no until there is a better solution.

Pay attention to a key word here - within the neighborhood. This shows that the search algorithm obtains a local optimal solution, which we will discuss later. Therefore, the search algorithm is also called local improvement , hill climbing algorithm , local search or neighborhood search.

Let's use an example to illustrate the principle of the search algorithm in detail - DClub location problem.

The DClub discount department store wants to build a new store in an area to get the greatest possible foot traffic. The map below marks three population centers in the area: population center 1 coordinates (-1,3), about 60,000 people, population center 2 coordinates (1,3), about 20,000 people, population center 3 coordinates (0,-4), about 30,000 people.

af162032473cf03859a39acfa44f370e.png

New stores can be built anywhere more than 0.5 miles from the population center, which is the shaded area on the graph. Obviously, there are only two decision variables in this model:

9342c631a88ae5985a8bb30c93281c51.png

According to past experience, the passenger flow obtained by the store from each population center obeys the "gravity" model - the passenger flow is directly proportional to the population of the population center (thousands of people), and inversely proportional to the square of 1+distance. From this we get the objective function :

18849866fbe26fa9fe2a0b42f9322cf4.png

Then according to the constraints, we get the optimized model :

e960ff7531b39acbffe0204f2738e0c8.png

According to the model classification, it can be seen that the DClub location problem is a nonlinear programming model. Using the three-dimensional graph, we can easily draw the image of the objective function:

e912d151878a1579938dd3f27bf05849.png

Let's use the contour line to look at the principle of the search algorithm in detail: Assume starting from point X0, after four times, we get the approximate optimal point X4 of the model, the process is:

330dc86fcbc865dfafa6e7f8be5196a7.png

d7eb3eccd21912ce216b84d371147838.png

With an optimization model like this one that is easy to plot, it is easy to find the optimal solution. If there is no way to observe through graphs for more complex optimization models, it is necessary to find a specific search algorithm to find the optimal solution. We know that each step of the iterative process above is to find the next set of solutions that improve the target in the current feasible solution field , until no better solution is found in the feasible solution field. Therefore, we define:

Local optimal solution (local optimum)

If there is no feasible point better than the solution in a sufficiently small neighborhood around a set of feasible solutions, the solution is called a local optimal solution

58a4370b3b47555fc41eb61579296de4.png

Starting from an initial value, the search algorithm iterates, and when the local optimum is reached, the algorithm stops iterating.

Further, we can define the global optimal solution (global optimum) :

If there is no other feasible point in the global range where the target value is better than a feasible solution, then the feasible solution is called the global optimal solution

4856b16d507b75a2eaa7fe074317bbdb.png

Simply put, the global optimal solution is the optimal group of all local optimal solutions, so the following conclusions are drawn: the global optimal solution is also a local optimal solution, and the local optimal solution is not necessarily the global optimal solution.

For example, in the example of DClub, the following is also a local optimal search path:

6a323f113cfd5ce0013220ade078de3b.png

9f81d21c9123387fe9756ce8f4552758.png

The two search paths: X0→X1→X2→X3→X4; X0→X5→X6→X7 both reach the local optimal solution, but obviously the target value of X4 is better than that of x7, so X7 is not the global optimal solution.

The next question is naturally - how to find the global optimum . The most common method is to try different search algorithms (generally using different initial points), and select the best result as an approximate or heuristic optimal. We will talk about this later. Now let's focus on another issue.

2

Feasible improvement direction

The previous section showed the basic logic of the search algorithm, but did not give a search process method to improve the target value. First define:

3cd2df3b7779179c6fcb9ec87bbc8023.png

According to the definition, the model can iterate in any direction to obtain the next solution in the current solution, but in order to ensure the improvement of the target value, we need to limit its iteration in the direction of improvement, so define again:

a5d0c4fe09145a4edec22a68469fc897.png

As the name implies, the improvement direction does not consider constraints, as long as the target value of the next iteration is guaranteed to be better than the current target value; while the feasible direction should be considered within the feasible range, but it does not guarantee that the next target value is better than the current target value , but also feasibility. Note that the value of λ here is small enough , which means that this is the next feasible solution to be searched in the field of the current solution.

For example, the expression for the dual crude oil model is:

4ba76bca89d111d21e79176b28910d69.png

Visualize its constraints, where the gray part is the feasible region:

79d23c4594162031f2859717ce4c7f74.png

Obviously, moving x2 in any direction for a short period in the figure does not violate the constraints, so for x2, every Δx is a feasible direction, but not every feasible direction is an improvement direction, such as Δx=(0,-1); for x1, along Δx=(0,1) is a feasible direction, Δx=(0,-1) violates the constraint condition; for x3, along the direction Δx=(0,1) violates the constraint condition, so it is not a feasible direction, but an improvement direction .

The direction of model optimization is naturally - the direction of feasible improvement. That is, if the current solution goes a small distance toward the feasible improvement direction, the next point must be in the feasible region, and the target value is improved. This naturally leads to a question, the step size along the direction of feasible improvement is small, so how long can this step size be at most to ensure that it is feasible and the target value is improved? We define the optimal step size λ:

The search algorithm usually maximizes the target value along the feasible improvement direction, and keeps the feasible distance as the optimal step size

2cc6b1d8cf6796f3c85c5aeaa206a375.png

Here we understand it from two aspects: the distance to improve the target value to the greatest extent and maintain the maximum feasible distance; when the target value stops or the constraints are not satisfied, the λ value is fixed and a new improvement direction is selected.

As an example, consider the optimal step size for a mathematical model:

ffec9d841f438c2c786eae265daaaeee.png

With the optimal step size method, there is only one last problem - how to determine the direction of improvement . The direction of improvement is relative to the target value. If the objective function f is differentiable, according to the differential definition, the change value of the objective function of the search algorithm after the current solution x moves forward in the direction Δx by a step length λ can be approximated as:

47e1ded9bc6f2c72b15a0b2c55bf53ac.png

According to this, the gradient information provides an algebraic test method for whether the search direction is improved :

27fd669ef1512148e7c4caa0011838f2.png

Further, because the squares of real numbers are all greater than or equal to 0, we know mathematically that the gradient is the direction in which the value of the function changes the fastest. As long as the direction Δx is aligned with the gradient direction (or the opposite direction), the target value can be iterated along the improvement direction:

f6500bcd5a43da7a2671ad9b88f95e98.png

Give an example to illustrate.

6fa12e4aa02d2cce6a3bc6a385e81f23.png

One thing to note here is that the gradient direction is an improved direction, but it is not always the best search direction . This point will be explained later.

3

3A algorithm

According to the previous foreshadowing, we can give the first set of search algorithm - 3A algorithm .

843575125c6d655cdc2b764fcd7eefa8.png

Use the above DClub search example to illustrate the 3A algorithm.

31e9adfe003e15d197f949e8638fc3f9.png

The initial feasible solution of the model is X0=(-5,0) , let t=0, it is obvious that X0 has a feasible improvement direction, skip the first step of the algorithm, and construct a feasible improvement direction, the selected direction is Δx=(2,-1) , here the optimal step size λ=2.75 is determined according to the graphing method , and the first iteration point is obtained:

1654528e9fe7bdc81d1d8e7875d961e3.png

Let t=t+1=1, return to step 1. Select the feasible improvement direction Δx=(-4,-1), determine the optimal step size λ=0.25, and get the second iteration point:

3ac4d6f5fdbe73063c4da890f0da1993.png

Let t=t+1=2, return to step 1. Select the feasible improvement direction Δx=(1-1), determine the optimal step size λ=0.5, and get the third iteration point:

7f991e86ecdd2cc570ffa5d085e3180c.png

Let t=t+1=3, return to step 1. If there is no feasible improvement direction, the search is stopped and the local optimum point X3 is obtained.

There are four points to note here. First, the improvement direction here is not the gradient direction, but a randomly selected improvement direction, which shows that the search direction of the 3A algorithm is not necessarily to iterate in the direction where the function value drops the fastest; second, determine the optimal step size according to the graphing method , that is to say, it is not possible to use constraints to obtain the optimal step size here, which is different from the previous example, because the objective function expressions of the two are not of the same type.

If the initial feasible solution X0 is improved along the gradient direction, and only the constraints are used to find the optimal step size, then the program will be like this, the gradient of the calculation function at point X0 is about (1, 0.45); calculated according to the constraints The value range of the step size is:

447b217ac4ed99f7823164aa1c565365.png

Obviously, the optimal step size λ cannot be determined according to the constraints, and we can see it at a glance by drawing a graph: when X0 is improved in the direction of the gradient, the step size should not be too small or too large, otherwise it is negative optimization, and the optimal step size can be determined according to the graph. The optimal step length is about 4.7

3877b60cb9eddd024679d9b8c0d02d18.png

Third, the 3A algorithm emphasizes that when there is no feasible improvement direction, the model is locally optimal under the premise of weak assumptions . The weak assumption here means that the current solution in which there is no feasible improvement direction is not a local optimum.

For example, the following two examples: Figure a can improve the target along the direction of the w curve, but the target value cannot be improved in any straight line direction, that is, there is no improvement direction ; Figure b has no feasible improvement direction at x, but obviously there is an adjacent to x Improvement direction, that is, there is no feasible improvement direction .

bf74ac666340fb62aeba37c59f70a6d2.jpeg

Fourth, the 3A algorithm mentions the case where the model is unbounded . The so-called unbounded means that if any step in a feasible improvement direction can improve the target value and maintain the feasibility at the same time, the model is unbounded (unbounded).

Let's take the simplest example to understand.

a74b23b62ecd1957a4d308161f464e30.png

4

Guarantee the condition of local optimum and global optimum

In the first section, we know that the local optimum is not necessarily the global optimum. So is there a condition where the local optimum is the global optimum? The answer is yes - global optimality of convex sets and linear objectives:

cd8a32ecb3f4029fa3cb349a465b4a5a.gif

For an optimization model with a linear objective and a convex feasible set, if the 3A algorithm stops at a feasible solution and there is no feasible improvement direction, then the feasible solution is the global optimal solution. In other words, for a model with a linear objective and a convex set, the locally optimal solution is the globally optimal solution.

There are two terms that need to be explained here, linear objective and convex feasible set.

8b8ca6df34be5427d38b1d268cbd09f5.png

Obviously, the gradient of the objective function f is constant and equal to the vector c . According to the improved algebraic test method of the search direction , for the maximization of the objective function, if and only if c Δ x>0, the direction Δx is the direction of improvement ; for the minimization of the objective function , if and only if cΔx<0, the direction Δx is the direction of improvement.

Next, you only need to ensure that the place for the next iteration is feasible— convex feasible set :

If the line connecting any two points in the feasible region is in the feasible region, then the feasible region is called a convex set

78826a4058c2d7530eb99476209825ea.png

Any point in the line connecting two points can be expressed as:

70244d63b13de297ce4ce319affb09ba.png

That is to say, if the feasible set of the optimization model is convex, then for any feasible solution there is always a feasible direction to another solution.

From this, we can prove the global optimality of convex sets with linear objectives :

9f2a16d05ba10dfe825455cc95878a94.png

Finally, introduce the concept of tight constraints to end today's content. A tight constraint , also known as an active constraint or a binding constraint, represents a constraint that just satisfies the equation at the current solution x .

Still taking the double crude oil as an example, the point X1=(7,0) satisfies the inequality constraint:

e297fea7b505a82b020a7b56db993c95.png

This constraint doesn't work because 0.4*7+0.2*0=2.8>1.5. But the constraint works at point X*=(2,3.5): 0.4*2+0.2*3.5=1.5

The code (part) of the attached drawing:

import numpy as np
from matplotlib import cm
import matplotlib.pyplot as plt


plt.rcParams['figure.dpi'] = 100 #设置图片分辨率


class Plot(object):


    def __init__(self,x,y,z):
        '''
        初始化对象
        :param x: x轴数据集
        :param y: y轴数据集t
        :param z: z轴数据集
        '''
        self.x = x
        self.y = y
        self.z = z


    def plot_3d(self):
        '''
        作3D图像
        :return:
        '''
        fig = plt.figure(figsize=(12,6))
        ax = plt.axes(projection='3d')
        # ax.plot3D(x1, x2, p, 'gray')
        surf = ax.plot_surface(self.x,self.y,self.z,cmap=cm.rainbow)
        ax.set_xlabel('x1')
        ax.set_ylabel('x2')
        ax.set_zlabel('p')
        ax.set_title('DClub')
        fig.colorbar(surf)
        plt.show()


    def plot_circle(self,center=(3, 3),r=2):
        '''
        作圆
        :param center: 圆点坐标
        :param r: 半径
        :return:
        '''
        x = np.linspace(center[0] - r, center[0] + r, 5000)
        y1 = np.sqrt(r**2 - (x-center[0])**2) + center[1]
        y2 = -np.sqrt(r**2 - (x-center[0])**2) + center[1]
        plt.plot(x, y1, c='r')
        plt.plot(x, y2, c='r')
        plt.fill_between(x, y1, y2, facecolor="white")


    def plot_contour(self):
        '''
        等高线绘制,并且制作算法迭代路径,可自行定义
        :return:
        '''
        plt.rcParams['axes.facecolor'] = '#DCDCDC'
        plt.figure(figsize=(12,6))
        CS = plt.contour(x1,x2,p,levels=[3.5,6,11.5,21.6,36.1,54],linestyles=6*['--'],colors=6*['gray'],linewidths=[1])
        self.plot_circle((-1,3),0.5)
        self.plot_circle((1,3),0.5)
        self.plot_circle((0,-4),0.5)
        plt.scatter([-1, 1, 0], [3, 3, -4], color='r')
        # plt.grid(True)
        plt.clabel(CS, inline=0, fontsize=0)


        plt.scatter([-5,-3,-1,0,-0.5],[0,4,4.5,3.5,3],color='black',linewidths=2)
        plt.text(-6,-1,s='X0(-5,0)',verticalalignment='bottom')
        plt.text(-3.6,4.5,s='X1(-3,4)',verticalalignment='bottom')
        plt.text(-1.5,4.6,s='X2(-1,4.5)',verticalalignment='bottom')
        plt.text(0.1,3.6,s='X3(0,3.5)',verticalalignment='bottom')
        plt.text(-0.8,2,s='X4(-0.5,3)',verticalalignment='bottom')
        plt.arrow(-5, 0, 2, 4, length_includes_head=True, head_width=0.35, width=0.05,alpha=0.3, fc='red', ec='blue')
        plt.arrow(-3, 4, 2, 0.5, length_includes_head=True, head_width=0.35, width=0.05,alpha=0.3, fc='red', ec='blue')
        plt.arrow(-1, 4.5, 1, -1, length_includes_head=True, head_width=0.35, width=0.05,alpha=0.3, fc='red', ec='blue')
        plt.arrow(0, 3.5, -0.5, -0.5, length_includes_head=True, head_width=0.35, width=0.05,alpha=0.3, fc='red', ec='blue')
        plt.xlabel("x1")
        plt.ylabel("x2")
        plt.title("DClub Contour")
        plt.tight_layout()
        plt.show()


if __name__ == '__main__':
    n = 100
    a = np.linspace(-8, 8, 100)
    x1 = np.repeat(a, n, axis=0)
    x2 = np.tile(a, n)
    x1, x2 = np.meshgrid(x1, x2)
    p = 60 / (1 + (x1 + 1) ** 2 + (x2 - 3) ** 2) + 20 / (1 + (x1 - 1) ** 2 + (x2 - 3) ** 2) + 30 / (
                1 + x1 ** 2 + (x2 + 4) ** 2)


    P = Plot(x1,x2,p)
    P.plot_3d()
    P.plot_contour()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False


x = np.linspace(0,9,500)
y1 = -3/4*x+5
y2 = -2*x+7.5
y3 = -2/3*x+5/3
y = np.piecewise(x,[x<2,x>=2],[lambda x:-2*x+7.5,lambda x:-0.75*x+5])  #分段函数


plt.plot(x, y1, c='r',label='0.3x1+0.4x2>2')
plt.plot(x, y2, c='g',label='0.4x1+0.2x2>1.5')
plt.plot(x, y3, c='#8B0000',label='0.2x1+0.3x2>0.5')
plt.plot(x, np.tile(np.array(6), 500), c='#FFFF00')
plt.plot(np.tile(np.array(9), 500),np.linspace(0,6,500),  c='b')
plt.fill_between(x, y, np.tile(np.array(6), 500), where=(x>=0.75)&(x<=9),facecolor="#CDC5BF")
plt.scatter([7,4,0.75,2],[0,4,6,3.5],color='black')
plt.xlim(0,10)
plt.ylim(0,8)
plt.text(7,0.2,s='X1(7,0)',verticalalignment='bottom')
plt.text(4.3,4,s='X2(4,4)',verticalalignment='bottom')
plt.text(1,5.5,s='X3(0.75,6)',verticalalignment='bottom')
plt.text(2.2,3.5,s='X*(2,3.5)',verticalalignment='bottom')
plt.legend()
plt.xlabel("x1")
plt.ylabel("x2")
plt.title("双原油模型约束条件")
plt.show()

Code for solving partial derivatives and inequalities:

import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import tensorflow as tf


x1 = tf.Variable(-5.0)
x2 = tf.Variable(0.0)


with tf.GradientTape(persistent=True) as t:
    p = 60 / (1 + (x1 + 1) ** 2 + (x2 - 3) ** 2) + 20 / (1 + (x1 - 1) ** 2 + (x2 - 3) ** 2) + 30 / (1 + x1 ** 2 + (x2 + 4) ** 2)
    
x1_grad, x2_grad= t.gradient(p, [x1,x2]) 
print(x1_grad.numpy(),x2_grad.numpy())


from sympy import symbols, solve
lam = symbols('lam')
x1 = -5+lam
x2 = 0.45*lam
f1 = (x1 + 1) ** 2 + (x2 - 3) ** 2 - 0.25
f2 = (x1 - 1) ** 2 + (x2 - 3) ** 2 - 0.25
f3 = x1**2 + (x2 + 4) ** 2 - 0.25
solve([f1 >=0, f2 >= 0, f3 >= 0])

Guess you like

Origin blog.csdn.net/qq_27388259/article/details/125827553