Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QueueManager (Juan Chavez's WAVES project) #414

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions demos/SpatialCoop2017/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ CFLAGS_nat_debug := -g $(CFLAGS_all)

# Emscripten compiler information
CXX_web := emcc
OFLAGS_web_all := -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']" -s TOTAL_MEMORY=67108864 --js-library $(EMP_DIR)/emp/web/library_emp.js -s EXPORTED_FUNCTIONS="['_main', '_empCppCallback']" -s DISABLE_EXCEPTION_CATCHING=1 -s NO_EXIT_RUNTIME=1 #--embed-file configs
OFLAGS_web_all := -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']" -s TOTAL_MEMORY=67108864 --js-library $(EMP_DIR)/emp/web/library_emp.js -s EXPORTED_FUNCTIONS="['_main', '_empCppCallback', '_empDoCppCallback']" -s DISABLE_EXCEPTION_CATCHING=1 -s NO_EXIT_RUNTIME=1 -s WASM=0 #--embed-file configs
OFLAGS_web := -Oz -DNDEBUG
OFLAGS_web_debug := -g4 -pedantic -Wno-dollar-in-identifier-extension
OFLAGS_web_debug := -pedantic -Wno-dollar-in-identifier-extension

CFLAGS_web := $(CFLAGS_all) $(OFLAGS_web) $(OFLAGS_web_all)
CFLAGS_web_debug := $(CFLAGS_all) $(OFLAGS_web_debug) $(OFLAGS_web_all)
Expand Down
13 changes: 7 additions & 6 deletions demos/SpatialCoop2017/source/SimplePDWorld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,17 @@ class SimplePDWorld {

void Reset() { Setup(r,u,N,E); }

void Run(size_t steps=-1) {
if (steps > E) steps = E;
// Run the organisms!
size_t end_epoch = epoch + steps;
while (epoch < end_epoch) {
void Run(size_t steps=1) {
emilydolson marked this conversation as resolved.
Show resolved Hide resolved

for (size_t i = 0; i < steps; i++) {
for (size_t o = 0; o < N; o++) Repro();
epoch++;
}
}

void RunStep() {
for (size_t o = 0; o < N; o++) Repro();
}

size_t CountCoop();
void PrintNeighborInfo(std::ostream & os);
};
Expand Down
151 changes: 45 additions & 106 deletions demos/SpatialCoop2017/source/web/SimplePDWorld-web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
// Copyright (C) Michigan State University, 2017.
// Released under the MIT Software license; see doc/LICENSE

#include <functional>
#include <string>

#include "emp/web/web.hpp"
#include "emp/prefab/QueueManager.hpp"
#include "../SimplePDWorld.hpp"

namespace UI = emp::web;
Expand All @@ -15,6 +19,19 @@ SimplePDWorld world;
int cur_x = -1;
int cur_y = -1;

std::function<emp::SettingConfig()> SetupConfig = [](){
emp::SettingConfig config;
config.AddSetting<double>("r") = {world.GetR()};
config.AddSetting<double>("u") = {world.GetU()};
config.AddSetting<size_t>("N") = {world.GetN()};
config.AddSetting<size_t>("E") = {world.GetE()};

return config;
};

emp::SettingConfig config = SetupConfig();
emp::QueueManager run_list(config);

void DrawCanvas() {
UI::Canvas canvas = doc.Canvas("canvas");
canvas.Clear("black");
Expand Down Expand Up @@ -42,40 +59,7 @@ void CanvasClick(int x, int y) {
DrawCanvas();
}

struct RunInfo {
size_t id;

double r;
double u;
size_t N;
size_t E;

size_t cur_epoch;
size_t num_coop;
size_t num_defect;

RunInfo(size_t _id, double _r, double _u, size_t _N, size_t _E)
: id(_id), r(_r), u(_u), N(_N), E(_E)
, cur_epoch(0), num_coop(0), num_defect(0)
{ ; }
};

struct RunList {
emp::vector<RunInfo> runs;
size_t cur_run = 0;

void AddRun(double r, double u, size_t N, size_t E) {
size_t id = runs.size();
runs.emplace_back(id, r, u, N, E);
}

bool Active() const { return cur_run < runs.size(); }
};

RunList run_list;
int anim_step = 1;

void TogglePlay()
void TogglePlay()
{
auto & anim = doc.Animate("anim_world");
anim.ToggleActive();
Expand All @@ -88,48 +72,43 @@ void TogglePlay()
else but.SetLabel("Fast Forward!");
}

int main()
int anim_step = 1;

int main()
{
doc << "<h2>Spatial Prisoner's Dilema</h2>";
doc << "<h2>Spatial Prisoner's Dillemma</h2>";
std::function<std::string()> coop_func = []() { return std::to_string(world.CountCoop()); };
std::function<std::string()> defect_func = []() { return std::to_string(run_list.FrontRun().runinfo_config.GetValue<size_t>("N") - world.CountCoop()); };

run_list.AddMetric(coop_func, "Num Coop");
run_list.AddMetric(defect_func, "Num Defect");

auto canvas = doc.AddCanvas(world_size, world_size, "canvas");
// canvas.On("click", CanvasClick);
auto & anim = doc.AddAnimation("anim_world", [](){
if (run_list.Active()) {
size_t id = run_list.cur_run;
auto & run = run_list.runs[id];
if (run.cur_epoch == 0) { // Are we starting a new run?
world.Setup(run.r, run.u, run.N, run.E);
doc.AddAnimation("anim_world", [](){
// if queue has runs
if (!run_list.IsEmpty()) {
emp::QueueManager::RunInfo & run = run_list.FrontRun(); // Referencing current run
if (run.GetEpoch() == 0) { // Are we starting a new run?
world.Setup(run.runinfo_config.GetValue<double>("r"), run.runinfo_config.GetValue<double>("u"), run.runinfo_config.GetValue<size_t>("N"), run.runinfo_config.GetValue<size_t>("E"));
DrawCanvas();
}
}
run.IncEpoch(anim_step);
}
world.Run(anim_step);
DrawCanvas();
if (run_list.Active()) {
size_t id = run_list.cur_run;
size_t cur_epoch = world.GetEpoch();
if (run_list.runs[id].E <= cur_epoch) { // Are we done with this run?
run_list.cur_run++;
}
run_list.runs[id].cur_epoch = cur_epoch;
run_list.runs[id].num_coop = world.CountCoop();
run_list.runs[id].num_defect = run_list.runs[id].N - run_list.runs[id].num_coop;

auto result_tab = doc.Table("result_tab");
result_tab.Freeze();
result_tab.GetCell(id+1,5).ClearChildren() << cur_epoch;
result_tab.GetCell(id+1,6).ClearChildren() << run_list.runs[id].num_coop;
result_tab.GetCell(id+1,7).ClearChildren() << run_list.runs[id].num_defect;
result_tab.Activate();
if (!run_list.IsEmpty()) {
run_list.Update(); //calculations for table
}
} );
});

doc << "<br>";
doc.AddButton([&anim](){
doc.AddButton([](){
anim_step = 1;
TogglePlay();
}, "Play", "start_but");
doc.AddButton([](){ world.Run(1); DrawCanvas(); }, "Step", "step_but");
doc.AddButton([&anim](){
doc.AddButton([](){
anim_step = 100;
TogglePlay();
}, "Fast Forward!", "run_but");
Expand Down Expand Up @@ -173,52 +152,12 @@ int main()
<< "<br>"
<< "How many runs? ";

auto run_input = doc.AddTextArea([](const std::string & str){
size_t num_runs = emp::from_string<size_t>(str);
world.SetNumRuns(num_runs);
}, "run_count");
run_input.SetText(emp::to_string(world.GetNumRuns()));

doc.AddButton([run_input](){
//size_t num_runs = emp::from_string<size_t>(run_input.GetText());
size_t num_runs = world.GetNumRuns();
auto result_tab = doc.Table("result_tab");
for (int run_id = 0; run_id < num_runs; run_id++) {
run_list.AddRun(world.GetR(), world.GetU(), world.GetN(), world.GetE());

// Update the table.
int line_id = result_tab.GetNumRows();
result_tab.Rows(line_id+1);
result_tab.GetCell(line_id, 0) << run_id;
result_tab.GetCell(line_id, 1) << world.GetR();
result_tab.GetCell(line_id, 2) << world.GetU();
result_tab.GetCell(line_id, 3) << world.GetN();
result_tab.GetCell(line_id, 4) << world.GetE();
result_tab.GetCell(line_id, 5) << "Waiting..."; // world.GetE();
result_tab.GetCell(line_id, 6) << "Waiting..."; // world.CountCoop();
result_tab.GetCell(line_id, 7) << "Waiting..."; // (world.GetN() - world.CountCoop());

// Draw the new table.
result_tab.CellsCSS("border", "1px solid black");
result_tab.Redraw();
}
}, "Queue", "queue_but");
run_list.AddQueueButton([](){return SetupConfig();}, [](){return world.GetE();});

doc << "<br>";

auto result_tab = doc.AddTable(1,8, "result_tab");
result_tab.SetCSS("border-collapse", "collapse");
result_tab.SetCSS("border", "3px solid black");
result_tab.CellsCSS("border", "1px solid black");

result_tab.GetCell(0,0).SetHeader() << "ID";
result_tab.GetCell(0,1).SetHeader() << "<i>r</i>";
result_tab.GetCell(0,2).SetHeader() << "<i>u</i>";
result_tab.GetCell(0,3).SetHeader() << "<i>N</i>";
result_tab.GetCell(0,4).SetHeader() << "<i>E</i>";
result_tab.GetCell(0,5).SetHeader() << "Epoch";
result_tab.GetCell(0,6).SetHeader() << "Num Coop";
result_tab.GetCell(0,7).SetHeader() << "Num Defect";
doc << run_list.GetDiv();
run_list.BuildTable();

DrawCanvas();
}
}
78 changes: 77 additions & 1 deletion include/emp/config/SettingConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace emp {
virtual bool SetValueID(size_t) {return false; } ///< Setup cur value in linked variable
virtual bool IsComboSetting() { return false; } ///< Do we have a combo setting?
virtual size_t GetID() const { return (size_t) -1; } ///< Combination ID for this setting.
virtual emp::Ptr<SettingBase> Clone() const = 0;

bool IsOptionMatch(const std::string & test_option) const { return test_option == option; }
bool IsFlagMatch(const char test_flag) const { return test_flag == flag; }
Expand All @@ -70,6 +71,18 @@ namespace emp {
emp::Ptr<T> _var=nullptr) ///< Pointer to variable to set (optional)
: SettingBase(_name, _desc, _flag, _arg), var_ptr(_var) { }

~SettingInfo(){if(var_ptr){var_ptr.Delete();}}

emp::Ptr<SettingBase> Clone() const override {
emilydolson marked this conversation as resolved.
Show resolved Hide resolved
emp::Ptr<T> new_var_ptr = nullptr;
if (var_ptr) {
new_var_ptr = NewPtr<T>(*var_ptr);
}
emp::Ptr<SettingInfo<T>> setting_info_ptr = emp::NewPtr<SettingInfo<T>>(name, desc, flag, args_label, new_var_ptr);
setting_info_ptr->value = this->value;
return setting_info_ptr;
}

size_t GetSize() const override { return 1; }
std::string AsString() const override { return emp::to_string(value); }
std::string AsString(size_t id) const override {
Expand Down Expand Up @@ -99,6 +112,19 @@ namespace emp {
emp::Ptr<T> _var=nullptr) ///< Pointer to variable to set (optional)
: SettingBase(_name, _desc, _flag, _args), var_ptr(_var) { }

~ComboSettingInfo(){if(var_ptr){var_ptr.Delete();}}

emp::Ptr<SettingBase> Clone() const override {
emp::Ptr<T> new_var_ptr = nullptr;
if (var_ptr) {
new_var_ptr = NewPtr<T>(*var_ptr);
}
auto csi_ptr = emp::NewPtr<ComboSettingInfo<T>>(name, desc, flag, args_label, new_var_ptr);
csi_ptr->values = this->values;
csi_ptr->id = this->id;
return csi_ptr;
}

size_t GetSize() const override { return values.size(); }
std::string AsString() const override {
std::stringstream ss;
Expand All @@ -108,8 +134,11 @@ namespace emp {
}
return ss.str();
}

std::string AsString(size_t id) const override {
return emp::to_string(values[id]);
std::stringstream ss;
ss << values[id];
return ss.str();
}

bool FromString(const std::string_view & input) override {
Expand Down Expand Up @@ -172,6 +201,23 @@ namespace emp {
public:
SettingConfig() = default;

SettingConfig(const SettingConfig &other) {
combo_settings.resize(other.combo_settings.size());
for (const auto &entry : other.setting_map) {
setting_map[entry.first] = other.setting_map.at(entry.first)->Clone();
}
for (size_t i = 0; i < combo_settings.size(); i++) {
combo_settings[i] = setting_map[other.combo_settings[i]->name];
}

action_map = other.action_map;
cur_combo = other.cur_combo;
combo_id = other.combo_id;
unused_args = other.unused_args;
errors = other.errors;
exe_name = other.exe_name;
}

~SettingConfig() {
for (auto [name,ptr] : setting_map) ptr.Delete();
}
Expand All @@ -184,6 +230,24 @@ namespace emp {
bool HasUnusedArgs() const { return unused_args.size(); }
bool HasErrors() const { return errors.size(); }

/// Retrieves all of the setting names in config and places into std::vector
std::vector<std::string> GetSettingMapNames() const {
std::vector<std::string> result;
for (const auto &p : setting_map) {
result.push_back(p.first);
}
return result;
}

/// Retrieves all of the setting names in config and places into std::vector
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't think this docstring is quite right

std::vector<emp::Ptr<SettingBase>> GetSettingMapBase() const {
std::vector<emp::Ptr<SettingBase>> result;
for (const auto &p : setting_map) {
result.push_back(p.second);
}
return result;
}

/// Get the current value of a specified setting.
template <typename T>
const T & GetValue(const std::string & name) const {
Expand Down Expand Up @@ -235,6 +299,18 @@ namespace emp {
return new_ptr->value;
}

/// Add a new setting not linked to a variable

template <typename T>
T &AddSetting(const std::string &name,
const std::string &desc = "",
const char option_flag = '\0') {
emp_assert(!emp::Has(setting_map, name));
auto new_ptr = emp::NewPtr<SettingInfo<T>>(name, desc, option_flag, "Value");
setting_map[name] = new_ptr;
return new_ptr->value;
}

/// Add a new setting of a specified type. Returns the (initially empty) vector of values
/// to allow easy setting.
/// Example:
Expand Down
3 changes: 2 additions & 1 deletion include/emp/datastructs/map_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

#include "../base/map.hpp"
#include "../base/vector.hpp"
#include "../meta/meta.hpp"

namespace emp {

/// Take any map type, and run find to determine if a key is present.
template <class MAP_T, class KEY_T>
inline bool Has( const MAP_T & in_map, const KEY_T & key ) {
inline auto Has( const MAP_T & in_map, const KEY_T & key ) -> emp::sfinae_decoy<bool, decltype(in_map.find(key))> {
return in_map.find(key) != in_map.end();
}

Expand Down
6 changes: 6 additions & 0 deletions include/emp/datastructs/vector_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ namespace emp {
return FindValue(v, val) >= 0;
}

// String version to fix template type deduction error
template <typename T>
bool Has(const emp::vector<T> & v, const std::string & val) {
return FindValue(v, val) >= 0;
}

/// Return number of times a value occurs in a vector
template <typename T>
int Count(const emp::vector<T> & vec, const T & val) {
Expand Down
Loading