移植了所有的策略控制函数
This commit is contained in:
parent
a464744bda
commit
d67c91f4c8
|
@ -31,6 +31,11 @@ public:
|
|||
*/
|
||||
int direction;
|
||||
|
||||
/**
|
||||
* 开始运行的时间
|
||||
*/
|
||||
int bus_time;
|
||||
|
||||
/**
|
||||
* 轨道对象
|
||||
*/
|
||||
|
@ -61,6 +66,15 @@ public:
|
|||
bool JudgeOnStation();
|
||||
|
||||
private:
|
||||
/**
|
||||
* 轨道的总长度
|
||||
*/
|
||||
int total_distance = 10;
|
||||
|
||||
/**
|
||||
* 获得公交车当前所在的位置
|
||||
* @return 公交车当前所在的位置
|
||||
*/
|
||||
int GetBusPosition() const;
|
||||
|
||||
/**
|
||||
|
@ -107,7 +121,27 @@ private:
|
|||
* 在当前站上可以顺便服务的请求
|
||||
* @return 服务的请求指针
|
||||
*/
|
||||
bus_query_t *SSTFBTWQuery();
|
||||
bus_query_t *SSTFBTWQuery() const;
|
||||
|
||||
/**
|
||||
* 获得在SCAN策略下应该处理的请求
|
||||
* @return 指向需要处理的请求的指针
|
||||
*/
|
||||
bus_query_t *SCANGetQuery();
|
||||
|
||||
/**
|
||||
* 根据指定的请求获得前进的方向
|
||||
* 在SCAN策略中使用
|
||||
* @param query 指定完成的请求
|
||||
* @return 前进的方向
|
||||
*/
|
||||
int SCANDirection(bus_query_t *query);
|
||||
|
||||
/**
|
||||
* 在当前站上可以顺便服务的请求
|
||||
* @return 服务的请求指针
|
||||
*/
|
||||
bus_query_t *SCANBTWQuery() const;
|
||||
};
|
||||
|
||||
#endif //AUTO_BUS_GUI_BUS_MODEL_H
|
||||
|
|
284
src/busControl.cpp
Normal file
284
src/busControl.cpp
Normal file
|
@ -0,0 +1,284 @@
|
|||
//
|
||||
// Created by ricardo on 2022/6/17.
|
||||
//
|
||||
#include "busModel.h"
|
||||
|
||||
int BusControllerModel::FCFSDirection() const
|
||||
{
|
||||
bus_query_t *p = query_manager->queries;
|
||||
|
||||
if(p == nullptr)
|
||||
{
|
||||
return BUS_STOP;
|
||||
} //如果没有请求,公交车停止
|
||||
|
||||
else
|
||||
{
|
||||
int clockwise = 0;
|
||||
int counterclockwise = 0; //用于顺,逆时针方向所经站台计数
|
||||
|
||||
/**
|
||||
* 公交车当前所在位置
|
||||
*/
|
||||
rail_node_t *now_position = rail_pos;
|
||||
/**
|
||||
* 公交车应该前进的方向
|
||||
*/
|
||||
rail_node_t *target_position = p->node;
|
||||
|
||||
rail_node_t *pos = now_position;
|
||||
while (pos != target_position) //顺时针计数
|
||||
{
|
||||
clockwise++;
|
||||
pos = pos->next_node;
|
||||
}
|
||||
|
||||
pos = now_position;
|
||||
while (pos != target_position) //逆时针计数
|
||||
{
|
||||
counterclockwise++;
|
||||
pos = pos->last_node;
|
||||
}
|
||||
|
||||
if(clockwise <= counterclockwise)
|
||||
{
|
||||
return BUS_CLOCK_WISE;
|
||||
}//若顺时针距离短(或顺逆相等),公交车顺时针运行
|
||||
else
|
||||
{
|
||||
return BUS_COUNTER_CLOCK_WISE;
|
||||
}//若逆时针距离短,公交车逆时针运行
|
||||
}
|
||||
}
|
||||
|
||||
bus_query_t *BusControllerModel::FCFSQuery() const
|
||||
{
|
||||
bus_query_t *result = nullptr;
|
||||
|
||||
if(query_manager->queries != nullptr)
|
||||
{
|
||||
if(rail_pos == query_manager->queries->node)
|
||||
{
|
||||
result = query_manager->queries;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bus_query_t *BusControllerModel::SSTFGetQuery()
|
||||
{
|
||||
// 当前没有请求
|
||||
if(query_manager->queries == nullptr)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
int length = 9999;
|
||||
bus_query_t *query = nullptr;
|
||||
bus_query_t *p = query_manager->queries;
|
||||
|
||||
// 遍历顺时针方向
|
||||
// 在两个方向路程相同时选择顺时针方向
|
||||
// 所以先遍历顺时针方向
|
||||
while (p != nullptr)
|
||||
{
|
||||
int temp = GetQueryDistance(p, BUS_CLOCK_WISE);
|
||||
if(temp < length)
|
||||
{
|
||||
length = temp;
|
||||
query = p;
|
||||
}
|
||||
p = p->next_node;
|
||||
}
|
||||
|
||||
// 遍历逆时针方向
|
||||
p = query_manager->queries;
|
||||
while (p != nullptr)
|
||||
{
|
||||
int temp = GetQueryDistance(p, BUS_COUNTER_CLOCK_WISE);
|
||||
if(temp < length)
|
||||
{
|
||||
length = temp;
|
||||
query = p;
|
||||
}
|
||||
p = p->next_node;
|
||||
}
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
int BusControllerModel::SSTFDirection(bus_query_t *query)
|
||||
{
|
||||
if (query == nullptr)
|
||||
{
|
||||
return BUS_STOP;
|
||||
}
|
||||
|
||||
int length = GetQueryDistance(query, BUS_CLOCK_WISE);
|
||||
if(length > total_distance / 2)
|
||||
{
|
||||
return BUS_COUNTER_CLOCK_WISE;
|
||||
}
|
||||
else if(length == 0)
|
||||
{
|
||||
return BUS_STOP;
|
||||
}
|
||||
else
|
||||
{
|
||||
return BUS_CLOCK_WISE;
|
||||
}
|
||||
}
|
||||
|
||||
bus_query_t *BusControllerModel::SSTFBTWQuery() const
|
||||
{
|
||||
bus_query_t *query = query_manager->queries;
|
||||
bus_query_t *allow_query = nullptr;
|
||||
rail_node_t *now_node = rail_pos;
|
||||
|
||||
while (query != nullptr)
|
||||
{
|
||||
if(query->node == now_node)
|
||||
{
|
||||
// 这里是设计上的缺陷,在bus_time显示时间的前一秒,公交车就实际上到达站台了
|
||||
if(query->time < bus_time - 1)
|
||||
{
|
||||
if(query->type == direction || query->type == BUS_TARGET)
|
||||
{
|
||||
allow_query = query;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
query = query->next_node;
|
||||
}
|
||||
|
||||
return allow_query;
|
||||
}
|
||||
|
||||
bus_query_t *BusControllerModel::SCANGetQuery()
|
||||
{
|
||||
// 当前没有请求
|
||||
if(query_manager->queries == nullptr)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if(direction == BUS_STOP)
|
||||
{
|
||||
// 在停止的状态下第一次开始选择方向
|
||||
int length = 9999;
|
||||
bus_query_t *query = nullptr;
|
||||
bus_query_t *p = query_manager->queries;
|
||||
|
||||
// 遍历顺时针方向
|
||||
// 在两个方向路程相同时选择顺时针方向
|
||||
// 所以先遍历顺时针方向
|
||||
while (p != nullptr)
|
||||
{
|
||||
int temp = GetQueryDistance(p, BUS_CLOCK_WISE);
|
||||
if(temp < length)
|
||||
{
|
||||
length = temp;
|
||||
query = p;
|
||||
}
|
||||
p = p->next_node;
|
||||
}
|
||||
|
||||
// 遍历逆时针方向
|
||||
p = query_manager->queries;
|
||||
while (p != nullptr)
|
||||
{
|
||||
int temp = GetQueryDistance(p, BUS_COUNTER_CLOCK_WISE);
|
||||
if(temp < length)
|
||||
{
|
||||
length = temp;
|
||||
query = p;
|
||||
}
|
||||
p = p->next_node;
|
||||
}
|
||||
|
||||
return query;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 在已经有方向的情况下处理方向
|
||||
int length = 9999;
|
||||
bus_query_t *query = nullptr;
|
||||
bus_query_t *p = query_manager->queries;
|
||||
|
||||
while (p != nullptr)
|
||||
{
|
||||
int temp = GetQueryDistance(p, direction);
|
||||
if(temp < length)
|
||||
{
|
||||
query = p;
|
||||
length = temp;
|
||||
}
|
||||
p = p->next_node;
|
||||
}
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
||||
|
||||
int BusControllerModel::SCANDirection(bus_query_t *query)
|
||||
{
|
||||
if(query == nullptr)
|
||||
{
|
||||
return BUS_STOP;
|
||||
}
|
||||
|
||||
if(direction == BUS_STOP)
|
||||
{
|
||||
int length = GetQueryDistance(query, BUS_CLOCK_WISE);
|
||||
if(length > total_distance / 2)
|
||||
{
|
||||
return BUS_COUNTER_CLOCK_WISE;
|
||||
}
|
||||
else
|
||||
{
|
||||
return BUS_CLOCK_WISE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int length = GetQueryDistance(query, direction);
|
||||
if(length > total_distance / 2)
|
||||
{
|
||||
if(direction == BUS_CLOCK_WISE)
|
||||
{
|
||||
return BUS_COUNTER_CLOCK_WISE;
|
||||
}
|
||||
else
|
||||
{
|
||||
return BUS_CLOCK_WISE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return direction;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bus_query_t *BusControllerModel::SCANBTWQuery() const
|
||||
{
|
||||
rail_node_t *now_position = rail_pos;
|
||||
//获取公交车当前所在站点
|
||||
bus_query_t *p = query_manager->queries;
|
||||
|
||||
while(p != nullptr)
|
||||
{
|
||||
if(p->node == now_position)
|
||||
{
|
||||
if(p->time < bus_time - 1)
|
||||
{
|
||||
return p;
|
||||
}
|
||||
}
|
||||
p = p->next_node;
|
||||
}//遍历请求链表,判断是否有可以顺便处理的请求
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
|
@ -285,6 +285,8 @@ void BusControllerModel::ReadConfigFile(const std::string& file_name)
|
|||
chosen_strategy = BUS_FCFS;
|
||||
}
|
||||
|
||||
// 得到轨道的总长度
|
||||
total_distance = node_space_length * total_station;
|
||||
// 重新生成轨道模型
|
||||
delete rail_manager;
|
||||
rail_manager = new RailsModel(node_space_length, total_station);
|
||||
|
|
Loading…
Reference in New Issue
Block a user