DPSA/app/main.py

71 lines
2.9 KiB
Python
Raw Normal View History

2025-05-06 20:27:42 +08:00
from flask import Flask, request, jsonify
from flasgger import Swagger
import json
2025-05-07 11:26:19 +08:00
from algorithms import *
2025-05-06 20:27:42 +08:00
app = Flask(__name__)
# 配置 Swagger
app.config['SWAGGER'] = {
'title': '插值计算API',
'description': '提供基于双线性插值的计算服务',
'version': '1.0.0',
'uiversion': 3
}
swagger = Swagger(app)
@app.route('/PythonCalculate', methods=['POST'])
def calculate():
"""
插值计算接口
---
tags:
- 计算
parameters:
- in: body
name: body
required: true
schema:
type: object
properties:
text:
type: string
description: 包含计算参数的JSON字符串
example: '{"FuncName":"calculate_main","ClassName":"","Par":[{"name":"X_NUM","dataType":"0","arrayType":"0","isOut":"2","data":5},{"name":"Y_NUM","dataType":"0","arrayType":"0","isOut":"2","data":5},{"name":"X","dataType":"1","arrayType":"0","isOut":"2","data":2.5},{"name":"Y","dataType":"1","arrayType":"0","isOut":"2","data":3.5},{"name":"X0","dataType":"1","arrayType":"1","isOut":"2","data":[1,2,3,4,5]},{"name":"Y0","dataType":"1","arrayType":"1","isOut":"2","data":[1,2,3,4,5]},{"name":"Z0","dataType":"1","arrayType":"1","isOut":"2","data":[1,2,3,4,5,2,3,4,5,6,3,4,5,6,7,4,5,6,7,8,5,6,7,8,9]},{"name":"Z","dataType":"1","arrayType":"0","isOut":"1","data":0},{"name":"ZX","dataType":"1","arrayType":"0","isOut":"1","data":0},{"name":"ZY","dataType":"1","arrayType":"0","isOut":"1","data":0},{"name":"Z_OUT","dataType":"1","arrayType":"1","isOut":"1","data":0}]}'
responses:
200:
description: 计算结果
schema:
type: object
properties:
text:
type: string
description: 包含计算结果的JSON字符串
"""
# 解析输入JSON
request_data = request.json
input_data = json.loads(request_data["text"])
# 提取输入参数
2025-05-07 11:26:19 +08:00
num_projects = input_data["Par"][0]["Data"]
total_resource = input_data["Par"][1]["Data"]
num_iterations = input_data["Par"][2]["Data"]
resource_allocation, total_revenue = dpsa(num_projects, total_resource, num_iterations,0.1)
2025-05-06 20:27:42 +08:00
# 调用算法函数
# 构建输出JSON
output_data = {
"funcName": "calculate_main",
"className": "",
"par": [
2025-05-07 11:26:19 +08:00
{"name": "num_projects", "dataType": 0, "arrayType": 0, "isOut": 2, "data": num_projects},
{"name": "total_resource", "dataType": 1, "arrayType": 0, "isOut": 2, "data": total_resource},
{"name": "num_iterations", "dataType": 0, "arrayType": 0, "isOut": 2, "data": num_iterations},
{"name": "resource_allocation", "dataType": 1, "arrayType": 1, "isOut": 1, "data": resource_allocation},
{"name": "total_revenue", "dataType": 1, "arrayType": 0, "isOut": 1, "data": total_revenue}
2025-05-06 20:27:42 +08:00
]
}
return jsonify(output_data)
if __name__ == '__main__':
app.run(debug=True)