Skip to content

Commit

Permalink
digital_evolution updated to ltr/env idiom, copy ctor and assignment …
Browse files Browse the repository at this point in the history
…fixed; metapop next
  • Loading branch information
dknoester committed Jul 9, 2014
1 parent 26fe67a commit 216cae1
Show file tree
Hide file tree
Showing 26 changed files with 389 additions and 1,805 deletions.
640 changes: 35 additions & 605 deletions archive/genetic_algorithm.h

Large diffs are not rendered by default.

28 changes: 0 additions & 28 deletions ealib.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,6 @@
423CBDFB157D0E450003A644 /* test_rng.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = test_rng.cpp; sourceTree = "<group>"; };
423CBE57157D0FCC0003A644 /* test-ealib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "test-ealib"; sourceTree = BUILT_PRODUCTS_DIR; };
423CBE6F157D10C80003A644 /* libboost_unit_test_framework.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libboost_unit_test_framework.dylib; path = /usr/local/lib/libboost_unit_test_framework.dylib; sourceTree = "<absolute>"; };
4243E771189417F900431D18 /* genetic_algorithm.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = genetic_algorithm.h; sourceTree = "<group>"; };
4243E7821894180F00431D18 /* test-ealib copy */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "test-ealib copy"; sourceTree = BUILT_PRODUCTS_DIR; };
4243E7831894184F00431D18 /* test_genetic_algorithm.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = test_genetic_algorithm.cpp; sourceTree = "<group>"; };
4245004516D3BA8E007827D7 /* test_digital_evolution_types.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = test_digital_evolution_types.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -607,15 +606,6 @@
428B7260184F77AB00E3BB4E /* graph.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = graph.h; sourceTree = "<group>"; };
428B7261184F77AB00E3BB4E /* individual.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = individual.h; sourceTree = "<group>"; };
428B7262184F77AB00E3BB4E /* island_model.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = island_model.h; sourceTree = "<group>"; };
428B7264184F77AB00E3BB4E /* action.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = action.h; sourceTree = "<group>"; };
428B7265184F77AB00E3BB4E /* cover.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cover.h; sourceTree = "<group>"; };
428B7266184F77AB00E3BB4E /* environment.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = environment.h; sourceTree = "<group>"; };
428B7267184F77AB00E3BB4E /* fitness.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = fitness.h; sourceTree = "<group>"; };
428B7268184F77AB00E3BB4E /* learning_classifier.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = learning_classifier.h; sourceTree = "<group>"; };
428B7269184F77AB00E3BB4E /* match.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = match.h; sourceTree = "<group>"; };
428B726A184F77AB00E3BB4E /* message.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = message.h; sourceTree = "<group>"; };
428B726B184F77AB00E3BB4E /* reward.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = reward.h; sourceTree = "<group>"; };
428B726C184F77AB00E3BB4E /* woods.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = woods.h; sourceTree = "<group>"; };
428B726D184F77AB00E3BB4E /* lifecycle.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = lifecycle.h; sourceTree = "<group>"; };
428B726E184F77AB00E3BB4E /* line_of_descent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = line_of_descent.h; sourceTree = "<group>"; };
428B726F184F77AB00E3BB4E /* metadata.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = metadata.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1094,15 +1084,13 @@
428B724E184F77AB00E3BB4E /* fitness_function.h */,
428B724F184F77AB00E3BB4E /* fitness_functions */,
428B7255184F77AB00E3BB4E /* functional.h */,
4243E771189417F900431D18 /* genetic_algorithm.h */,
428B7257184F77AB00E3BB4E /* generational_models */,
428B725E184F77AB00E3BB4E /* generators.h */,
4212C0C319375DAB001D435A /* genome_types */,
428B7260184F77AB00E3BB4E /* graph.h */,
428B7261184F77AB00E3BB4E /* individual.h */,
428B7262184F77AB00E3BB4E /* island_model.h */,
42B89EC819377D7C00435794 /* iterators */,
428B7263184F77AB00E3BB4E /* lcs */,
428B726D184F77AB00E3BB4E /* lifecycle.h */,
428B726E184F77AB00E3BB4E /* line_of_descent.h */,
426F9F5C18E363CC00910682 /* math */,
Expand Down Expand Up @@ -1188,22 +1176,6 @@
path = generational_models;
sourceTree = "<group>";
};
428B7263184F77AB00E3BB4E /* lcs */ = {
isa = PBXGroup;
children = (
428B7264184F77AB00E3BB4E /* action.h */,
428B7265184F77AB00E3BB4E /* cover.h */,
428B7266184F77AB00E3BB4E /* environment.h */,
428B7267184F77AB00E3BB4E /* fitness.h */,
428B7268184F77AB00E3BB4E /* learning_classifier.h */,
428B7269184F77AB00E3BB4E /* match.h */,
428B726A184F77AB00E3BB4E /* message.h */,
428B726B184F77AB00E3BB4E /* reward.h */,
428B726C184F77AB00E3BB4E /* woods.h */,
);
path = lcs;
sourceTree = "<group>";
};
428B7280184F77AB00E3BB4E /* selection */ = {
isa = PBXGroup;
children = (
Expand Down
17 changes: 6 additions & 11 deletions examples/logic9.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ using namespace ealib;
of the EQU logic task (bitwise equals).
*/
struct lifecycle : public default_lifecycle {

//! Called as the final step of EA construction (must not depend on configuration parameters)
//! Called after EA initialization.
template <typename EA>
void after_construction(EA& ea) {
void after_initialization(EA& ea) {
// for compatibility with emscripten, we're fully qualifying the names of
// some instructions. for c++ only, feel free to drop them.
using namespace ealib::instructions;
Expand Down Expand Up @@ -59,15 +58,11 @@ struct lifecycle : public default_lifecycle {
append_isa<h_divide>(ea);
append_isa<fixed_input>(ea);
append_isa<output>(ea);
}

//! Initialize the EA (may use configuration parameters)
template <typename EA>
void initialize(EA& ea) {

typedef typename EA::task_library_type::task_ptr_type task_ptr_type;
typedef typename EA::resource_ptr_type resource_ptr_type;

// Add tasks
// tasks:
task_ptr_type task_not = make_task<tasks::task_not,catalysts::additive<1> >("not", ea);
task_ptr_type task_nand = make_task<tasks::task_nand,catalysts::additive<1> >("nand", ea);
task_ptr_type task_and = make_task<tasks::task_and,catalysts::additive<2> >("and", ea);
Expand All @@ -78,7 +73,7 @@ struct lifecycle : public default_lifecycle {
task_ptr_type task_xor = make_task<tasks::task_xor,catalysts::additive<3> >("xor", ea);
task_ptr_type task_equals = make_task<tasks::task_equals,catalysts::additive<4> >("equals", ea);


// resources:
resource_ptr_type resA = make_resource("resA", 0.1, 100.0, 1.0, 0.01, 0.05, ea);
resource_ptr_type resB = make_resource("resB", 100.0, 1.0, 0.01, 0.05, ea);
resource_ptr_type resC = make_resource("resC", 100.0, 1.0, 0.01, 0.05, ea);
Expand All @@ -88,7 +83,7 @@ struct lifecycle : public default_lifecycle {
resource_ptr_type resG = make_resource("resG", 100.0, 1.0, 0.01, 0.05, ea);
resource_ptr_type resH = make_resource("resH", 100.0, 1.0, 0.01, 0.05, ea);
resource_ptr_type resI = make_resource("resI", 100.0, 1.0, 0.01, 0.05, ea);

task_not->consumes(resA);
task_nand->consumes(resB);
task_and->consumes(resC);
Expand Down
2 changes: 1 addition & 1 deletion libea/include/ea/ancestors.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace ealib {
template <typename EA>
void generate_initial_population(EA& ea) {
generate_population(ea);
ea.lifecycle().initial_population(ea);
ea.lifecycle().after_initial_population(ea);
}

/*! Fills the entire population with ancestors.
Expand Down
119 changes: 30 additions & 89 deletions libea/include/ea/cmdline_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@

namespace ealib {

LIBEA_MD_DECL(COMMAND_LINE, "ea.run.command_line", std::string);

//! Abstract base class allowing for a limited set of interactions with an EA.
class ea_interface {
public:
Expand Down Expand Up @@ -103,9 +101,7 @@ namespace ealib {
ci->_tools[tool_type::name()] = p;
}


/*! This selector is used with the command-line interface to DISABLE registration.
*/
//! This selector is used with the command-line interface to DISABLE registration.
struct do_not_registerS { };

/*! Command-line interface to an EA.
Expand All @@ -123,7 +119,11 @@ namespace ealib {
registrar::instance()->register_ea(this);
}

//! Non-registering constructor.
/*! Non-registering constructor.
This enables us to use the cmdline_interface class without triggering
the registrar defined above.
*/
cmdline_interface(do_not_registerS) : ea_interface(), _ea_options("Configuration file and command-line options") {
}

Expand All @@ -136,14 +136,13 @@ namespace ealib {
//! Gather the events that occur during a trial of this EA.
virtual void gather_events(EA& ea) { }

//! Called before initialization (good place to calculate config options).
virtual void before_initialization(EA& ea) { }
//! Called after initialization (good place to calculate config options).
virtual void after_initialization(EA& ea) { }

//! Execute an EA based on the given command-line parameters.
virtual void exec(int argc, char* argv[]) {
gather_options();
parse_all(argc, argv);

ea_type ea;

if(_vm.count("analyze")) {
Expand Down Expand Up @@ -214,14 +213,23 @@ namespace ealib {
}

po::notify(_vm);

// print 'em out, stuff the options in metadata:
std::cerr << std::endl << "Active configuration options:" << std::endl;
for(po::variables_map::iterator i=_vm.begin(); i!=_vm.end(); ++i) {
const std::string& k=i->first;
const std::string& v=i->second.as<std::string>();
put(k, v, _md);
std::cerr << "\t" << k << "=" << v << std::endl;
}
std::cerr << std::endl;
}

//! Analyze an EA instance.
void analyze(ea_type& ea) {
load_if(ea);
apply(ea);
before_initialization(ea);
ea.initialize();
ea.initialize(_md);
after_initialization(ea);
gather_tools();

std::string toolname = _vm["analyze"].template as<std::string>();
Expand All @@ -236,110 +244,42 @@ namespace ealib {
//! Continue a previously-checkpointed EA.
void continue_checkpoint(ea_type& ea) {
load(ea);

// conditionally apply command-line and/or file parameters:
if(_vm.count("override")) {
apply(ea);
}

// conditionally reset all fitnesses
// conditionally reset the EA:
if(_vm.count("reset")) {
ea.reset();
}

before_initialization(ea);
ea.initialize();
ea.initialize(_md);
after_initialization(ea);
gather_events(ea);
if(_vm.count("verbose")) {
add_event<datafiles::runtime>(ea);
}
ea.lifecycle().advance_all(ea);
}

//! Initialize an EA.
void initialize_ea(ea_type& ea) {
apply(ea);

if(exists<RNG_SEED>(ea)) {
ea.reset_rng(get<RNG_SEED>(ea));
}

before_initialization(ea);
ea.initialize();
//! Run the EA.
void run(ea_type& ea) {
ea.initialize(_md);
after_initialization(ea);
gather_events(ea);
if(_vm.count("verbose")) {
add_event<datafiles::runtime>(ea);
}
generate_initial_population(ea);
}

//! Continue an already initialized EA for another epoch.
void continue_ea(ea_type& ea) {
ea.lifecycle().advance_all(ea);
}

//! Run the EA.
void run(ea_type& ea) {
initialize_ea(ea);
continue_ea(ea);
}

protected:

//! Apply meta-data to a single population.
void apply(const std::string& k, const std::string& v, EA& ea, bool verbose, singlePopulationS) {
if(verbose) {
std::cerr << "\t" << k << "=" << v << std::endl;
}
put(k, v, ea.md());
}

//! Apply meta-data to multiple populations.
void apply(const std::string& k, const std::string& v, EA& ea, bool verbose, multiPopulationS) {
if(verbose) {
std::cerr << "\t" << k << "=" << v << " (+subpopulations)" << std::endl;
}
put(k, v, ea.md());
for(typename EA::iterator i=ea.begin(); i!=ea.end(); ++i) {
put(k,v,i->md());
}
}

//! Apply any command line options to the EA.
void apply(EA& ea) {
namespace po = boost::program_options;
bool verbose = (_vm.count("verbose") > 0);

if(verbose) {
std::cerr << std::endl << "Active configuration options:" << std::endl;
}

for(po::variables_map::iterator i=_vm.begin(); i!=_vm.end(); ++i) {
const std::string& k=i->first;
const std::string& v=i->second.as<std::string>();
apply(k, v, ea, verbose, typename EA::population_structure_tag());
}

if(verbose) {
std::cerr << std::endl;
}
}

//! Returns true if a checkpoint file should be loaded.
bool has_checkpoint() {
return (_vm.count("checkpoint") > 0);
}

//! Load the EA from the checkpoint file, if present.
void load_if(ea_type& ea) {
if(has_checkpoint()) {
if(_vm.count("checkpoint") > 0) {
load(ea);
}
}

//! Load the EA from the checkpoint file.
void load(ea_type& ea) {
if(!has_checkpoint()) {
if(_vm.count("checkpoint") == 0) {
throw fatal_error_exception("required checkpoint file not found.");
}
std::string cpfile(_vm["checkpoint"].template as<std::string>());
Expand All @@ -354,6 +294,7 @@ namespace ealib {

boost::program_options::options_description _ea_options; //!< Options that are configured for this EA.
boost::program_options::variables_map _vm; //!< Variables (loaded from options).
metadata _md; //!< Metadata for the EA.
tool_registry _tools; //!< Registry for EA analysis tools.
};

Expand Down
Loading

0 comments on commit 216cae1

Please sign in to comment.