235 lines
10 KiB
C++
235 lines
10 KiB
C++
#include "settlement/core/deterministic_rng.hpp"
|
|
#include "settlement/core/world.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <charconv>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <exception>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <system_error>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
struct Options {
|
|
std::uint64_t seed = 42;
|
|
std::uint64_t agent_count = 50;
|
|
std::uint64_t job_count = 3000;
|
|
std::uint64_t ticks = 2000;
|
|
std::uint64_t warmup_ticks = 100;
|
|
std::uint64_t map_size = 160;
|
|
std::uint64_t blocked_percent = 8;
|
|
std::uint64_t workers = [] {
|
|
return static_cast<std::uint64_t>(std::min(8U,
|
|
std::max(static_cast<unsigned>(settlement::min_worker_count), std::thread::hardware_concurrency())));
|
|
}();
|
|
std::string svg_path;
|
|
};
|
|
|
|
bool parse_u64(std::string_view text, std::uint64_t& value) {
|
|
const char* const begin = text.data();
|
|
const char* const end = begin + text.size();
|
|
const auto result = std::from_chars(begin, end, value);
|
|
return result.ec == std::errc{} && result.ptr == end;
|
|
}
|
|
|
|
bool parse_options(int argc, char** argv, Options& options) {
|
|
for (int index = 1; index < argc; ++index) {
|
|
const std::string_view flag(argv[index]);
|
|
if (index + 1 >= argc) {
|
|
return false;
|
|
}
|
|
|
|
const std::string_view value_text(argv[++index]);
|
|
if (flag == "--svg") {
|
|
options.svg_path = std::string(value_text);
|
|
continue;
|
|
}
|
|
|
|
std::uint64_t value = 0;
|
|
if (!parse_u64(value_text, value)) {
|
|
return false;
|
|
}
|
|
|
|
if (flag == "--seed") {
|
|
options.seed = value;
|
|
} else if (flag == "--agents") {
|
|
options.agent_count = value;
|
|
} else if (flag == "--jobs") {
|
|
options.job_count = value;
|
|
} else if (flag == "--ticks") {
|
|
options.ticks = value;
|
|
} else if (flag == "--warmup") {
|
|
options.warmup_ticks = value;
|
|
} else if (flag == "--map-size") {
|
|
options.map_size = value;
|
|
} else if (flag == "--blocked-percent") {
|
|
options.blocked_percent = value;
|
|
} else if (flag == "--workers") {
|
|
options.workers = value;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
constexpr std::uint64_t max_ticks = 1000000;
|
|
constexpr std::uint64_t max_agent_ticks = 100000000;
|
|
if (options.agent_count > 100000 || options.job_count > 100000 ||
|
|
options.map_size < 32 || options.map_size > 512 || options.blocked_percent > 30 ||
|
|
options.ticks > max_ticks || options.warmup_ticks > max_ticks ||
|
|
options.ticks > max_ticks - options.warmup_ticks ||
|
|
options.workers < settlement::min_worker_count || options.workers > 32) {
|
|
return false;
|
|
}
|
|
|
|
const std::uint64_t total_ticks = options.ticks + options.warmup_ticks;
|
|
const std::uint64_t map_cells = options.map_size * options.map_size;
|
|
const std::uint64_t open_cells = map_cells - map_cells * options.blocked_percent / 100;
|
|
return (options.agent_count == 0 || total_ticks <= max_agent_ticks / options.agent_count) &&
|
|
options.job_count <= open_cells;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
Options options;
|
|
if (!parse_options(argc, argv, options)) {
|
|
std::cerr << "Usage: settlement_sim [--seed N] [--agents N] [--jobs N] [--ticks N] "
|
|
"[--warmup N] [--map-size N] [--blocked-percent N] [--workers 2..32] [--svg PATH]\n";
|
|
return 2;
|
|
}
|
|
|
|
try {
|
|
const auto map_size = static_cast<std::int32_t>(options.map_size);
|
|
settlement::World world(options.seed, map_size, map_size,
|
|
static_cast<std::size_t>(options.workers));
|
|
settlement::DeterministicRng terrain_rng(options.seed ^ 0x5445525241494E31ULL);
|
|
settlement::DeterministicRng agent_rng(options.seed ^ 0x4147454E545F3031ULL);
|
|
settlement::DeterministicRng job_rng(options.seed ^ 0x4A4F42535F303031ULL);
|
|
|
|
const std::uint64_t map_cells = options.map_size * options.map_size;
|
|
const std::uint64_t blocked_cell_count = map_cells * options.blocked_percent / 100;
|
|
std::uint64_t blocked_cells = 0;
|
|
while (blocked_cells < blocked_cell_count) {
|
|
const std::uint64_t cell = terrain_rng.bounded(map_cells);
|
|
const auto x = static_cast<std::int32_t>(cell % options.map_size);
|
|
const auto y = static_cast<std::int32_t>(cell / options.map_size);
|
|
const settlement::GridPosition position{x, y};
|
|
if (world.is_walkable(position)) {
|
|
world.set_blocked(position);
|
|
++blocked_cells;
|
|
}
|
|
}
|
|
|
|
const settlement::GridPosition town_center{map_size / 2, map_size / 2};
|
|
const std::int32_t camp_radius = 5;
|
|
const std::int32_t job_min_distance = std::max(8, map_size / 10);
|
|
std::vector<settlement::GridPosition> camp_spawn_cells;
|
|
std::vector<settlement::GridPosition> job_spawn_cells;
|
|
for (std::int32_t y = 0; y < map_size; ++y) {
|
|
for (std::int32_t x = 0; x < map_size; ++x) {
|
|
const settlement::GridPosition position{x, y};
|
|
if (!world.is_walkable(position)) {
|
|
continue;
|
|
}
|
|
const std::int32_t dx = x > town_center.x ? x - town_center.x : town_center.x - x;
|
|
const std::int32_t dy = y > town_center.y ? y - town_center.y : town_center.y - y;
|
|
const std::int32_t distance_from_center = dx + dy;
|
|
if (distance_from_center <= camp_radius) {
|
|
camp_spawn_cells.push_back(position);
|
|
}
|
|
if (distance_from_center >= job_min_distance) {
|
|
job_spawn_cells.push_back(position);
|
|
}
|
|
}
|
|
}
|
|
if (camp_spawn_cells.empty() || options.job_count > job_spawn_cells.size()) {
|
|
throw std::runtime_error("map has too few open cells for the selected camp and jobs");
|
|
}
|
|
|
|
for (std::size_t index = job_spawn_cells.size(); index > 1; --index) {
|
|
const auto other = static_cast<std::size_t>(job_rng.bounded(static_cast<std::uint64_t>(index)));
|
|
std::swap(job_spawn_cells[index - 1], job_spawn_cells[other]);
|
|
}
|
|
|
|
for (std::uint64_t index = 0; index < options.agent_count; ++index) {
|
|
const auto camp_index = static_cast<std::size_t>(
|
|
agent_rng.bounded(static_cast<std::uint64_t>(camp_spawn_cells.size())));
|
|
static_cast<void>(world.spawn_agent(camp_spawn_cells[camp_index]));
|
|
}
|
|
for (std::uint64_t index = 0; index < options.job_count; ++index) {
|
|
const std::uint32_t work_required = static_cast<std::uint32_t>(20 + job_rng.bounded(61));
|
|
const auto job_index = static_cast<std::size_t>(index);
|
|
static_cast<void>(world.add_job(job_spawn_cells[job_index], work_required));
|
|
}
|
|
|
|
for (std::uint64_t tick = 0; tick < options.warmup_ticks; ++tick) {
|
|
world.step();
|
|
}
|
|
if (!options.svg_path.empty()) {
|
|
world.write_svg(options.svg_path);
|
|
}
|
|
|
|
std::vector<double> tick_times_microseconds;
|
|
tick_times_microseconds.reserve(static_cast<std::size_t>(options.ticks));
|
|
const auto benchmark_start = std::chrono::steady_clock::now();
|
|
for (std::uint64_t tick = 0; tick < options.ticks; ++tick) {
|
|
const auto tick_start = std::chrono::steady_clock::now();
|
|
world.step();
|
|
const auto tick_end = std::chrono::steady_clock::now();
|
|
const std::chrono::duration<double, std::micro> elapsed = tick_end - tick_start;
|
|
tick_times_microseconds.push_back(elapsed.count());
|
|
}
|
|
const auto benchmark_end = std::chrono::steady_clock::now();
|
|
|
|
std::sort(tick_times_microseconds.begin(), tick_times_microseconds.end());
|
|
const auto percentile = [&tick_times_microseconds](double fraction) {
|
|
if (tick_times_microseconds.empty()) {
|
|
return 0.0;
|
|
}
|
|
const auto index = static_cast<std::size_t>(
|
|
fraction * static_cast<double>(tick_times_microseconds.size() - 1));
|
|
return tick_times_microseconds[index];
|
|
};
|
|
const std::chrono::duration<double, std::milli> total_elapsed = benchmark_end - benchmark_start;
|
|
const double average_microseconds = options.ticks == 0
|
|
? 0.0
|
|
: total_elapsed.count() * 1000.0 / static_cast<double>(options.ticks);
|
|
|
|
const auto active_agents = std::count_if(
|
|
world.agents().begin(), world.agents().end(),
|
|
[](const settlement::Agent& agent) { return agent.state != settlement::AgentState::Idle; });
|
|
|
|
std::cout << "scenario=work_and_astar\n"
|
|
<< "seed=" << world.seed() << '\n'
|
|
<< "map=" << world.width() << 'x' << world.height() << '\n'
|
|
<< "agents=" << world.agents().size() << '\n'
|
|
<< "workers=" << world.worker_count() << '\n'
|
|
<< "jobs=" << world.jobs().size() << '\n'
|
|
<< "blocked_percent=" << options.blocked_percent << '\n'
|
|
<< "warmup_ticks=" << options.warmup_ticks << '\n'
|
|
<< "measured_ticks=" << options.ticks << '\n'
|
|
<< "world_ticks=" << world.tick() << '\n'
|
|
<< "active_agents=" << active_agents << '\n'
|
|
<< "completed_jobs=" << world.jobs_completed() << '\n'
|
|
<< "path_searches=" << world.path_searches() << '\n'
|
|
<< "path_nodes_visited=" << world.path_nodes_visited() << '\n'
|
|
<< "total_ms=" << total_elapsed.count() << '\n'
|
|
<< "avg_tick_us=" << average_microseconds << '\n'
|
|
<< "p50_tick_us=" << percentile(0.50) << '\n'
|
|
<< "p95_tick_us=" << percentile(0.95) << '\n';
|
|
if (!options.svg_path.empty()) {
|
|
std::cout << "svg=" << options.svg_path << '\n';
|
|
}
|
|
} catch (const std::exception& error) {
|
|
std::cerr << "Simulation failed: " << error.what() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|