Skip to content

Commit

Permalink
Event: Add mutually exclusive events mechanic (#21)
Browse files Browse the repository at this point in the history
* Event: Add mutually exclusive events mechanic

Add event_id, stores their respective mutually exclusive events in a vector, add a function that checks if the vector for all events are applied correctly.

* fix clang format

* cleanup deprecated functions and duplicate lines
  • Loading branch information
eric15342335 authored Apr 16, 2024
1 parent c02ed4b commit e4d0a77
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 19 deletions.
80 changes: 69 additions & 11 deletions events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,81 @@
* stores the event texts and modifiers
*/
#include "events.h"
// #include <iostream>
#include <iostream>
#include <algorithm>

Stock_event example = {
/** text */ "The FED has decreased the interest rate!",
/** duration */ 5,
/** percentage_permille */ 100,
/** type_of_event */ all_stocks,
/** category */ 0,
/** modifiers*/ {{standard_deviation, 0.1}, {mean, 1.02}, {lower_limit, 0}, {upper_limit, 20}}};
/** The list of all events that can be applied to the stocks */
std::vector<Stock_event> all_stock_events = {
{/** event_id */ 0,
/** mutually_exclusive_events */ {1, 2},
/** text */ "The FED has decreased the interest rate!",
/** duration */ 5,
/** percentage_permille */ 10,
/** type_of_event */ all_stocks,
/** category */ 0,
/** modifiers*/ {{standard_deviation, 0.1}, {mean, 2}, {lower_limit, 0}, {upper_limit, 20}}},
{/** event_id */ 1,
/** mutually_exclusive_events */ {0, 2},
/** text */ "The FED has increased the interest rate!",
/** duration */ 5,
/** percentage_permille */ 10,
/** type_of_event */ all_stocks,
/** category */ 0,
/** modifiers*/ {{standard_deviation, 0.1}, {mean, -2}, {lower_limit, -20}, {upper_limit, 0}}},
{/** event_id */ 2,
/** mutually_exclusive_events */ {},
/** text */ "This event is used for testing the mutual exclusivity function!",
/** duration */ 5,
/** percentage_permille */ 10,
/** type_of_event */ all_stocks,
/** category */ 0,
/** modifiers*/ {{standard_deviation, 0}, {mean, 0}, {lower_limit, 0}, {upper_limit, 0}}}};

float Stock_event::get_modifier(stock_modifiers modifier) {
return modifiers[modifier];
// print a map
void print_map(std::map<unsigned int, std::vector<unsigned int>> map) {
for (auto i : map) {
std::cout << i.first << ": ";
for (unsigned int j : i.second) {
std::cout << j << " ";
}
std::cout << std::endl;
}
}

/**
int main() {
std::cout << fed_decrease_interest_rate.get_modifier(stock_modifiers::standard_deviation) << std::endl;
// This outputs 0.1
std::cout << all_stock_events[0].modifiers[standard_deviation] << std::endl;
// Checks the events for mutual exclusivity
print_map(check_mutual_exclusivity(all_stock_events));
// Outputs:
// 0: 2
// 1: 2
// 2:
//@todo Consider remove this example in the future.
return 0;
}
*/

std::map<unsigned int, std::vector<unsigned int>> check_mutual_exclusivity(std::vector<Stock_event> all_events) {
std::map<unsigned int, std::vector<unsigned int>> mut_excl_map;
// Build the map
for (unsigned int i = 0; i < all_events.size(); i++) {
for (unsigned int j = 0; j < all_events[i].mutually_exclusive_events.size(); j++) {
mut_excl_map[all_events[i].event_id].push_back(all_events[i].mutually_exclusive_events[j]);
}
}
// If two events are mutually exclusive, they should be in each other's list.
// Remove such two events from the map.
// E.g. {0: [1,2], 1: [0], 2:[]} -> {0: [2]}
// In this case, 2 does not state that it is mutually exclusive with 0.
for (auto i : mut_excl_map) {
for (unsigned int j : i.second) {
if (std::find(mut_excl_map[j].begin(), mut_excl_map[j].end(), i.first) != mut_excl_map[j].end()) {
mut_excl_map[i.first].erase(std::remove(mut_excl_map[i.first].begin(), mut_excl_map[i.first].end(), j), mut_excl_map[i.first].end());
mut_excl_map[j].erase(std::remove(mut_excl_map[j].begin(), mut_excl_map[j].end(), i.first), mut_excl_map[j].end());
}
}
}
return mut_excl_map; // Add the missing return statement
}
29 changes: 22 additions & 7 deletions events.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
enum stock_modifiers {
standard_deviation,
mean,
mean, /** 5 means the mean of normal_distribution is increased by 5% */
lower_limit,
upper_limit
};
Expand All @@ -30,10 +30,19 @@ enum event_type {
};

/**
* The class that stores the event that will be applied to the stocks
* The data structre of an event that will be applied to the stocks.
* @struct Stock_event
*/
struct Stock_event {
public:
/** The id of the event.
* This is still required for checking, despite the fact that we are using a vector.
*/
unsigned int event_id;

/** A list of event_ids that this event is mutually exclusive with */
std::vector<unsigned int> mutually_exclusive_events;

/** The text that will be displayed to the player */
std::string text;

Expand All @@ -51,11 +60,6 @@ struct Stock_event {

/** Stores the stock_modifiers that the event applies. */
std::map<stock_modifiers, float> modifiers;

/**
* Get the value of a specific type of modifiers
*/
float get_modifier(stock_modifiers modifier);
};

/**
Expand All @@ -65,4 +69,15 @@ struct Stock_event {
*/
std::vector<Stock_event> pick_events(unsigned int num_events);

/**
* If A is mutually exclusive with B, then B is mutually exclusive with A.
* Check if these two events specifies each other as mutually exclusive in mutually_exclusive_events.
* @param all_events The list of all events
* @return A map of event_id to a vector of mutually exclusive event_ids that does not exist but should.
*/
std::map<unsigned int, std::vector<unsigned int>> check_mutual_exclusivity(std::vector<Stock_event> all_events);

/** Print a map to std::cout */
void print_map(std::map<unsigned int, std::vector<unsigned int>> map);

#endif
2 changes: 1 addition & 1 deletion stock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ float Stock::sum_attribute(stock_modifiers attribute) {
float sum = 0;
list<Stock_event>::iterator event_itr = events.begin();
while (event_itr != events.end()) {
sum += event_itr->get_modifier(attribute);
sum += event_itr->modifiers[attribute];
}
return sum;
}
Expand Down

0 comments on commit e4d0a77

Please sign in to comment.