This commit is contained in:
ljy 2025-05-07 11:26:19 +08:00
parent 3bcfafd02a
commit d43e505b30
3 changed files with 60 additions and 53 deletions

View File

@ -1,29 +1,51 @@
import numpy as np import numpy as np
from typing import List, Tuple from typing import List, Dict, Tuple
import numpy as np
# 动态生成收益函数
def revenue_functions(num_projects):
functions = []
for i in range(num_projects):
# 这里可以根据实际需求调整系数
a = -0.1 - 0.05 * i # 二次项系数,每次递减 0.05
b = 2 + 0.5 * i # 一次项系数,每次递增 0.5
def r(x, a=a, b=b):
return a * x**2 + b * x
functions.append(r)
return functions
def dpsa(num_projects, total_resource, num_iterations, step_size):
# 初始化每个项目的资源分配为 0
resource_allocation = [0] * num_projects
revenue_funs = revenue_functions(num_projects)
for _ in range(num_iterations):
# 保存上一次的资源分配结果
prev_allocation = resource_allocation.copy()
for i in range(num_projects):
# 计算当前项目的梯度(导数)
gradient = (revenue_funs[i](resource_allocation[i] + step_size) - revenue_funs[i](resource_allocation[i])) / step_size
# 更新资源分配
resource_allocation[i] += gradient * step_size
# 确保资源分配不超过总资源
if sum(resource_allocation) > total_resource:
# 调整资源分配以满足约束条件
excess = sum(resource_allocation) - total_resource
# 按比例减少每个项目的资源分配
factor = (total_resource - sum(resource_allocation[:i])) / (sum(resource_allocation[i:]))
for j in range(i, num_projects):
resource_allocation[j] *= factor
# 判断是否收敛
if np.linalg.norm(np.array(resource_allocation) - np.array(prev_allocation)) < 1e-6:
break
# 计算总收益
total_revenue = sum(revenue_funs[i](resource_allocation[i]) for i in range(num_projects))
return resource_allocation, total_revenue
def bilinear_interpolation(
X_NUM: int,
Y_NUM: int,
X: float,
Y: float,
X0: List[float],
Y0: List[float],
Z0: np.ndarray
) -> Tuple[float, float, float, List[float]]:
"""
双线性插值计算
:param X_NUM: X方向点数 (整数)
:param Y_NUM: Y方向点数 (整数)
:param X: 目标X坐标 (浮点数)
:param Y: 目标Y坐标 (浮点数)
:param X0: X坐标数组 (浮点数列表)
:param Y0: Y坐标数组 (浮点数列表)
:param Z0: 二维数据数组 (numpy.ndarray, 形状为 Y_NUM x X_NUM)
:return: 元组 (Z, ZX, ZY, Z_OUT)
"""
X0_arr = np.array(X0) # 转换为NumPy数组
Z = np.interp(Y, Y0, Z0[:, np.argmin(np.abs(X0_arr - X))])
ZX = (np.interp(Y, Y0, Z0[:, np.argmin(np.abs(X0_arr - X + 0.1))]) - Z) / 0.1
ZY = (np.interp(Y + 0.1, Y0, Z0[:, np.argmin(np.abs(X0_arr - X))]) - Z) / 0.1
Z_OUT = Z0.flatten().tolist()
return Z, ZX, ZY, Z_OUT

View File

@ -1,8 +1,7 @@
import numpy as np
from flask import Flask, request, jsonify from flask import Flask, request, jsonify
from flasgger import Swagger from flasgger import Swagger
import json import json
from algorithms import bilinear_interpolation from algorithms import *
app = Flask(__name__) app = Flask(__name__)
@ -48,35 +47,21 @@ def calculate():
input_data = json.loads(request_data["text"]) input_data = json.loads(request_data["text"])
# 提取输入参数 # 提取输入参数
X_NUM = input_data["Par"][0]["Data"] num_projects = input_data["Par"][0]["Data"]
Y_NUM = input_data["Par"][1]["Data"] total_resource = input_data["Par"][1]["Data"]
X = input_data["Par"][2]["Data"] num_iterations = input_data["Par"][2]["Data"]
Y = input_data["Par"][3]["Data"] resource_allocation, total_revenue = dpsa(num_projects, total_resource, num_iterations,0.1)
X0 = input_data["Par"][4]["Data"]
Y0 = input_data["Par"][5]["Data"]
Z0 = input_data["Par"][6]["Data"]
# 调用算法函数 # 调用算法函数
Z, ZX, ZY, Z_OUT = bilinear_interpolation(
X_NUM, Y_NUM, X, Y, X0, Y0, np.array(Z0).reshape(Y_NUM, X_NUM)
)
# 构建输出JSON # 构建输出JSON
output_data = { output_data = {
"funcName": "calculate_main", "funcName": "calculate_main",
"className": "", "className": "",
"par": [ "par": [
{"name": "X_NUM", "dataType": 0, "arrayType": 0, "isOut": 2, "data": X_NUM}, {"name": "num_projects", "dataType": 0, "arrayType": 0, "isOut": 2, "data": num_projects},
{"name": "Y_NUM", "dataType": 0, "arrayType": 0, "isOut": 2, "data": Y_NUM}, {"name": "total_resource", "dataType": 1, "arrayType": 0, "isOut": 2, "data": total_resource},
{"name": "X", "dataType": 1, "arrayType": 0, "isOut": 2, "data": X}, {"name": "num_iterations", "dataType": 0, "arrayType": 0, "isOut": 2, "data": num_iterations},
{"name": "Y", "dataType": 2, "arrayType": 0, "isOut": 2, "data": Y}, {"name": "resource_allocation", "dataType": 1, "arrayType": 1, "isOut": 1, "data": resource_allocation},
{"name": "X0", "dataType": 1, "arrayType": 1, "isOut": 2, "data": X0}, {"name": "total_revenue", "dataType": 1, "arrayType": 0, "isOut": 1, "data": total_revenue}
{"name": "Y0", "dataType": 1, "arrayType": 1, "isOut": 2, "data": Y0},
{"name": "Z0", "dataType": 1, "arrayType": 1, "isOut": 2, "data": Z0},
{"name": "Z", "dataType": 1, "arrayType": 0, "isOut": 1, "data": float(Z)},
{"name": "ZX", "dataType": 1, "arrayType": 0, "isOut": 1, "data": float(ZX)},
{"name": "ZY", "dataType": 1, "arrayType": 0, "isOut": 1, "data": float(ZY)},
{"name": "Z_OUT", "dataType": 1, "arrayType": 1, "isOut": 1, "data": Z_OUT}
] ]
} }

View File

@ -1,5 +1,5 @@
flask==3.0.3 flask==3.0.3
numpy==2.2.4 numpy==1.26.4
flasgger==0.9.7.1 flasgger==0.9.7.1
werkzeug==3.0.3 werkzeug==3.0.3
jinja2==3.1.4 jinja2==3.1.4