[Avida-SVN] r2975 - in development/source: main tools
brysonda at myxo.css.msu.edu
brysonda at myxo.css.msu.edu
Wed Nov 26 11:18:09 PST 2008
Author: brysonda
Date: 2008-11-26 14:18:09 -0500 (Wed, 26 Nov 2008)
New Revision: 2975
Modified:
development/source/main/cEnvironment.cc
development/source/main/cPopulation.cc
development/source/main/cStateGrid.h
development/source/tools/cArgContainer.cc
development/source/tools/cArgSchema.cc
development/source/tools/cArgSchema.h
development/source/tools/tDictionary.h
Log:
Implement state grid load command.
Add support to cArgSchema for defining range limits on integer and double argument types.
Modified: development/source/main/cEnvironment.cc
===================================================================
--- development/source/main/cEnvironment.cc 2008-11-26 19:06:02 UTC (rev 2974)
+++ development/source/main/cEnvironment.cc 2008-11-26 19:18:09 UTC (rev 2975)
@@ -29,6 +29,7 @@
#include "cEnvironment.h"
+#include "cArgSchema.h"
#include "cAvidaContext.h"
#include "cEnvReqs.h"
#include "cHardwareManager.h"
@@ -47,6 +48,9 @@
#include "cTaskEntry.h"
#include "cTools.h"
#include "cWorld.h"
+#include "tAutoRelease.h"
+
+
#include <iostream>
#include <algorithm>
@@ -731,7 +735,88 @@
bool cEnvironment::LoadStateGrid(cString desc)
{
- // @TODO - state grid load line
+ // First component is the name
+ cString name = desc.Pop(':');
+
+ cArgSchema schema(':','=');
+
+ // Integer Arguments
+ schema.AddEntry("width", 0, 0, INT_MAX);
+ schema.AddEntry("height", 1, 0, INT_MAX);
+ schema.AddEntry("initx", 2, 0, INT_MAX);
+ schema.AddEntry("inity", 3, 0, INT_MAX);
+ schema.AddEntry("initfacing", 4, 0, 7);
+
+ // String Arguments
+ schema.AddEntry("states", 0, cArgSchema::SCHEMA_STRING);
+ schema.AddEntry("grid", 1, cArgSchema::SCHEMA_STRING);
+
+ tList<cString> errors;
+ tAutoRelease<cArgContainer> args(cArgContainer::Load(desc, schema, &errors));
+ if (args.IsNull() || errors.GetSize() > 0) {
+ cString* err_str;
+ while ((err_str = errors.Pop()) != NULL) {
+ cerr << "error: " << *err_str << endl;
+ delete err_str;
+ }
+ return false;
+ }
+
+ int width = args->GetInt(0);
+ int height = args->GetInt(1);
+ int initx = args->GetInt(2);
+ int inity = args->GetInt(3);
+ int initfacing = args->GetInt(4);
+
+ if (initx >= width || inity >= height) {
+ cerr << "error: initx and inity must not exceed (width - 1) and (height - 1)" << endl;
+ return false;
+ }
+
+
+ cString temp;
+
+ tArray<cString> states;
+ cString statestr = args->GetString(0);
+ statestr.Trim();
+ while (statestr.GetSize()) {
+ temp = statestr.Pop(',');
+ temp.Trim();
+ for (int i = 0; i < states.GetSize(); i++) {
+ if (temp == states[i]) {
+ cerr << "error: duplicate state identifier for state grid " << name << endl;
+ return false;
+ }
+ }
+ states.Push(temp);
+ }
+ if (states.GetSize() == 0) {
+ cerr << "error: no states defined for state grid " << name << endl;
+ return false;
+ }
+
+ tArray<int> grid(width * height);
+ cString gridstr = args->GetString(1);
+ int cell = 0;
+ while (gridstr.GetSize() && cell < grid.GetSize()) {
+ temp = gridstr.Pop(',');
+ temp.Trim();
+ for (int i = 0; i < states.GetSize(); i++) {
+ if (temp == states[i]) {
+ grid[cell++] = i;
+ break;
+ }
+ }
+ cerr << "error: state identifier undefined for cell (" << (cell / width) << ", " << (cell % width) << ") in state grid " << name << endl;
+ return false;
+ }
+ if (cell != (grid.GetSize() - 1) || gridstr.GetSize() > 0) {
+ cerr << "error: grid definition size mismatch for state grid " << name << endl;
+ return false;
+ }
+
+ m_state_grids.Push(new cStateGrid(name, width, height, initx, inity, initfacing, states, grid));
+
return true;
}
Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc 2008-11-26 19:06:02 UTC (rev 2974)
+++ development/source/main/cPopulation.cc 2008-11-26 19:18:09 UTC (rev 2975)
@@ -377,18 +377,18 @@
environment.SetupInputs(ctx, parent_cell.m_inputs);
int pc_phenotype = m_world->GetConfig().PRECALC_PHENOTYPE.Get();
- if (pc_phenotype){
+ if (pc_phenotype) {
cCPUTestInfo test_info;
cTestCPU* test_cpu = m_world->GetHardwareManager().CreateTestCPU();
test_info.UseManualInputs(parent_cell.GetInputs()); // Test using what the environment will be
test_cpu->TestGenome(ctx, test_info, parent_organism.GetHardware().GetMemory()); // Use the true genome
if (pc_phenotype & 1) { // If we must update the merit
parent_phenotype.SetMerit(test_info.GetTestPhenotype().GetMerit());
- }
+ }
if (pc_phenotype & 2) { // If we must update the gestation time
parent_phenotype.SetGestationTime(test_info.GetTestPhenotype().GetGestationTime());
- }
- parent_phenotype.SetFitness(parent_phenotype.GetMerit().CalcFitness(parent_phenotype.GetGestationTime())); //Update fitness
+ }
+ parent_phenotype.SetFitness(parent_phenotype.GetMerit().CalcFitness(parent_phenotype.GetGestationTime())); // Update fitness
delete test_cpu;
}
}
Modified: development/source/main/cStateGrid.h
===================================================================
--- development/source/main/cStateGrid.h 2008-11-26 19:06:02 UTC (rev 2974)
+++ development/source/main/cStateGrid.h 2008-11-26 19:18:09 UTC (rev 2975)
@@ -38,7 +38,7 @@
int m_init_x;
int m_init_y;
int m_init_facing;
- int m_num_states;
+ tArray<cString> m_states;
tArray<int> m_grid;
cStateGrid(); // @not_implemented
@@ -46,8 +46,8 @@
cStateGrid& operator=(const cStateGrid&); // @not_implemented
public:
- cStateGrid(const cString& name, int w, int h, int x, int y, int facing, int num_states, const tArray<int>& grid)
- : m_name(name), m_w(w), m_h(h), m_init_x(x), m_init_y(y), m_init_facing(facing), m_num_states(num_states), m_grid(grid) { ; }
+ cStateGrid(const cString& name, int w, int h, int x, int y, int f, const tArray<cString>& states, const tArray<int>& grid)
+ : m_name(name), m_w(w), m_h(h), m_init_x(x), m_init_y(y), m_init_facing(f), m_states(states), m_grid(grid) { ; }
~cStateGrid() { ; }
int GetWidth() const { return m_w; }
@@ -55,7 +55,7 @@
int GetInitialX() const { return m_init_x; }
int GetInitialY() const { return m_init_y; }
int GetInitialFacing() const { return m_init_facing; }
- int GetNumStates() const { return m_num_states; }
+ int GetNumStates() const { return m_states.GetSize(); }
int GetStateAt(int x, int y) const { return m_grid[x * m_w + y]; }
};
Modified: development/source/tools/cArgContainer.cc
===================================================================
--- development/source/tools/cArgContainer.cc 2008-11-26 19:06:02 UTC (rev 2974)
+++ development/source/tools/cArgContainer.cc 2008-11-26 19:18:09 UTC (rev 2975)
@@ -59,10 +59,30 @@
case cArgSchema::SCHEMA_INT:
set_ints[index] = true;
ret->m_ints[index] = arg_ent.AsInt();
+ if (!schema.ValidateInt(index, ret->m_ints[index])) {
+ cString* err_str = new cString();
+ cString name;
+ if (schema.GetIntName(index, name)) {
+ err_str->Set("Value of '%s' exceeds its defined range", static_cast<const char*>(name));
+ } else {
+ err_str->Set("Invalid int schema entry at index %d.", index);
+ }
+ errors->PushRear(err_str);
+ }
break;
case cArgSchema::SCHEMA_DOUBLE:
set_doubles[index] = true;
ret->m_doubles[index] = arg_ent.AsDouble();
+ if (!schema.ValidateDouble(index, ret->m_doubles[index])) {
+ cString* err_str = new cString();
+ cString name;
+ if (schema.GetDoubleName(index, name)) {
+ err_str->Set("Value of '%s' exceeds its defined range", static_cast<const char*>(name));
+ } else {
+ err_str->Set("Invalid double schema entry at index %d.", index);
+ }
+ errors->PushRear(err_str);
+ }
break;
case cArgSchema::SCHEMA_STRING:
set_strings[index] = true;
Modified: development/source/tools/cArgSchema.cc
===================================================================
--- development/source/tools/cArgSchema.cc 2008-11-26 19:06:02 UTC (rev 2974)
+++ development/source/tools/cArgSchema.cc 2008-11-26 19:18:09 UTC (rev 2975)
@@ -76,6 +76,30 @@
return true;
}
+bool cArgSchema::AddEntry(cString in_name, int in_idx, int upper, int lower)
+{
+ if (AddEntry(in_name, in_idx, SCHEMA_INT)) {
+ m_ints[in_idx]->has_range_limits = true;
+ m_ints[in_idx]->r_l_int = lower;
+ m_ints[in_idx]->r_u_int = upper;
+ return true;
+ }
+
+ return false;
+}
+
+bool cArgSchema::AddEntry(cString in_name, int in_idx, int upper, int lower, int def)
+{
+ if (AddEntry(in_name, in_idx, def)) {
+ m_ints[in_idx]->has_range_limits = true;
+ m_ints[in_idx]->r_l_int = lower;
+ m_ints[in_idx]->r_u_int = upper;
+ return true;
+ }
+
+ return false;
+}
+
bool cArgSchema::AddEntry(cString in_name, int in_idx, double def)
{
AdjustArgName(in_name);
@@ -90,6 +114,30 @@
return true;
}
+bool cArgSchema::AddEntry(cString in_name, int in_idx, double upper, double lower)
+{
+ if (AddEntry(in_name, in_idx, SCHEMA_DOUBLE)) {
+ m_ints[in_idx]->has_range_limits = true;
+ m_ints[in_idx]->r_l_double = lower;
+ m_ints[in_idx]->r_u_double = upper;
+ return true;
+ }
+
+ return false;
+}
+
+bool cArgSchema::AddEntry(cString in_name, int in_idx, double upper, double lower, double def)
+{
+ if (AddEntry(in_name, in_idx, def)) {
+ m_ints[in_idx]->has_range_limits = true;
+ m_ints[in_idx]->r_l_double = lower;
+ m_ints[in_idx]->r_u_double = upper;
+ return true;
+ }
+
+ return false;
+}
+
bool cArgSchema::AddEntry(cString in_name, int in_idx, const cString& def)
{
AdjustArgName(in_name);
Modified: development/source/tools/cArgSchema.h
===================================================================
--- development/source/tools/cArgSchema.h 2008-11-26 19:06:02 UTC (rev 2974)
+++ development/source/tools/cArgSchema.h 2008-11-26 19:18:09 UTC (rev 2975)
@@ -47,16 +47,26 @@
double def_double;
cString* def_string;
};
+ bool has_range_limits;
+ union {
+ int r_l_int;
+ double r_l_double;
+ };
+ union {
+ int r_u_int;
+ double r_u_double;
+ };
+
sArgSchemaEntry() { ; }
sArgSchemaEntry(const cString& in_name, int in_idx, tType in_type) // Required Argument (supplied type)
: name(in_name), type(in_type), index(in_idx), optional(false) { ; }
sArgSchemaEntry(const cString& in_name, int in_idx, int def) // Optional Int Argument
- : name(in_name), type(SCHEMA_INT), index(in_idx), optional(true), def_int(def) { ; }
+ : name(in_name), type(SCHEMA_INT), index(in_idx), optional(true), def_int(def), has_range_limits(false) { ; }
sArgSchemaEntry(const cString& in_name, int in_idx, double def) // Optional Double Argument
- : name(in_name), type(SCHEMA_DOUBLE), index(in_idx), optional(true), def_double(def) { ; }
+ : name(in_name), type(SCHEMA_DOUBLE), index(in_idx), optional(true), def_double(def), has_range_limits(false) { ; }
sArgSchemaEntry(const cString& in_name, int in_idx, cString* def) // Optional String Argument
- : name(in_name), type(SCHEMA_STRING), index(in_idx), optional(true), def_string(def) { ; }
+ : name(in_name), type(SCHEMA_STRING), index(in_idx), optional(true), def_string(def), has_range_limits(false) { ; }
~sArgSchemaEntry() { if (type == SCHEMA_STRING && optional) delete def_string; } // Cleanup string object
};
@@ -83,10 +93,14 @@
char GetValueSeparator() const { return m_sep_value; }
bool IsCaseSensitive() const { return m_case_sensitive; }
- bool AddEntry(cString in_name, int in_idx, tType in_type); // Required Argument (supplied type)
- bool AddEntry(cString in_name, int in_idx, int def); // Optional Int Argument
- bool AddEntry(cString in_name, int in_idx, double def); // Optional Double Argument
- bool AddEntry(cString in_name, int in_idx, const cString& def); // Optional String Argument
+ bool AddEntry(cString in_name, int in_idx, tType in_type); // Required Argument (supplied type)
+ bool AddEntry(cString in_name, int in_idx, int def); // Optional Int Argument
+ bool AddEntry(cString in_name, int in_idx, int lower, int upper); // Required Int Argument (with range limits)
+ bool AddEntry(cString in_name, int in_idx, int lower, int upper, int def); // Optional Int Argument (with range limits)
+ bool AddEntry(cString in_name, int in_idx, double def); // Optional Double Argument
+ bool AddEntry(cString in_name, int in_idx, double lower, double upper); // Required Double Argument (with range limits)
+ bool AddEntry(cString in_name, int in_idx, double lower, double upper, double def); // Optional Double (with range limits)
+ bool AddEntry(cString in_name, int in_idx, const cString& def); // Optional String Argument
bool FindEntry(const cString& in_name, tType& ret_type, int& ret_idx) const;
@@ -104,7 +118,10 @@
inline bool GetIntName(int i, cString& name) const;
inline bool GetDoubleName(int i, cString& name) const;
- inline bool GetStringName(int i, cString& name) const;
+ inline bool GetStringName(int i, cString& name) const;
+
+ inline bool ValidateInt(int i, int v) const;
+ inline bool ValidateDouble(int i, double v) const;
};
@@ -177,4 +194,25 @@
return false;
}
+inline bool cArgSchema::ValidateInt(int i, int v) const
+{
+ if (i < m_ints.GetSize() && m_ints[i] &&
+ (!m_ints[i]->has_range_limits || (v <= m_ints[i]->r_u_int && v >= m_ints[i]->r_l_int))) {
+ return true;
+ }
+
+ return false;
+}
+
+inline bool cArgSchema::ValidateDouble(int i, double v) const
+{
+ if (i < m_doubles.GetSize() && m_doubles[i] &&
+ (!m_doubles[i]->has_range_limits || (v <= m_doubles[i]->r_u_double && v >= m_doubles[i]->r_l_double))) {
+ return true;
+ }
+
+ return false;
+}
+
+
#endif
Modified: development/source/tools/tDictionary.h
===================================================================
--- development/source/tools/tDictionary.h 2008-11-26 19:06:02 UTC (rev 2974)
+++ development/source/tools/tDictionary.h 2008-11-26 19:18:09 UTC (rev 2975)
@@ -65,8 +65,8 @@
inline tDictionary(int in_hash_size) : m_hash(in_hash_size) { ; }
// The following methods just call the encapsulated tHashTable
- inline bool OK() { return m_hash.OK(); }
- inline int GetSize() { return m_hash.GetSize(); }
+ inline bool OK() const { return m_hash.OK(); }
+ inline int GetSize() const { return m_hash.GetSize(); }
inline void Add(const cString& name, T data) { m_hash.Add(name, data); }
inline void SetValue(const cString& name, T data) { m_hash.SetValue(name, data); }
inline bool HasEntry(const cString& name) const { return m_hash.HasEntry(name); }
More information about the Avida-cvs
mailing list