Files
RCS-3000/src/dispatch/enhanced_agv_manager.h
2026-01-09 15:01:58 +08:00

204 lines
6.1 KiB
C++

#ifndef ENHANCED_AGV_MANAGER_H
#define ENHANCED_AGV_MANAGER_H
#include "agv_manager_base.h"
#include "resource_manager.h"
#include <unordered_map>
#include <functional>
/**
* @brief AGV分配策略枚举
*/
enum class AssignmentStrategy {
FIRST_FIT, // 第一个适配(当前策略)
NEAREST_FIT, // 最近适配
BATTERY_AWARE, // 考虑电量
LOAD_BALANCED, // 负载均衡
PRIORITY_BASED, // 基于优先级
COST_OPTIMAL // 成本最优
};
/**
* @brief AGV评估结果
*/
struct AGVEvaluation {
AGV* agv;
double score; // 评估分数
double estimated_time; // 预计完成时间
double distance_to_task; // 到任务起点的距离
std::string reason; // 选择原因
AGVEvaluation(AGV* a, double s, double t, double d, const std::string& r)
: agv(a), score(s), estimated_time(t), distance_to_task(d), reason(r) {}
};
/**
* @brief 任务分配选项
*/
struct AssignmentOption {
Task* task;
AGV* agv;
std::vector<Path*> path;
double total_cost; // 总成本(时间、距离等)
double estimated_duration; // 预计执行时间
double battery_consumption; // 预计电量消耗
AssignmentOption(Task* t, AGV* a, const std::vector<Path*>& p, double c, double d, double b)
: task(t), agv(a), path(p), total_cost(c), estimated_duration(d), battery_consumption(b) {}
};
/**
* @brief 增强的AGV管理器
* 支持多种任务分配策略
*/
class EnhancedAGVManager : public AGVManagerBase {
private:
AssignmentStrategy strategy_; // 当前分配策略
std::unordered_map<AssignmentStrategy, std::function<double(AGV*, Task*, double)>> scoring_functions_;
ResourceManager* resource_manager_; // 资源管理器集成
// 分配权重
struct AssignmentWeights {
double distance_weight = 0.4; // 距离权重
double battery_weight = 0.3; // 电量权重
double load_weight = 0.2; // 负载权重
double speed_weight = 0.1; // 速度权重
} weights_;
public:
EnhancedAGVManager(GraphMap* map, ResourceManager* resource_mgr = nullptr, AssignmentStrategy strategy = AssignmentStrategy::NEAREST_FIT)
: AGVManagerBase(map), strategy_(strategy), resource_manager_(resource_mgr) {
initializeScoringFunctions();
}
/**
* @brief 设置资源管理器
*/
void setResourceManager(ResourceManager* resource_mgr) {
resource_manager_ = resource_mgr;
}
/**
* @brief 设置分配策略
*/
void setStrategy(AssignmentStrategy strategy) {
strategy_ = strategy;
}
/**
* @brief 设置分配权重
*/
void setWeights(const AssignmentWeights& weights) {
weights_ = weights;
}
/**
* @brief 智能任务分配
* 使用当前策略找到最优的AGV-任务匹配
*/
int assignTasksSmart();
/**
* @brief 为特定任务寻找最佳AGV
* @param task 要分配的任务
* @param exclude_excluded 排除已分配任务的AGV
* @return 评估结果列表,按分数排序
*/
std::vector<AGVEvaluation> findBestAGVForTask(Task* task, bool exclude_assigned = true);
/**
* @brief 计算任务-AGV匹配的成本
*/
double calculateAssignmentCost(AGV* agv, Task* task, std::vector<Path*>& path);
/**
* @brief 批量任务分配优化
* 同时考虑多个任务,找到全局最优分配
*/
std::vector<AssignmentOption> optimizeBatchAssignment(std::vector<Task*>& tasks,
std::vector<AGV*>& available_agvs);
/**
* @brief 预测AGV在未来时间段的可用性
*/
std::pair<double, bool> predictAvailability(AGV* agv, double future_time);
/**
* @brief 获取AGV负载情况
*/
double getAGVLoad(AGV* agv);
/**
* @brief 打印分配策略分析
*/
void printAssignmentAnalysis();
/**
* @brief 完成任务并释放资源
*/
void completeTaskWithResources(AGV* agv, bool success = true);
private:
void initializeScoringFunctions();
// 各种分配策略的评分函数
double scoreFirstFit(AGV* agv, Task* task, double distance);
double scoreNearestFit(AGV* agv, Task* task, double distance);
double scoreBatteryAware(AGV* agv, Task* task, double distance);
double scoreLoadBalanced(AGV* agv, Task* task, double distance);
double scorePriorityBased(AGV* agv, Task* task, double distance);
double scoreCostOptimal(AGV* agv, Task* task, double distance);
// 辅助函数
double calculateDistance(AGV* agv, Task* task);
double calculateBatteryConsumption(AGV* agv, Task* task, double distance);
double calculateLoadFactor(AGV* agv);
std::vector<Path*> findPathWithAvoidance(AGV* agv, Task* task);
std::string getReasonForSelection(AGV* agv, Task* task, double distance, double score);
std::string agvStateToString(AGVState state);
// 新增辅助方法
bool assignSingleTask(AGV* agv, Task* task);
std::string assignmentStrategyToString(AssignmentStrategy strategy);
};
/**
* @brief 任务分配分析器
* 用于分析和优化分配策略
*/
class AssignmentAnalyzer {
private:
struct Metrics {
double avg_completion_time;
double agv_utilization;
double battery_efficiency;
int task_count;
int reassignments;
};
std::unordered_map<AssignmentStrategy, Metrics> strategy_metrics_;
public:
/**
* @brief 记录任务分配结果
*/
void recordAssignment(AssignmentStrategy strategy, AGV* agv, Task* task,
double completion_time, double battery_used);
/**
* @brief 获取最优策略
*/
AssignmentStrategy getOptimalStrategy();
/**
* @brief 生成分配报告
*/
void generateReport();
/**
* @brief 重置统计数据
*/
void reset();
};
#endif // ENHANCED_AGV_MANAGER_H