Ejemplo n.º 1
0
void CController::ResetEnvironment(){

	m_vecSweepers.clear();
	m_vecObjects.clear();

	m_NumSweepers = CParams::iNumSweepers;
	m_NumSuperMines = CParams::iNumSuperMines;

	//let's create the mine sweepers
	for (int i=0; i<m_NumSweepers; ++i)
	{
		m_vecSweepers.push_back(CMinesweeper());
	}

	//initialize super mines in random positions within the application window
	// such that they aren't spawned on a sweeper
	for (int i=0; i<m_NumSuperMines; ++i)
	{
		SVector2D position;
		// don't spawn on sweepers. keep finding another location if they do.
		boolean collision = false;
		do{
			position = SVector2D(RandFloat() * cxClient, RandFloat() * cyClient);
			collision = false;
			for(int i=0; i < m_vecSweepers.size(); i++){
				if(Vec2DLengthSquared(position-m_vecSweepers[i].Position()) <= mineSpawnThreshold){
					collision = true;
					break;
				}
			}
		}while(collision);
		m_vecObjects.push_back(CCollisionObject(CCollisionObject::SuperMine, position));
	}

	// Spawn mines
	for (int i=0; i<m_NumMines; ++i)
	{
		SVector2D position;
		// don't spawn on super mines.
		boolean collision = false;
		do{
			position = SVector2D(RandFloat() * cxClient, RandFloat() * cyClient);
			collision = false;
			for(int i=0; i < m_vecObjects.size(); i++){
				if(m_vecObjects[i].getType() == CCollisionObject::SuperMine){
					if(Vec2DLengthSquared(position-m_vecObjects[i].getPosition()) <= mineSpawnThreshold){
						collision = true;
						break;
					}
				}
			}
		}while(collision);
		m_vecObjects.push_back(CCollisionObject(CCollisionObject::Mine, position));
	}
}
Ejemplo n.º 2
0
// Initialize the sweepers, their brains and the GA factory.
SceneController::SceneController(int width, int height, QObject *parent) :
		QObject(parent),
		gs(new QGraphicsScene(0, 0, width, height)),
		gsInfo(new QGraphicsSimpleTextItem),
		gsDefaultPen(),
		gsElitePen(Qt::red),
		gsMinePen(Qt::green),
		vpWidth(width),
		vpHeight(height),
		m_pGA(nullptr),
		m_bInternalError(false),
		m_iTicks(0),
		m_iGenerations(0) {

	// MSVC does not support the std::initializer_list, so this is the only way
	// to create our object templates and be platform independent...
	for (unsigned int i = 0; i < sizeof(objectMinePoints) / sizeof(*objectMinePoints); ++i)
		objectMine.append(objectMinePoints[i]);
	for (unsigned int i = 0; i < sizeof(objectSweeperPoints) / sizeof(*objectSweeperPoints); ++i)
		objectSweeper.append(objectSweeperPoints[i]);

	// compatibility mode with Qt5
	gsDefaultPen.setCosmetic(true);
	gsElitePen.setCosmetic(true);
	gsMinePen.setCosmetic(true);

	gs->addItem(gsInfo);

	// let's create the minesweepers
	for (int i = MainWindow::s.iNumSweepers; i; --i)
		vecSweepers.push_back(CMinesweeper(RandFloat() * vpWidth,
					RandFloat() * vpHeight, RandFloat() * 2 * M_PI));

	// and initial population of mines
	updateMineObjects(MainWindow::s.iNumMines);

	// get the total number of weights used in the sweepers
	// NN so we can initialize the GA
	int m_NumWeightsInNN = vecSweepers[0].GetNumberOfWeights();

	// initialize the Genetic Algorithm class
	delete m_pGA;
	m_pGA = new CGenAlg(vecSweepers.size(), m_NumWeightsInNN);

	// get the weights from the GA and insert into the sweepers brains
	vecThePopulation = QVector<SGenome>::fromStdVector(m_pGA->GetChromos());

	for (int i = 0; i < vecThePopulation.size(); i++)
		vecSweepers[i].PutWeights(vecThePopulation[i].vecWeights);

}
Ejemplo n.º 3
0
//---------------------------------------constructor---------------------
//
//	initilaize the sweepers, their brains and the GA factory
//
//-----------------------------------------------------------------------
CController::CController(HWND hwndMain,
	int  cxClient,
	int  cyClient): m_NumSweepers(CParams::iPopSize), 
	m_bFastRender(false),
	m_bRenderBest(false),
	m_iTicks(0),
	m_iTrials(0),
	m_hwndMain(hwndMain),
	m_hwndInfo(NULL),
	m_iGenerations(0),
	m_cxClient(cxClient),
	m_cyClient(cyClient),
	joeEverChangedTimes(0),
	m_iViewThisSweeper(0)
{
	int i = 0;
	/*for (i = 0; i < NumObjectVerts; i++) {
		objects[i] = SPoint(objects[i].x * 2, objects[i].y * 2);
	}*/
	//let's create the mine sweepers
	for (i=0; i<m_NumSweepers; ++i)
	{
		m_vecSweepers.push_back(CMinesweeper());
	}

	//and the vector of sweepers which will hold the best performing sweeprs
	for (i=0; i<CParams::iNumBestSweepers; ++i)
	{
		m_vecBestSweepers.push_back(CMinesweeper());
	}

	UpdateReverseTrial();
	joeEverChanged = false;
	joeChanged = false;
	m_pPop = new Cga(CParams::iPopSize,
		CParams::iNumInputs,
		CParams::iNumOutputs);

	//create the phenotypes
	vector<CNeuralNet*> pBrains = m_pPop->CreatePhenotypes();

	//assign the phenotypes
	for (i=0; i<m_NumSweepers; i++)
	{   
		m_vecSweepers[i].InsertNewBrain(pBrains[i]);
	}

	//create a pen for the graph drawing
	m_BluePen        = CreatePen(PS_SOLID, 1, RGB(0, 255, 255));
	m_RedPen         = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
	m_GreenPen       = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
	m_GreyPenDotted  = CreatePen(PS_DOT, 1, RGB(100, 100, 100));
	m_RedPenDotted   = CreatePen(PS_DOT, 1, RGB(200, 0, 0));
	m_BlackPen		 = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));

	m_OldPen	= NULL;

	//and the brushes
	m_BlueBrush = CreateSolidBrush(RGB(0,244,244));
	m_RedBrush  = CreateSolidBrush(RGB(150,0,0));

	highBrush = CreateSolidBrush(RGB(221, 255, 209));
	lowBrush = CreateSolidBrush(RGB(255, 214, 216));

	//fill the vertex buffers
	for (i=0; i<NumSweeperVerts; ++i)
	{
		m_SweeperVB.push_back(sweeper[i]);
	}

	for (i=0; i<NumObjectVerts; ++i)
	{
		m_ObjectsVB.push_back(objects[i]);
	}
}