-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjres_solver.cpp
More file actions
106 lines (91 loc) · 3.25 KB
/
Copy pathjres_solver.cpp
File metadata and controls
106 lines (91 loc) · 3.25 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
105
106
/**
* @author popmonkey+jres@gmail.com
* @file jres_solver.cpp
* @brief JRES Solver Library C-API Wrapper
*/
#include <string>
#include <stdexcept>
#include <cstring>
#include "jres_solver/jres_solver.hpp"
#include "jres_standard_solver.hpp"
#include "jres_diagnostic_solver.hpp"
#include "jres_solver_types.hpp"
using json = nlohmann::json;
// --- C-API Implementation ---
char* create_output_string(const std::string& s) {
char* cstr = new char[s.length() + 1];
std::strcpy(cstr, s.c_str());
return cstr;
}
SpotterMode translate_spotter_mode(JresSpotterMode mode) {
switch (mode) {
case JRES_SPOTTER_MODE_NONE: return SpotterMode::None;
case JRES_SPOTTER_MODE_INTEGRATED: return SpotterMode::Integrated;
case JRES_SPOTTER_MODE_SEQUENTIAL: return SpotterMode::Sequential;
default: throw std::runtime_error("Unknown spotter mode enum provided.");
}
}
int solve_race_schedule(const char* raceDataJson,
const JresSolverOptions& options,
char** outputJson)
{
try {
json rawJsonData = json::parse(raceDataJson);
SolverContext ctx;
ctx.timeLimit = options.timeLimit;
ctx.spotterMode = translate_spotter_mode(options.spotterMode);
ctx.allowNoSpotter = options.allowNoSpotter;
ctx.optimalityGap = options.optimalityGap;
ctx.raceData = rawJsonData.get<RaceData>();
// Use Standard Solver
JresStandardSolver solver(ctx);
json resultJson = solver.solve();
*outputJson = create_output_string(resultJson.dump(4));
return 0;
} catch (const SolverException& e) {
// Custom exception with metadata - preserve timing/complexity
json errorJson = e.metadata;
errorJson["success"] = false;
errorJson["error"] = e.what();
*outputJson = create_output_string(errorJson.dump(4));
return -1;
} catch (const std::exception& e) {
// Generic exception - minimal error JSON
json errorJson;
errorJson["success"] = false;
errorJson["error"] = e.what();
*outputJson = create_output_string(errorJson.dump(4));
return -1;
}
}
int diagnose_race_schedule(const char* raceDataJson,
const JresSolverOptions& options,
char** outputJson)
{
try {
json rawJsonData = json::parse(raceDataJson);
SolverContext ctx;
ctx.timeLimit = options.timeLimit;
ctx.spotterMode = translate_spotter_mode(options.spotterMode);
ctx.allowNoSpotter = options.allowNoSpotter;
ctx.optimalityGap = options.optimalityGap;
ctx.raceData = rawJsonData.get<RaceData>();
// Use Diagnostic Solver
JresDiagnosticSolver solver(ctx);
json resultJson = solver.diagnose();
*outputJson = create_output_string(resultJson.dump(4));
return 0;
} catch (const std::exception& e) {
json errorJson;
errorJson["success"] = false;
errorJson["error"] = std::string("Internal Diagnosis Error: ") + e.what();
*outputJson = create_output_string(errorJson.dump(4));
return -1;
}
}
void free_solver_result(char* resultJson)
{
if (resultJson != nullptr) {
delete[] resultJson;
}
}