Esempio n. 1
0
void treeWrapper() {
    Trees treeObj;
    treeObj.addSerialNumToTree(10);
    //treeObj.addRandomIntTree(100);
    cout << "Total Nodes:" << treeObj.countAllNodes() << endl;
    treeObj.deleteTree();
    cout << "Total Nodes after Delete:" << treeObj.countAllNodes() << endl;
    //treeObj.printTree(PREORDER);
}
// Public methods.
void RandomForestBuilder::build() {
  common::debug::print("Creating %d random tree(s)\n", classificatorParameters.RandomTreesCount);

  Trees trainedRandomTrees;
  trainedRandomTrees.reserve(classificatorParameters.RandomTreesCount);

  for (int i = 0; i < classificatorParameters.RandomTreesCount; ++i) {
    std::vector<int> trainingSet;
    std::vector<int> outOfBagSet;

    generateBootStrap(trainingSet, outOfBagSet);

    RandomTreeBuilder treeBuilder(featuresCollection, trainingSet, classificatorParameters);
    treeBuilder.build();

    trainedRandomTrees.push_back(treeBuilder.getTreeStructure());
    common::debug::print("[%d] Trained Random Tree...\n", i + 1);
  }

  cleanUp();
  forest = new RandomForest(trainedRandomTrees, featuresCollection.size());
}
Esempio n. 3
0
bool compare(const Trees &lhs, const Trees &rhs)
{
  return lhs.to_string() < rhs.to_string();
}
Esempio n. 4
0
void Editor_controller::Handle_event(const Ustring& event_handle, const Event& event) {
	Layout& layout = layout_controller->Get_layout();
	if(event_handle == "save") {
		ALLEGRO_FILECHOOSER* fc = al_create_native_file_dialog(
			layout.Get_filename().Cstring(),
			"Save",
			"*",
			ALLEGRO_FILECHOOSER_SAVE);
		al_show_native_file_dialog(NULL, fc);
		if(al_get_native_file_dialog_count(fc) > 0) {
			const char *filename = al_get_native_file_dialog_path(fc, 0);
			layout.Set_filename(filename);

			bool s = layout.Save_yaml();
			std::cout<<(s?"Saved":"Save failed")<<std::endl;
		}
		al_destroy_native_file_dialog(fc);
	}
	if(event_handle == "load") {
		ALLEGRO_FILECHOOSER* fc = al_create_native_file_dialog(
			layout.Get_filename().Cstring(),
			"Load",
			"*",
			ALLEGRO_FILECHOOSER_FILE_MUST_EXIST);
		al_show_native_file_dialog(NULL, fc);
		if(al_get_native_file_dialog_count(fc) > 0) {
			const char *filename = al_get_native_file_dialog_path(fc, 0);
			layout.Set_filename(filename);

			layout_controller->Clear();
			if(layout.Load_yaml())
			{
				std::cout<<"Loaded"<<std::endl;
				const Name_to_widget& layout_widgets = layout.Get_widgets();

				layout_controller->Set_root(layout.Get_root());
				layout_controller->Get_root()->Set_size(Vector2(al_get_display_width(layout_display), al_get_display_height(layout_display)));
				layout_controller->Set_tree(layout_controller->Get_root_tree(), layout.Get_root());

				typedef std::stack<Tree*> Trees_todo;
				Trees_todo trees_todo;
				trees_todo.push(layout_controller->Get_root_tree());
				
				while(!trees_todo.empty())
				{
					Tree* current_tree = trees_todo.top();
					trees_todo.pop();

					Container* parent = dynamic_cast<Container*>(layout_controller->Get_widget(current_tree));

					if(parent)
					{
						Widgets children = parent->Get_children();
						for(Widgets::iterator i = children.begin(); i != children.end(); ++i)
						{
							Tree* tree_child = layout_controller->Get_skin().Clone<Tree>("tree");
							tree_child->Set_text((*i)->Get_name());
							layout_controller->Set_tree(tree_child, *i);
							current_tree->Add_child(tree_child);
							events[Event(tree_child, "selected")] = "select";
							trees_todo.push(tree_child);
						}
					}
				}
			}
			else
			{
				layout_controller->Set_root(layout_controller->Get_skin().Clone<Desktop>("desktop"));
				layout_controller->Get_root()->Set_position(Vector2(0, 0));
				layout_controller->Get_root()->Set_size(Vector2(al_get_display_width(layout_display), al_get_display_height(layout_display)));
				
				layout.Add_widget("root", layout_controller->Get_root(), NULL);
				layout_controller->Set_tree(layout_controller->Get_root_tree(), layout_controller->Get_root());
			}
			layout_controller->Select_tree(NULL);
		}
		al_destroy_native_file_dialog(fc);
	}
	if(event_handle == "remove") {
		Tree* parent = dynamic_cast<Tree*>(layout_controller->Get_current_tree()->Get_parent());
		if(parent)
		{
			dynamic_cast<Container*>(layout_controller->Get_widget(parent))->Remove_child(layout_controller->Get_current_widget());

			parent->Remove_child(layout_controller->Get_current_tree());
			Trees deadlist;
			deadlist.push_back(layout_controller->Get_current_tree());
			layout_controller->Select_tree(NULL);
			int count = 0;
			while(!deadlist.empty())
			{
				Tree* current = deadlist.back();
				deadlist.erase(--deadlist.end());
				Trees& children = current->Get_children();
				for(Trees::iterator i = children.begin(); i != children.end(); ++i)
				{
					deadlist.push_back(*i);
				}
				layout.Remove_widget(layout_controller->Get_widget(current));
				layout_controller->Destroy_widget(current);
				++count;
			}
			layout_controller->Select_tree(parent);
			parent->Select();
		}
	}
	if(event_handle == "create") {
		std::cout<<"Create"<<std::endl;
		if(layout_controller->Get_current_tree())
		{
			std::cout<<"Current tree"<<std::endl;
			Menu* create_menu = dynamic_cast<Menu*>(controller_layout.Get_widget("create menu"));
			int option = create_menu->Get_selected_option();
			if(option != -1){
				Widget* child = layout_controller->Get_skin().Clone<Widget>(create_menu->Get_option(option));
				Container* parent = dynamic_cast<Container*>(layout_controller->Get_current_widget());
				std::cout<<"parent: "<<parent<<std::endl;

				if(parent && parent->Add_child(child))
				{
					Ustring name = layout.Add_widget(create_menu->Get_option(option), child, parent);
					Tree* tree_child = layout_controller->Get_skin().Clone<Tree>("tree");
					tree_child->Set_text(name);
					layout_controller->Get_current_tree()->Add_child(tree_child);
					layout_controller->Set_tree(tree_child, child);
					layout_controller->Get_current_tree()->Open();
					Text_interface* has_text = dynamic_cast<Text_interface*>(child);
					if(has_text)
						has_text->Set_text(name);
					events[Event(tree_child, "selected")] = "select";
				}
				else
					delete child;
			}
		}
	}

	if(event_handle == "select")
	{
		Tree* newsel = dynamic_cast<Tree*>(event.source);
		if(newsel)
		{
			Widget* tw = layout_controller->Get_widget(newsel);
			if(tw)
			{
				Container* attributes_vbox = dynamic_cast<Container*>(controller_layout.Get_widget("attribute vbox"));
				layout_controller->Select_tree(newsel);
				Widgets widgets = attributes_vbox->Get_children();
				for(Widgets::iterator i = widgets.begin(); i != widgets.end(); ++i) {
					attributes_vbox->Remove_child(*i);
				}
				attributes_vbox->Add_child(attribute_controllers["widget"]->Get_root());
				attribute_controllers["widget"]->Synchronize_values();
				Attribute_controllers::iterator ac = attribute_controllers.find(tw->Get_prototype_name().Cstring());
				if(ac != attribute_controllers.end()) {
					attributes_vbox->Add_child(ac->second->Get_root());
					ac->second->Synchronize_values();
				}
			}
		}
	}
	if(event_handle == "move up") {
		Tree* currenttree = dynamic_cast<Tree*>(layout_controller->Get_current_tree());
		Tree* parenttree = dynamic_cast<Tree*>(currenttree->Get_parent());
		if(currenttree && parenttree)
		{
			Widget* widget = layout_controller->Get_current_widget();
			Container* parent = dynamic_cast<Container*>(widget->Get_parent());
			Widgets children = parent->Get_children();
			Widgets::iterator i = std::find(children.begin(), children.end(), widget);
			Trees treechildren = parenttree->Get_children();
			Trees::iterator itree = std::find(treechildren.begin(), treechildren.end(), currenttree);
			if(i != children.begin()) {
				i = children.erase(i);
				itree = treechildren.erase(itree);
				if(i != children.begin()){
					--i;
					--itree;
				}
				children.insert(i, widget);
				treechildren.insert(itree, currenttree);
				for(Widgets::iterator i = children.begin(); i != children.end(); ++i) {
					parent->Remove_child(*i);
				}
				for(Trees::iterator i = treechildren.begin(); i != treechildren.end(); ++i) {
					parenttree->Remove_child(*i);
				}
				for(Widgets::iterator i = children.begin(); i != children.end(); ++i) {
					parent->Add_child(*i);
				}
				for(Trees::iterator i = treechildren.begin(); i != treechildren.end(); ++i) {
					parenttree->Add_child(*i);
				}
			}
		}
	}
	if(event_handle == "move down") {
		Tree* currenttree = dynamic_cast<Tree*>(layout_controller->Get_current_tree());
		Tree* parenttree = dynamic_cast<Tree*>(currenttree->Get_parent());
		if(currenttree && parenttree)
		{
			Widget* widget = layout_controller->Get_current_widget();
			Container* parent = dynamic_cast<Container*>(widget->Get_parent());
			Widgets children = parent->Get_children();
			Widgets::iterator i = std::find(children.begin(), children.end(), widget);
			Trees treechildren = parenttree->Get_children();
			Trees::iterator itree = std::find(treechildren.begin(), treechildren.end(), currenttree);
			if(i+1 != children.end()) {
				i = children.erase(i);
				itree = treechildren.erase(itree);
				++i;
				++itree;
				children.insert(i, widget);
				treechildren.insert(itree, currenttree);
				for(Widgets::iterator i = children.begin(); i != children.end(); ++i) {
					parent->Remove_child(*i);
				}
				for(Trees::iterator i = treechildren.begin(); i != treechildren.end(); ++i) {
					parenttree->Remove_child(*i);
				}
				for(Widgets::iterator i = children.begin(); i != children.end(); ++i) {
					parent->Add_child(*i);
				}
				for(Trees::iterator i = treechildren.begin(); i != treechildren.end(); ++i) {
					parenttree->Add_child(*i);
				}
			}
		}
	}
	if(event_handle == "cut") {
		std::cout<<"Cut"<<std::endl;
		Tree* currenttree = dynamic_cast<Tree*>(layout_controller->Get_current_tree());
		Tree* parenttree = dynamic_cast<Tree*>(currenttree->Get_parent());
		if(currenttree && parenttree)
		{
			Widget* widget = layout_controller->Get_current_widget();
			Container* parent = dynamic_cast<Container*>(widget->Get_parent());
			
			parent->Remove_child(widget);
			parenttree->Remove_child(currenttree);

			layout_controller->Select_tree(NULL);
			layout_controller->Select_tree(parenttree);
			parenttree->Select();

			if(cut_widget) {
				Trees deadlist;
				deadlist.push_back(cut_tree);
				int count = 0;
				while(!deadlist.empty())
				{
					Tree* current = deadlist.back();
					deadlist.erase(--deadlist.end());
					Trees& children = current->Get_children();
					for(Trees::iterator i = children.begin(); i != children.end(); ++i)
					{
						deadlist.push_back(*i);
					}
					layout.Remove_widget(layout_controller->Get_widget(current));
					layout_controller->Destroy_widget(current);
					++count;
				}
				std::cout<<"Deleted "<<count<<" widgets in cut buffer"<<std::endl;
			}
			
			cut_widget = widget;
			cut_tree = currenttree;
		}
	}
	if(event_handle == "paste") {
		Container* parent = dynamic_cast<Container*>(layout_controller->Get_current_widget());
		if(cut_widget && parent)
		{
			Tree* currenttree = dynamic_cast<Tree*>(layout_controller->Get_current_tree());
			parent->Add_child(cut_widget);
			currenttree->Add_child(cut_tree);
			layout_controller->Select_tree(cut_tree);
			cut_tree->Select();
			cut_widget = NULL;
			cut_tree = NULL;
		}
	}
}
Esempio n. 5
0
int main(int argc, const char * argv[]) {
    string pattern = ";";           //";" as spliter
    string data[1024][3];           //Store the valid data and being sorted
    int n = 0;                      //The nth valid line
    
    //Read files according to the names input on the command line
    for (int f = 1; f<argc; f++) {
        vector<string> result;
        //string fileName = argv[f];
        ifstream in(argv[f]);
        string line;
        
    if(in){
        while(getline(in,line)){
            
            // Check if each line of data can be splitted into 3 parts
            char s[1024];
            line.copy(s, line.length());
            int splitCheck = 0;
            for (int snum = 0; snum < line.length(); snum++) {
                if (s[snum] == ';') splitCheck++;
            }
            
            if (splitCheck == 2) {
                result = split(line, pattern); //Split current line of data by ";"
                
                // Check the empty item
                int checkFormat = 0;
                if ((result[0] == "") && (result[1] == "") && (result[2] == "")) {
                    checkFormat++;
                }
                
                // Check the duplicate item
                int checkSame = 0;
                for (int check = 0; check < n ; check++) {
                    if (data[check][0] == result[0] && data[check][1] == result[1] && data[check][1] == result[1]) {
                        checkSame++;
                    }
                }
                
                // Check if the input age is a number (Check data type)
                int checkInt = 0;
                string age = result[1];
                for (int ageNum = 0 ; ageNum<age.length(); ageNum++) {
                    if (isdigit(result[1][ageNum])){
                        checkInt++;
                    }
                }
                
                // Check if the input age is in the normal range
                int checkAge = 0;
                int a = atoi(age.c_str());
                if (a>=0 && a <= 200) {
                    checkAge++;
                }
                
                // If passing all the checking processes, store the current line to the data array
                if (checkFormat == 0 && checkSame == 0 && checkInt > 0 && checkAge > 0) {
                    for (int i = 0; i<3; i++) {
                        data[n][i] = result[i];
                    }
                    n++;    // Ready for the next item
                }
            }
        }
        in.close();//File closed
    }
    else{
        cout<<"no such file"<<endl;
    }
    }
    
   
    Trees tr;        // Initialize a Tree
    
    // Put the stored data into the Tree for sorting
    for (int i = 0; i<n; i++) {
        int age = atoi(data[i][1].c_str());
        string code = data[i][0];
        string name = data[i][2];
        tr.create(age,code,name);
    }
    cout<<" code   age    name"<<endl;
    tr.disp();      // Output the results
    cout<<endl;
    return 0;
}