-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_server.cc
More file actions
104 lines (81 loc) · 2.72 KB
/
agent_server.cc
File metadata and controls
104 lines (81 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "agent_server.h"
#include <utility>
namespace agent_server {
AgentServer::ReturnCodes AgentServer::CreateEnvironment(
const std::string &name, const environment::Config &config,
const std::chrono::system_clock::time_point &time,
const std::chrono::duration<int> &time_step_length,
const environment::Terrain &terrain) {
auto result = name_to_env_.find(name);
if (result != name_to_env_.end()) {
return ALREADY_EXISTS;
}
name_to_env_.emplace(std::make_pair(
name, environment::Environment(config, time, time_step_length, terrain)));
return OK;
}
AgentServer::ReturnCodes AgentServer::DeleteEnvironment(
const std::string &name) {
auto result = name_to_env_.find(name);
if (result == name_to_env_.end()) {
return ENV_NOT_FOUND;
}
name_to_env_.erase(result);
return OK;
}
AgentServer::ReturnCodes AgentServer::CreateQLearningAgent(
const std::string &agent_name, const std::string &env_name, const int row,
const int col) {
auto agent_result = name_to_agent_.find(agent_name);
if (agent_result != name_to_agent_.end()) {
return ALREADY_EXISTS;
}
auto env_result = name_to_env_.find(env_name);
if (env_result == name_to_env_.end()) {
return ENV_NOT_FOUND;
}
name_to_agent_.emplace(std::make_pair(
agent_name,
new agent::Agent(agent_name, &(env_result->second))));
return OK;
}
AgentServer::ReturnCodes AgentServer::DeleteAgent(const std::string &name) {
auto result = name_to_agent_.find(name);
if (result == name_to_agent_.end()) {
return AGENT_NOT_FOUND;
}
name_to_agent_.erase(result);
return OK;
}
std::pair<AgentServer::ReturnCodes, std::optional<environment::Environment>>
AgentServer::GetEnvironment(const std::string &name) {
auto result = name_to_env_.find(name);
if (result == name_to_env_.end()) {
return std::make_pair(ENV_NOT_FOUND, std::nullopt);
}
return std::make_pair(OK, result->second);
}
AgentServer::ReturnCodes AgentServer::SimulateToTimeStep(
const std::string &env_name, const int64_t time_step) {
auto env_it = name_to_env_.find(env_name);
if (env_it == name_to_env_.end()) {
return ENV_NOT_FOUND;
}
env_it->second.JumpToTimeStep(time_step);
return OK;
}
AgentServer::ReturnCodes AgentServer::AgentTakeAction(
const std::string &agent_name, const agent::action::Action *action) {
auto agent_it = name_to_agent_.find(agent_name);
if (agent_it == name_to_agent_.end()) {
return AGENT_NOT_FOUND;
}
auto action_ret = agent_it->second->TakeAction(action);
if (action_ret == agent::Agent::NOT_ENOUGH_RESOURCES) {
return ACTION_NOT_ENOUGH_RESOURCES;
} else if (action_ret == agent::Agent::SUCCESS) {
return OK;
}
return UNKNOWN_ERROR;
}
} // namespace agent_server