Skip to content

Commit

Permalink
Merge remote-tracking branch 'group_2/development' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
FergusonAJ committed Dec 7, 2023
2 parents 52cead2 + 5051212 commit 7a413cd
Show file tree
Hide file tree
Showing 31 changed files with 2,023 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
[submodule "third_party/tinyxml2"]
path = third_party/tinyxml2
url = https://github.com/leethomason/tinyxml2.git
[submodule "third_party/json"]
path = third_party/json
url = https://github.com/nlohmann/json.git
15 changes: 5 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function(add_source_to_target TARGET_NAME SOURCE_PATH)
message(STATUS "Loading source: ${SOURCE_PATH}")
target_sources(${TARGET_NAME}
PRIVATE ${CMAKE_SOURCE_DIR}/${SOURCE_PATH}
)
)
endfunction()

# Set the necessary C++ flags, some of which are configuration-specific
Expand Down Expand Up @@ -59,11 +59,11 @@ if (SANITIZE_MEMORY)
target_include_directories(gp_train_main
PRIVATE ${CMAKE_SOURCE_DIR}/source/core
${CMAKE_SOURCE_DIR}/source/Agents
)
)
target_link_libraries(gp_train_main
PRIVATE tinyxml2
PRIVATE pthread
)
)


endif ()
Expand Down Expand Up @@ -91,21 +91,17 @@ if (BUILD_GP_ONLY)
target_include_directories(gp_train_main
PRIVATE ${CMAKE_SOURCE_DIR}/source/core
${CMAKE_SOURCE_DIR}/source/Agents
)
)
target_link_libraries(gp_train_main
PRIVATE tinyxml2
PRIVATE pthread
)
)

endif ()


# Typically you don't care so much for a third party library's tests to be
# run from your own project's code.
set(JSON_BuildTests OFF CACHE INTERNAL "")



# Configure json
set(JSON_SRC_DIR third_party/json)
set(JSON_BUILD_DIR json_build)
Expand Down Expand Up @@ -201,4 +197,3 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_SFML_INTERFACE")
endif ()
endforeach ()
endif ()

4 changes: 4 additions & 0 deletions source/DataCollection/AgentData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,9 @@ namespace DataCollection
std::string GetName() const {
return name;
}

std::vector<cse491::GridPosition> GetPositions() const {
return position;
}
};
} // namespace DataCollection
56 changes: 56 additions & 0 deletions source/DataCollection/AgentInteractionCollector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once

#include <unordered_map>
#include <string>
#include "JsonBuilder.hpp"

namespace DataCollection {

/**
* @brief A data collector class to quantify agent interactions.
*
* Useful for setting up graphs for common interactions.
*/
class AgentInteractionCollector {
private:
std::unordered_map<std::string, int> interactionData; /// Data storage map of agent name to interactions.
public:
/**
* Default constructor for AgentInteractionCollector
*/
AgentInteractionCollector() = default;

/**
* Getter for interaction data
* @return Const reference to the interaction data storage.
*/
const std::unordered_map<std::string, int>& GetInteractionData() { return interactionData; }

/**
* Get the amount of unique agents that occured
* @return int amount of agent occurances
*/
size_t GetUniqueInteractions() { return interactionData.size(); }

/**
* Increment occurance amount for a certain agent.
* @param agentName Agent name to record new interaction with
*/
void RecordInteraction(const std::string& agentName) { interactionData[agentName]++; }

void WriteToInteractionFile(const std::string filename){
JsonBuilder json_builder;
std::ofstream jsonfilestream(filename);
json_builder.StartArray("agentInteractions");
for (auto& [agentName, interactionCount] : interactionData) {
json_builder.AddName(agentName);
json_builder.AddInt("interactionCount", interactionCount);
json_builder.InputToArray("agentInteractions", json_builder.GetJSON());
json_builder.ClearJSON();
}
json_builder.WriteToFile(jsonfilestream, json_builder.GetJSONArray());
jsonfilestream.close();
}

};
}
33 changes: 33 additions & 0 deletions source/DataCollection/AgentReciever.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include <filesystem>
#include "DataReceiver.hpp"
#include "AgentData.hpp"
#include "JsonBuilder.hpp"

namespace DataCollection {

Expand Down Expand Up @@ -29,9 +31,19 @@ namespace DataCollection {
StoreIntoStorage(*agent);
}

// void StoreIntoStorage(AgentData obj) override {
// storage.push_back(obj);
// }

/**
* @brief Stores agent data into the storage and writes to a json file
*
* @param name the name of the agent
*/
void AddAgent(const std::string& name) {
AgentData agent(name);
agent_map[name] = std::make_shared<AgentData>(agent);

}

std::shared_ptr<AgentData> GetAgent(const std::string& name)
Expand All @@ -48,5 +60,26 @@ namespace DataCollection {
AgentData GetAgentData(const std::string& name) {
return *agent_map[name];
}

/**
* @brief Writes the stored AgentData Positions to a JSON file.
*
* @param agent The AgentData Position to be stored.
*/
void WriteToPositionFile(std::string path) {
std::ofstream jsonfilestream(path);
JsonBuilder json_builder;
json_builder.StartArray("AgentPositions");
for (auto& agent : agent_map) {
json_builder.Addagentname(agent.first);
for (auto& pos: agent.second->GetPositions()) {
json_builder.AddPosition(pos);
}
json_builder.InputToArray("AgentPositions", json_builder.GetJSON());
json_builder.ClearJSON();
}
json_builder.WriteToFile(jsonfilestream, json_builder.GetJSONArray());
jsonfilestream.close();
}
};
} // namespace DataCollection
75 changes: 75 additions & 0 deletions source/DataCollection/DamageCollector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#pragma once

#include <unordered_map>
#include <string>
#include <vector>
#include <numeric>
#include <fstream>
#include "JsonBuilder.hpp"

namespace DataCollection {

/**
* @brief A data collector class for damage of game items.
*
* Useful for setting up graphs for analysis of item balancing.
*/
class DamageCollector {
private:
std::unordered_map<std::string, std::vector<double>> damageData; // Damage storage map of item name to damage amounts
public:
/**
* @brief Default constructor for DamageCollector.
*/
DamageCollector() = default;

/**
* Store a damage amount for a certain item.
* @param itemName Name of the item to store the damage for
* @param damageAmt Amount of damage this item did
*/
void RecordDamageResult(const std::string& itemName, double damageAmt) {
damageData[itemName].push_back(damageAmt);
}

/**
* Get the damage amounts for a certain item.
* @param itemName Name of the item to get damage amounts for
* @return Reference to the vector of damage amounts
*/
std::vector<double>& GetDamageAmounts(std::string itemName) {
if (damageData.contains(itemName)) {
return damageData[itemName];
} else {
// Created only once, subsequent calls will reference this
static std::vector<double> empty;
return empty;
}
}

/**
* Calculate average damage for a certain item
* @param itemName Item name to calculate average for
* @return The average damage as a double, -1 if the item does not exist
*/
double CalculateAverageDamage(const std::string& itemName) {
if (damageData.contains(itemName)) {
std::vector<double>& damages = damageData[itemName];
return std::accumulate(damages.begin(), damages.end(), 0.0) / damages.size();
}

return -1.0;
}

void WriteToDamageFile(std::string path) {
std::ofstream jsonfilestream(path);
JsonBuilder json_builder;
for (auto& damage : damageData) {
json_builder.AddName(damage.first);
for (auto& damageAmt : damage.second) {
json_builder.AddDamage(damageAmt);
}
}
}
};
}
34 changes: 34 additions & 0 deletions source/DataCollection/DamageData.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <memory>
#include <utility>
#include "../core/Entity.hpp"
#include "../core/AgentBase.hpp"

namespace DataCollection {

/**
* @brief Represents damage related data between an agent and other entities (agents, items, grids, etc)
*/
class DamageData {
private:
std::shared_ptr<cse491::AgentBase> agent; /// The agent that took damage
std::shared_ptr<cse491::Entity> source; /// The source entity that inflicted the damage
int amount; /// The amount of damage taken from this source

public:
/**
* @brief Default constructor for a DamageData
* @param src Damage source entity
* @param amt Amount of damage taken
*/
DamageData(std::shared_ptr<cse491::AgentBase> agnt,
std::shared_ptr<cse491::Entity> src,
int amt) : agent(std::move(agnt)), source(std::move(src)), amount(amt) {}

/**
* @brief Destructor for DamageData class.
*/
~DamageData() = default;
};
}
17 changes: 17 additions & 0 deletions source/DataCollection/DamageReceiver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "DataReceiver.hpp"
#include "DamageData.hpp"

namespace DataCollection {

/**
* @brief Data receiver class specialized for storing DamageData objects.
*
* This class extends DataReceiver class and provides specific functionality
* for storing DamageData objects along with damage sources and amounts.
*/
class DamageReceiver : public DataReceiver<DamageData> {

};
}
Loading

0 comments on commit 7a413cd

Please sign in to comment.