32 Conventional Methods and Case Codes of Mathematical Modeling

32 conventional methods of mathematical modeling and case codes compiled during the competition for friendly sharing:

Link: https://pan.baidu.com/s/18uDr1113a0jhd2No8O1Nog 
Extraction code: xae5

In mathematical modeling, conventional algorithms refer to those classic algorithms that are widely used to solve various problems. These algorithms cover different areas of mathematics and computation, including optimization, linear algebra, graph theory, combinatorial optimization, numerical computation, and more. The following introduces 32 conventional algorithms:

  1. Exhaustive Search: Find the optimal solution by trying all possible solutions, which is suitable for small-scale problems.

  2. Greedy Algorithm: Each step selects the currently optimal solution, and the selection of the local optimal solution may not be the global optimal solution.

  3. Dynamic Programming: Decompose the problem into sub-problems and save the solutions of the sub-problems, avoiding repeated calculations, and are usually used for optimization problems.

  4. Backtracking algorithm (Backtracking): Find all possible solutions through trial and error, which is suitable for combinatorial optimization and permutation and combination problems.

  5. Branch and Bound method (Branch and Bound): Find the optimal solution by continuously dividing the problem space, avoiding invalid searches.

  6. Integer Programming: On the basis of linear programming, variables are required to be integers to deal with integer constraint problems.

  7. Linear Programming: Solve the maximum or minimum value of the objective function under linear constraints, which is widely used in optimization problems.

  8. Minimum Spanning Tree algorithm (Minimum Spanning Tree): Find a spanning tree in a weighted graph to minimize the sum of weights.

  9. Shortest Path Algorithm (Shortest Path): Find the shortest path between two points in the graph. Commonly used algorithms include Dijkstra and Floyd-Warshall.

  10. Max-Flow Min-Cut: Find the maximum flow path from source to sink in a directed graph.

  11. Topological Sorting: Arrange the nodes in the directed acyclic graph (DAG) in order to ensure that the directions of all edges are consistent.

  12. Graph Coloring algorithm (Graph Coloring): Color the nodes of the graph, and the colors of adjacent nodes are different.

  13. Knapsack Problem Algorithm (Knapsack Problem): Given the knapsack capacity and item value and weight, choose to load it into the knapsack to maximize the total value.

  14. Network Flow algorithm (Network Flow): Find the distribution of maximum flow and minimum cut in the network.

  15.    Interpolation and Curve Fitting: From known data points, a function is derived to approximate these points.

  16. Numerical Integration Algorithm (Numerical Integration): Calculate the integral value of a function on a certain interval by numerical methods.

  17. Equation Solving: Solve the solution of mathematical equations. Commonly used algorithms include dichotomy and Newton's method.

  18. Approximation Algorithm: Approximate solutions for NP-hard problems.

  19. Goal Programming: An algorithm that simultaneously considers multiple objective functions in an optimization problem.

  20. Monte Carlo Method: Solve the problem through random sampling and statistical simulation.

  21. Scheduling algorithm (Scheduling): Arrange and allocate tasks reasonably to achieve optimal results.

  22. Sparse Matrix: Efficient calculations for matrices with a large number of zero elements.

  23. Fourier Transform (Fourier Transform): Converts a signal from the time domain to the frequency domain for signal processing and image processing.

  24. Interpolation algorithm (Interpolation): A function is derived from known data points to approximate these points.

  25. Random Optimization (Random Optimization): optimize the solution of the problem through random search.

  26. Numerical Differentiation: Calculate the derivative value of a function by numerical methods.

  27. Integer Partition: Split a positive integer into the sum of several positive integers.

  28. Spectral Methods: Numerical solution via Fourier series or other spectral expansion methods.

  29. Maximum Clique algorithm (Maximum Clique): Find the largest complete subgraph (clique) in an undirected graph.

  30. Approximation Methods: Approximate solutions for complex problems.

  31. Relaxation Methods: Iterative algorithms for solving systems of linear and nonlinear equations.

  32. Convex Optimization: A method for solving convex optimization problems that guarantees a global optimal solution.

Mathematical modeling involves many classic algorithms and methods. Some common classic mathematical modeling algorithms are listed below, and simple case codes are provided as examples. These case codes are for reference only, and may need to be adjusted and optimized according to specific problems in actual applications.

  1. Linear regression algorithm:

Linear regression is an algorithm used to fit a linear model to establish the relationship between dependent and independent variables. Here is a simple linear regression case code:

import numpy as np
from sklearn.linear_model import LinearRegression

# 示例数据
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 5, 4, 5])

# 创建线性回归模型
model = LinearRegression()

# 拟合数据
model.fit(X, y)

# 预测
prediction = model.predict([[6]])

print("预测结果:", prediction)

2. Linear programming algorithm:

Linear programming is an optimization problem in which the objective function is linear. Here is a simple linear programming example code:

from scipy.optimize import linprog

# 线性规划示例
c = [-1, 2]  # 目标函数系数
A = [[-3, 1], [1, 2]]  # 不等式约束系数
b = [-3, 5]  # 不等式约束右侧常数
x_bounds = (0, None)  # x取值范围

# 求解线性规划
result = linprog(c, A_ub=A, b_ub=b, bounds=[x_bounds, x_bounds])

print("最优解:", result.x)
print("最优值:", -result.fun)

3. TSP (traveling salesman problem) algorithm:

TSP is a classic combinatorial optimization problem that requires finding the shortest circuit such that the traveling salesman visits all cities and returns to the starting city. Here is an example of a simple TSP algorithm:

import numpy as np
from itertools import permutations

# 示例数据
distances = np.array([
    [0, 10, 15, 20],
    [10, 0, 35, 25],
    [15, 35, 0, 30],
    [20, 25, 30, 0]
])

# 求解TSP
min_distance = float('inf')
optimal_path = None

for path in permutations(range(len(distances))):
    total_distance = 0
    for i in range(len(path) - 1):
        total_distance += distances[path[i]][path[i + 1]]
    total_distance += distances[path[-1]][path[0]]
    
    if total_distance < min_distance:
        min_distance = total_distance
        optimal_path = path

print("最短距离:", min_distance)
print("最优路径:", optimal_path)

4. Monte Carlo

The Monte Carlo method is a numerical calculation method based on random sampling, which is used to solve complex problems or perform probability statistics. A case and simple code implementation of the Monte Carlo method will be given below.

Example: Calculate the area of ​​a circle

We can estimate the area of ​​a circle by the Monte Carlo method. Consider a square of side length 2, completely enclosing a unit circle within the square. We can randomly generate a large number of points inside this square, and count how many of these points fall inside the circle. According to probability statistics, we can use the ratio of the number of points falling in the circle to the total number of points to estimate the area of ​​the circle.

import random

def monte_carlo_circle_area(num_points):
    inside_circle = 0
    for _ in range(num_points):
        x = random.uniform(-1, 1)
        y = random.uniform(-1, 1)
        distance = x**2 + y**2
        if distance <= 1:
            inside_circle += 1

    square_area = 4  # 正方形的面积
    circle_area = (inside_circle / num_points) * square_area
    return circle_area

num_points = 100000  # 采样点数
estimated_area = monte_carlo_circle_area(num_points)
print("估计的圆的面积:", estimated_area)

5. Interpolation algorithm

An interpolation algorithm is a method used to estimate unknown data points between data points. It infers the function value of unknown data points through the function value of known data points, so as to achieve a smooth approximation of the data. The following is a case and code implementation of a common interpolation algorithm - Lagrange interpolation method.

Case: Given a set of discrete data points, use Lagrangian interpolation to estimate the function value at the intermediate position.

def lagrange_interpolation(x_known, y_known, x):
    # 计算拉格朗日插值多项式的系数
    n = len(x_known)
    result = 0.0
    for i in range(n):
        term = y_known[i]
        for j in range(n):
            if i != j:
                term *= (x - x_known[j]) / (x_known[i] - x_known[j])
        result += term
    return result

# 示例数据
x_known = [1, 2, 4, 7]
y_known = [3, 5, 9, 8]

# 插值点
x_interp = 3

# 使用拉格朗日插值法计算插值点的函数值
y_interp = lagrange_interpolation(x_known, y_known, x_interp)

print("插值点 ({}, {}) 的函数值为: {}".format(x_interp, y_interp))

In this case, we are given a set of discrete data points (x_known, y_known), and we require x_interpinterpolation at intermediate positions to estimate the corresponding function value y_interp. The Lagrangian interpolation method uses polynomials to approximate the data points and obtain the function value of the interpolated points. Please note that Lagrangian interpolation is a simple and intuitive interpolation algorithm, but when dealing with a large number of data points, it may cause Runge phenomenon, resulting in unstable interpolation results. For large-scale datasets, other interpolation methods such as spline interpolation may be more appropriate.

The code provided above is just a simple implementation of the corresponding algorithm. In actual mathematical modeling, more complex processing may be required according to the characteristics of specific problems. The application of classical algorithms in mathematical modeling depends on the specific needs and complexity of the problem.

Learning the 32 general algorithms in mathematical modeling takes time and effort, but through methodical study and practice, these algorithms can be mastered. Here are some suggestions for learning general algorithms for mathematical modeling:

  1. Master basic mathematical knowledge: Mathematical modeling involves multiple mathematical fields, such as linear algebra, calculus, probability theory, graph theory, etc. First, make sure you have these basic maths down.

  2. Learn algorithm principles: For each algorithm, it is very important to learn its principles and basic ideas. Learn how and where algorithms work, as well as their strengths and weaknesses.

  3. Research resources: Materials such as books, tutorials, and academic papers on mathematical modeling and algorithms are good learning resources. Consult the related literature for in-depth understanding of the details and practical applications of each algorithm.

  4. Learning examples: Through case study of solving practical problems, it can help you better understand the application of algorithms. Try to imitate and understand the algorithm process in the example.

  5. Programming Practice: It is very beneficial to implement these algorithms using a programming language. Through programming practice, you can deepen your understanding of algorithms while mastering practical application skills.

  6. Problem Solving: Challenge yourself to solve real problems. Apply the learned algorithms to real situations and solve mathematical modeling problems, which will improve your ability to apply algorithms.

  7. Learning Tools: Understand and learn related mathematical modeling tools and software. These tools can help you implement algorithms faster and more efficiently.

  8. Participate in competitions: Participating in mathematical modeling competitions is a great opportunity to exercise and apply the algorithms you have learned. A variety of algorithms are often involved in the competition, and participating in the competition can improve the ability of the algorithm to solve problems.

  9. Discuss with others: Discuss the experience and problems of learning and applying algorithms with other people who are interested in mathematical modeling, and you can learn from each other and make progress.

  10. Keep practicing: Learning mathematical modeling and algorithms takes time and practice. Persevere in learning and practice, and constantly accumulate experience and skills.

Most importantly, learning mathematical modeling and general algorithms requires continuous study and practice. Don't expect to achieve it overnight, maintain a positive learning attitude, and continuously improve your abilities.

Guess you like

Origin blog.csdn.net/qq_51533426/article/details/131973876