Example #1
0
void RunHeapSort()
{
	HeapSort heapSort;

	heapSort.AddData(19);
	heapSort.AddData(8);
	heapSort.AddData(2);
	heapSort.AddData(1);
	heapSort.AddData(24);
	heapSort.AddData(29);
	heapSort.AddData(3);
	heapSort.AddData(23);
	heapSort.AddData(234);
	heapSort.AddData(32);

	heapSort.Sort();

	heapSort.PrintResult();
}
BinaryTreeChromosome* TradingSystem::PerformAnalysis(
	const std::vector<Candlestick>& candlesticks,
	const std::vector<BaseIndicator *>& indicators,
	unsigned populationCount,
	unsigned generationCount,
	unsigned selectionAmount,
	double leafValueMutationProbability,
	double leafSignMutationProbability,
	double logicalNodeMutationProbability,
	double leafIndicatorMutationProbability,
	double crossoverProbability,
	BinaryTreeChromosome* chromosomeToStartWith,
	std::function<void(double fitness, BinaryTreeChromosome * chromosome, int generation)> update
)
{
	srand(static_cast<unsigned int>(time(nullptr)));

	std::vector<IndicatorTuple> dataSet = EvaluateCandlesticks(candlesticks, indicators);

	// Initialization
	std::vector<BinaryTreeChromosome *> front_buffer = std::vector<BinaryTreeChromosome *>();
	std::vector<BinaryTreeChromosome *> back_buffer = std::vector<BinaryTreeChromosome *>();

	BinaryTreeFitness fitness(&(EvaluateFitness), &dataSet);

	BinaryTreeGeneticAlgo selection = BinaryTreeGeneticAlgo(
		selectionAmount,
		leafValueMutationProbability,
		leafSignMutationProbability,
		logicalNodeMutationProbability,
		leafIndicatorMutationProbability,
		crossoverProbability
	);

	for (unsigned y = 0; y < populationCount; y++)
	{
		front_buffer.push_back(new BinaryTreeChromosome());
		back_buffer.push_back(new BinaryTreeChromosome());
	}


	for (unsigned i = 0; i < populationCount; i++)
	{
		front_buffer[i]->GenerateTree(3, indicators);
		back_buffer[i]->GenerateTree(3, indicators);

		front_buffer[i]->setFitness(0);
		back_buffer[i]->setFitness(0);
	}

 	if (chromosomeToStartWith != nullptr)
	{
		for (unsigned i = 0; i < populationCount; i++)
		{
			chromosomeToStartWith->copyTo(front_buffer[i]);
			chromosomeToStartWith->copyTo(back_buffer[i]);

			if (i != 0) {
				front_buffer[i]->Mutate(
					leafValueMutationProbability,
					leafSignMutationProbability,
					logicalNodeMutationProbability,
					crossoverProbability,
					leafIndicatorMutationProbability
				);

				back_buffer[i]->Mutate(
					leafValueMutationProbability,
					leafSignMutationProbability,
					logicalNodeMutationProbability,
					crossoverProbability,
					leafIndicatorMutationProbability
				);
			}
		}
	}


	HeapSort heapSort;

	std::vector<BinaryTreeChromosome*>* p_front_buffer = &front_buffer;
	std::vector<BinaryTreeChromosome*>* p_back_buffer = &back_buffer;
	std::vector<BinaryTreeChromosome*>* tmp2;

	for (unsigned y = 0; y < generationCount; y++)
	{
		fitness.CalculateFitness(p_front_buffer);

		tmp2 = p_front_buffer;
		p_front_buffer = p_back_buffer;
		p_back_buffer = tmp2;

		heapSort.Sort(p_back_buffer, populationCount);

		update(p_back_buffer->at(populationCount - 1)->getFitness(), p_back_buffer->at(populationCount - 1), y + 1 /* Start with 1 */);

		// Selection
		selection.Select(p_front_buffer, p_back_buffer, populationCount);
	}

	fitness.CalculateFitness(p_front_buffer);

	heapSort.Sort(p_front_buffer, populationCount);

	BinaryTreeChromosome* bestFit = new BinaryTreeChromosome(p_front_buffer->at(populationCount - 1));

	for (unsigned i = 0; i < populationCount; i++)
	{
		delete front_buffer[i];
		delete back_buffer[i];
	}

	return bestFit;
} // TradingSystem::PerformAnalysis
Example #3
0
void TestSortingAlgos() {

	try
	{
		BubbleSort testBObj;
		int arrB[ARRAY_SIZE] = { 15,3,12,10,1,9,6,11,5,4 };

		testBObj.LoadData(arrB, ARRAY_SIZE);
		testBObj.Print();
		testBObj.Sort();

		cout << "Bubble Sort Output:";
		testBObj.Print();

		SelectionSort testSObj;
		int arrS[ARRAY_SIZE] = { 15,3,12,10,1,9,6,11,5,4 };

		testSObj.LoadData(arrS, ARRAY_SIZE);
		testSObj.Print();
		testSObj.Sort();

		cout << "Selection Sort Output:";
		testSObj.Print();


		InsertionSort testIObj;
		int arrI[ARRAY_SIZE] = { 15,3,12,10,1,9,6,11,5,4 };

		testIObj.LoadData(arrI, ARRAY_SIZE);
		testIObj.Print();
		testIObj.Sort();

		cout << "Insertion Sort Output:";
		testIObj.Print();


		MergeSort testMObj;

		int arrM[ARRAY_SIZE] = { 15,3,12,10,1,9,6,11,5,4 };

		testMObj.LoadData(arrM, ARRAY_SIZE);
		testMObj.Print();
		testMObj.Sort();

		cout << "Insertion Sort Output:";
		testMObj.Print();


		QuickSort testQObj;

		int arrQ[] = { 15,3,12,10,1,9,6,11,5,4, 12, 8,1, -23, 87,45, 12, 423 };

		testQObj.LoadData(arrQ, sizeof(arrQ)/sizeof(int));
		testQObj.Print();
		testQObj.Sort();

		cout << "Quick Sort Output:";
		testQObj.Print();



		HeapSort testHObj;

		int arrH[] = { 15,3,12,10,1,9,6,11,5,4, 12, 8,1, -23, 87,45, 12, 423 };

		testHObj.LoadData(arrH, sizeof(arrH) / sizeof(int));
		testHObj.Print();
		testHObj.Sort();

		cout << "Heap Sort Output:";
		testHObj.Print();


		QuickSortRandomized  testQRObj;

		int arrQR[] = { 15,3,12,10,1,9,6,11,5,4, 12, 8,1, -23, 87,45, 12, 423 };

		testQRObj.LoadData(arrQR, sizeof(arrQR) / sizeof(int));
		testQRObj.Print();
		testQRObj.Sort();

		cout << "Heap Sort Output:";
		testQRObj.Print();

		HeapSortRevised  testHSRObj;

		int arrHSR[] = { 15,3,12,10,1,9,6,11,5,4, 12, 8,1, -23, 87,45, 12, 423 };

		testHSRObj.LoadData(arrHSR, sizeof(arrHSR) / sizeof(int));
		testHSRObj.Print();
		testHSRObj.Sort();

		cout << "Heap Sort Output:";
		testHSRObj.Print();

	}
	catch (const std::exception& E)
	{
		cerr << "Caught exception \"" << E.what() << "\"\n";
	}
	catch (...)
	{
		cerr << "Caught unknown exception";
	}

}