-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmoral_analyzer_example.cpp
More file actions
392 lines (341 loc) · 14 KB
/
moral_analyzer_example.cpp
File metadata and controls
392 lines (341 loc) · 14 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
╔═════════════════════════════════════════════════════════════════════╗
║ ThemisDB - Hybrid Database System ║
╠═════════════════════════════════════════════════════════════════════╣
File: moral_analyzer_example.cpp ║
Version: 0.0.47 ║
Last Modified: 2026-04-15 18:43:55 ║
Author: unknown ║
╠═════════════════════════════════════════════════════════════════════╣
Quality Metrics: ║
• Maturity Level: 🟢 PRODUCTION-READY ║
• Quality Score: 100.0/100 ║
• Total Lines: 395 ║
• Open Issues: TODOs: 0, Stubs: 0 ║
╠═════════════════════════════════════════════════════════════════════╣
Status: ✅ Production Ready ║
╚═════════════════════════════════════════════════════════════════════╝
*/
/**
* @file moral_analyzer_example.cpp
* @brief Example demonstrating the MoralAnalyzer for graph-based ethical reasoning
*
* This example shows how to use the MoralAnalyzer class to:
* 1. Create ethical scenarios (Trolley Problem, Autonomous Vehicle)
* 2. Build decision graphs in ThemisDB
* 3. Analyze scenarios with different philosophical frameworks
* 4. Use multi-philosophy ensemble reasoning
* 5. Store and visualize ethical decisions
*
* Compile and run:
* mkdir -p build && cd build
* cmake ..
* make moral_analyzer_example
* ./moral_analyzer_example
*/
#include "llm/moral_analyzer.h"
#include "llm/ethical_guidelines_manager.h"
#include "storage/rocksdb_wrapper.h"
#include <iostream>
#include <memory>
using namespace themis;
using namespace themis::llm;
void printSeparator(const std::string& title = "") {
std::cout << "\n" << std::string(70, '=') << "\n";
if (!title.empty()) {
std::cout << title << "\n";
std::cout << std::string(70, '=') << "\n";
}
}
void printDecision(const MoralAnalyzer::EthicalDecision& decision) {
std::cout << "Decision ID: " << decision.decision_id << "\n";
std::cout << "Philosophy: " << decision.philosophy << "\n";
std::cout << "Recommended Action: " << decision.recommended_action << "\n";
std::cout << "Confidence: " << (decision.confidence * 100) << "%\n";
std::cout << "\nReasoning:\n" << decision.reasoning << "\n";
if (!decision.principle_citations.empty()) {
std::cout << "\nPrinciple Citations:\n";
for (const auto& principle : decision.principle_citations) {
std::cout << " - " << principle << "\n";
}
}
std::cout << "\nMetrics:\n";
std::cout << " Consistency: " << (decision.metrics.consistency * 100) << "%\n";
std::cout << " Fairness: " << (decision.metrics.fairness * 100) << "%\n";
std::cout << " Transparency: " << (decision.metrics.transparency * 100) << "%\n";
if (!decision.alternative_perspectives.empty()) {
std::cout << "\nAlternative Perspectives:\n";
for (const auto& [phil, action] : decision.alternative_perspectives) {
std::cout << " " << phil << ": " << action << "\n";
}
}
}
/**
* Example 1: Classic Trolley Problem
*/
void example1_trolleyProblem(MoralAnalyzer& analyzer) {
printSeparator("Example 1: Classic Trolley Problem");
// Create the trolley problem scenario
MoralAnalyzer::EthicalScenario scenario;
scenario.id = "trolley_001";
scenario.description =
"A runaway trolley is heading towards five people tied to the tracks. "
"You are standing next to a lever that can divert the trolley to a side track, "
"where only one person is tied. Do you pull the lever?";
scenario.domain = "classic_dilemma";
scenario.stakeholders = {
{"people_on_main_track", 5},
{"person_on_side_track", 1},
{"decision_maker", 1}
};
scenario.possible_actions = {
"pull_lever",
"do_nothing"
};
scenario.relevant_principles = {
"minimize_harm",
"do_not_kill",
"respect_autonomy"
};
std::cout << "Scenario: " << scenario.description << "\n\n";
// Analyze with Kantian ethics
std::cout << "--- Analyzing with Kantian Ethics ---\n";
auto [kant_status, kant_decision] = analyzer.analyzeWithPhilosophy(
scenario,
"kant"
);
if (kant_status.ok) {
printDecision(kant_decision);
} else {
std::cerr << "Error: " << kant_status.message << "\n";
}
std::cout << "\n--- Analyzing with Utilitarian Ethics ---\n";
auto [util_status, util_decision] = analyzer.analyzeWithPhilosophy(
scenario,
"utilitarian"
);
if (util_status.ok) {
printDecision(util_decision);
} else {
std::cerr << "Error: " << util_status.message << "\n";
}
}
/**
* Example 2: Autonomous Vehicle Dilemma
*/
void example2_autonomousVehicle(MoralAnalyzer& analyzer) {
printSeparator("Example 2: Autonomous Vehicle Dilemma");
MoralAnalyzer::EthicalScenario scenario;
scenario.id = "av_001";
scenario.description =
"An autonomous vehicle's brakes fail. It must choose between hitting "
"a barrier (risking passenger life) or swerving into pedestrians "
"(risking pedestrian lives). What should the vehicle do?";
scenario.domain = "autonomous_systems";
scenario.stakeholders = {
{"passenger", 1},
{"pedestrians", 3},
{"other_drivers", 2}
};
scenario.possible_actions = {
"hit_barrier",
"swerve_into_pedestrians",
"emergency_brake"
};
scenario.relevant_principles = {
"protect_passengers",
"minimize_harm",
"fairness",
"responsibility"
};
std::cout << "Scenario: " << scenario.description << "\n\n";
// Use multi-philosophy analysis
std::cout << "--- Multi-Philosophy Analysis ---\n";
auto [status, decision] = analyzer.analyzeMultiPhilosophy(
scenario,
{"kant", "utilitarian", "virtue"}
);
if (status.ok) {
printDecision(decision);
// Store the decision
auto store_status = analyzer.storeDecision(decision);
if (store_status.ok) {
std::cout << "\n✓ Decision stored in ThemisDB\n";
}
} else {
std::cerr << "Error: " << status.message << "\n";
}
}
/**
* Example 3: Medical Ethics - Organ Transplant
*/
void example3_organTransplant(MoralAnalyzer& analyzer) {
printSeparator("Example 3: Medical Ethics - Organ Transplant");
MoralAnalyzer::EthicalScenario scenario;
scenario.id = "medical_001";
scenario.description =
"A doctor has five patients who will die without organ transplants. "
"A healthy patient comes in for a routine checkup. The doctor could "
"harvest the healthy patient's organs to save the five. Should they?";
scenario.domain = "medical_ethics";
scenario.stakeholders = {
{"healthy_patient", 1},
{"patients_needing_organs", 5},
{"doctor", 1},
{"hospital", 1}
};
scenario.possible_actions = {
"harvest_organs",
"do_not_harvest",
"seek_alternative_donors"
};
scenario.relevant_principles = {
"do_no_harm",
"respect_autonomy",
"beneficence",
"justice"
};
scenario.context_weights = {
{"urgency", 0.9},
{"certainty", 0.8},
{"reversibility", 0.0}
};
std::cout << "Scenario: " << scenario.description << "\n\n";
// Analyze with all three major frameworks
std::vector<std::string> philosophies = {"kant", "utilitarian", "virtue"};
for (const auto& philosophy : philosophies) {
std::cout << "\n--- " << philosophy << " perspective ---\n";
auto [status, decision] = analyzer.analyzeWithPhilosophy(scenario, philosophy);
if (status.ok) {
std::cout << "Recommended: " << decision.recommended_action << "\n";
std::cout << "Confidence: " << (decision.confidence * 100) << "%\n";
std::cout << "Score: " << decision.reasoning_path.total_score << "\n";
}
}
// Get synthesis
std::cout << "\n--- Synthesized Multi-Philosophy Decision ---\n";
auto [synth_status, synth_decision] = analyzer.analyzeMultiPhilosophy(
scenario,
philosophies
);
if (synth_status.ok) {
printDecision(synth_decision);
}
}
/**
* Example 4: Export Decision Graph
*/
void example4_exportGraph(MoralAnalyzer& analyzer) {
printSeparator("Example 4: Visualize Decision Graph");
// Create a simple scenario
MoralAnalyzer::EthicalScenario scenario;
scenario.id = "export_001";
scenario.description = "Should AI be allowed to make life-or-death decisions?";
scenario.domain = "ai_ethics";
scenario.stakeholders = {
{"humans_affected", 10},
{"ai_developers", 5}
};
scenario.possible_actions = {
"allow_with_oversight",
"prohibit_completely",
"allow_in_limited_contexts"
};
std::cout << "Building decision graph...\n";
auto build_status = analyzer.buildDecisionGraph(scenario);
if (build_status.ok) {
std::cout << "✓ Decision graph built successfully\n\n";
// Export to DOT format
std::string dot = analyzer.exportDecisionGraphDOT(scenario.id);
std::cout << "Decision Graph (DOT format):\n";
std::cout << dot << "\n";
std::cout << "\nTo visualize, save to file and run:\n";
std::cout << " dot -Tpng decision_graph.dot -o decision_graph.png\n";
} else {
std::cerr << "Error: " << build_status.message << "\n";
}
}
/**
* Example 5: Reasoning Explanation
*/
void example5_reasoningExplanation(MoralAnalyzer& analyzer) {
printSeparator("Example 5: Detailed Reasoning Explanation");
MoralAnalyzer::EthicalScenario scenario;
scenario.id = "privacy_001";
scenario.description =
"A company has user data that could help law enforcement catch a criminal. "
"Should they provide the data without a warrant?";
scenario.domain = "privacy_ethics";
scenario.stakeholders = {
{"users", 1000000},
{"law_enforcement", 10},
{"potential_victims", 5},
{"criminal", 1}
};
scenario.possible_actions = {
"provide_data_without_warrant",
"require_warrant",
"refuse_all_requests"
};
scenario.relevant_principles = {
"privacy",
"public_safety",
"rule_of_law",
"transparency"
};
std::cout << "Scenario: " << scenario.description << "\n\n";
// Analyze with Kantian ethics
auto [status, decision] = analyzer.analyzeWithPhilosophy(scenario, "kant");
if (status.ok) {
// Get detailed explanation
std::string explanation = analyzer.getReasoningExplanation(decision);
std::cout << explanation << "\n";
}
}
/**
* Main function - Run all examples
*/
int main(int argc, char* argv[]) {
try {
printSeparator("ThemisDB Moral Analyzer - Examples");
std::cout << "\nInitializing ThemisDB...\n";
// Initialize RocksDB
RocksDBWrapper db;
if (!db.open("/tmp/themisdb_moral_analyzer_example")) {
std::cerr << "Failed to open database\n";
return 1;
}
std::cout << "✓ Database initialized\n";
// Create ethical guidelines manager
auto ethical_guidelines = std::make_shared<EthicalGuidelinesManager>();
std::cout << "✓ Ethical guidelines manager initialized\n";
// Create moral analyzer
MoralAnalyzer analyzer(db, ethical_guidelines);
std::cout << "✓ Moral analyzer initialized\n";
// Run examples
example1_trolleyProblem(analyzer);
example2_autonomousVehicle(analyzer);
example3_organTransplant(analyzer);
example4_exportGraph(analyzer);
example5_reasoningExplanation(analyzer);
printSeparator("All Examples Completed");
std::cout << "\nThe MoralAnalyzer has demonstrated:\n";
std::cout << " ✓ Graph-based ethical scenario representation\n";
std::cout << " ✓ Multi-philosophy reasoning (Kant, Utilitarian, Virtue)\n";
std::cout << " ✓ Decision synthesis from multiple perspectives\n";
std::cout << " ✓ Transparent reasoning chains\n";
std::cout << " ✓ Storage in ThemisDB multi-model architecture\n";
std::cout << " ✓ Graph visualization export\n";
std::cout << "\nFor production use:\n";
std::cout << " - Integrate with LLM for sophisticated reasoning\n";
std::cout << " - Add vector search for similar scenario retrieval\n";
std::cout << " - Implement bias detection and mitigation\n";
std::cout << " - Connect to ethics evaluation metrics\n";
std::cout << " - Enable real-time monitoring and dashboards\n";
db.close();
return 0;
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
return 1;
}
}