00001 #include <ECF/ECF.h> 00002 #include <vcclr.h> 00003 #include <msclr\marshal_cppstd.h> 00004 using namespace Tree::Primitives; 00005 using namespace SpaceFlight::Simulation; 00006 using namespace msclr::interop; 00007 00008 class SpaceFlightEvalOp : public EvaluateOp 00009 { 00010 public: 00011 void registerParameters(StateP state) 00012 { 00013 state->getRegistry()->registerEntry("testcases.dir", static_cast<voidP>(new std::string), ECF::STRING); 00014 } 00015 00016 bool initialize(StateP state) 00017 { 00018 if (!state->getRegistry()->isModified("testcases.dir")) 00019 { 00020 return false; 00021 } 00022 try 00023 { 00024 std::string *dir = static_cast<std::string *>(state->getRegistry()->getEntry("testcases.dir").get()); 00025 evaluator = gcnew Evaluator(marshal_as<System::String ^>(*dir)); 00026 return true; 00027 } 00028 catch (System::Exception ^e) 00029 { 00030 printf("Error: %s\n", marshal_as<std::string>(e->Message).c_str()); 00031 return false; 00032 } 00033 } 00034 00035 FitnessP evaluate(IndividualP individual) 00036 { 00037 TreeP tree = boost::dynamic_pointer_cast<Tree::Tree>(individual->getGenotype()); 00038 std::string nodes; 00039 nodes.reserve(1500); 00040 for (unsigned i = 0; i < tree->size(); ++i) 00041 { 00042 nodes += (*tree)[i]->primitive_->getName(); 00043 nodes += ' '; 00044 } 00045 00046 AiShipController ^controller = gcnew AiShipController(marshal_as<System::String ^>(nodes)); 00047 FitnessP fitness = static_cast<FitnessP>(new FitnessMax); 00048 fitness->setValue(evaluator->Evaluate(controller) * 1000); 00049 return fitness; 00050 } 00051 00052 private: 00053 gcroot<Evaluator ^> evaluator; 00054 }; 00055 00056 class IfLessThen : public Primitive 00057 { 00058 public: 00059 IfLessThen() 00060 { 00061 nArguments_ = 4; 00062 name_ = "iflt"; 00063 } 00064 00065 void execute(void *result, Tree::Tree &tree) 00066 { 00067 } 00068 }; 00069 00070 int main(int argc, char **argv) 00071 { 00072 TreeP tree = static_cast<TreeP>(new Tree::Tree); 00073 tree->addFunction(static_cast<PrimitiveP>(new IfLessThen)); 00074 00075 StateP state = static_cast<StateP>(new State); 00076 state->addGenotype(tree); 00077 state->setEvalOp(static_cast<EvaluateOpP>(new SpaceFlightEvalOp)); 00078 state->initialize(argc, argv); 00079 state->run(); 00080 return 0; 00081 }