Exemple #1
0
int main (int argc, const char * argv[])
{
	/**
	 * The pole must remain upright within ±r the pole failure angle
	 * The cart must remain within ±h of origin
	 * The controller must always exert a non-zero force F
	 */
	
	/**
	 * The movement of cart and pivot is completely frictionless
	 * The electrical system for response is instantaneous
	 * The wheels of cart do not slip
	 * The motor torque limit is not encountered
	 */

	srand(time(NULL));
	
	printf("Pole Balance\n");

	Generation g = getRandomGeneration();

	printf("\n");
	
	FILE *stream;
	char output_file[254];
	sprintf(output_file, "average_fitness.%i.txt", GENERATION_SIZE);
	stream = fopen(output_file, "wt");
	
	int i;
	for (i = 0; i < GENERATION_COUNT; i++) {
		printf("\rgenerating %0.2f%%", 100 * ((float) i + 1) / (float) GENERATION_COUNT);
		if (i % 5 == 0 && i == 20) {
			Entity best = getBestEntity(&g);
			writeEntity(&best, i, "-best");
			Entity worst = getWorstEntity(&g);
			writeEntity(&worst, i, "-worst");
		}
		fflush(stdout);
		g = getNextGeneration(&g);
		fprintf(stream, "%i\t%f\n", i + 1, getGenerationAverageFitness(&g));
	}
	fclose(stream);

	printf("\nbest solution:\n");
	Entity solution = getBestEntity(&g);
	printEntity(&solution);
	writeEntity(&solution, i, "-best");
	
	Entity worst = getWorstEntity(&g);
	writeEntity(&worst, i, "-worst");
    return 0;
}
Exemple #2
0
 void MapWriter::writeToStream(const Model::Map& map, std::ostream& stream) {
     assert(stream.good());
     stream.unsetf(std::ios::floatfield);
     
     const Model::EntityList& entities = map.entities();
     for (unsigned int i = 0; i < entities.size(); i++)
         writeEntity(*entities[i], stream);
 }
Exemple #3
0
        void MapWriter::writeObjectsToStream(const Model::EntityList& pointEntities, const Model::BrushList& brushes, std::ostream& stream) {
            assert(stream.good());
            stream.unsetf(std::ios::floatfield);

            Model::Entity* worldspawn = NULL;
            
            // group the brushes by their containing entities
            typedef std::map<Model::Entity*, Model::BrushList> EntityBrushMap;
            EntityBrushMap entityToBrushes;
            
            Model::BrushList::const_iterator brushIt, brushEnd;
            for (brushIt = brushes.begin(), brushEnd = brushes.end(); brushIt != brushEnd; ++brushIt) {
                Model::Brush& brush = **brushIt;
                Model::Entity& entity = *brush.entity();
                entityToBrushes[&entity].push_back(&brush);
                if (entity.worldspawn())
                    worldspawn = &entity;
            }
            
            // write worldspawn first
            if (worldspawn != NULL) {
                Model::BrushList& brushList = entityToBrushes[worldspawn];
                writeEntityHeader(*worldspawn, stream);
                for (brushIt = brushList.begin(), brushEnd = brushList.end(); brushIt != brushEnd; ++brushIt) {
                    writeBrush(**brushIt, stream);
                }
                writeEntityFooter(stream);
            }
            
            // now write the point entities
            Model::EntityList::const_iterator entityIt, entityEnd;
            for (entityIt = pointEntities.begin(), entityEnd = pointEntities.end(); entityIt != entityEnd; ++entityIt) {
                Model::Entity& entity = **entityIt;
                writeEntity(entity, stream);
            }

            // finally write the brush entities
            EntityBrushMap::iterator it, end;
            for (it = entityToBrushes.begin(), end = entityToBrushes.end(); it != end; ++it) {
                Model::Entity* entity = it->first;
                if (entity != worldspawn) {
                    Model::BrushList& brushList = it->second;
                    writeEntityHeader(*entity, stream);
                    for (brushIt = brushList.begin(), brushEnd = brushList.end(); brushIt != brushEnd; ++brushIt) {
                        writeBrush(**brushIt, stream);
                    }
                    writeEntityFooter(stream);
                }
            }
        }
void LC_MakerCamSVG::writeEntities(RS_Document* document, RS_Layer* layer) {

    RS_DEBUG->print("RS_MakerCamSVG::writeEntitiesFromBlock: Writing entities from layer ...");

	for(auto e: *document){

        if (e->getLayer() == layer) {

            if (!(e->getFlag(RS2::FlagUndone))) {

                writeEntity(e);
            }
        }
    }
}
void LC_MakerCamSVG::writeEntities(RS_Document* document, RS_Layer* layer) {

    RS_DEBUG->print("RS_MakerCamSVG::writeEntitiesFromBlock: Writing entities from layer ...");

    for (RS_Entity* e = document->firstEntity(RS2::ResolveNone); e != NULL; e = document->nextEntity(RS2::ResolveNone)) {

        if (e->getLayer() == layer) {

            if (!(e->getFlag(RS2::FlagUndone))) {

                writeEntity(e);
            }
        }
    }
}
Exemple #6
0
void writeEntities(FILE* f)
{
	int i;
	u16 cnt=0;
	size_t pos=ftell(f);
	fwrite(&cnt,sizeof(u16),1,f);
	int k=0;for(i=0;i<NUMENTITIES;i++)if(entity[i].used && entity[i].type)entity[i].writeID=k++;
	for(i=0;i<NUMENTITIES;i++)
	{
		if(writeEntity(&entity[i], f))cnt++;
	}
	fseek(f, pos, SEEK_SET);
	fwrite(&cnt,sizeof(u16),1,f);
	fseek(f, 0, SEEK_END);
}
void EditorScreen::buttonClicked(int button)
{
  int entity = CalculatorState::instance().printableEntityByButton(button);

  if (entity >= 0) // Printable entity
    writeEntity(entity);
  else if (cursorVisible())
    switch (button)
    {
    case Button_Up: moveUp(); break;
    case Button_Down: moveDown(); break;
    case Button_Left: moveLeft(); break;
    case Button_Right: moveRight(); break;
    case Button_Del: deleteString(); break;
    case Button_Ins: insertClicked(); break;
    default:;
    }
}
Exemple #8
0
        void MapWriter::writeToFileAtPath(Model::Map& map, const String& path, bool overwrite) {
            FileManager fileManager;
            if (fileManager.exists(path) && !overwrite)
                return;
            
            const String directoryPath = fileManager.deleteLastPathComponent(path);
            if (!fileManager.exists(directoryPath))
                fileManager.makeDirectory(directoryPath);
            
            FILE* stream = fopen(path.c_str(), "w");
            if (stream == NULL)
                throw IOException::openError(path);
            // std::fstream stream(path.c_str(), std::ios::out | std::ios::trunc);

            size_t lineNumber = 1;
            const Model::EntityList& entities = map.entities();
            for (unsigned int i = 0; i < entities.size(); i++)
                lineNumber += writeEntity(*entities[i], lineNumber, stream);
            fclose(stream);
        }