/** * GA Optimizer Function: * Returns: nothing */ C_INT32 COptMethodEP2::optimise() { NumGeneration = (C_INT32) getValue("EvolutionaryProgram2.Iterations"); PopulationSize = (C_INT32) getValue("EvolutionaryProgram2.PopulationSize"); NumParameter = mpOptProblem->getVariableSize(); /* Create a random number generator */ CRandom::Type Type; Type = (CRandom::Type)(C_INT32) getValue("EvolutionaryProgram2.RandomGenerator.Type"); C_INT32 Seed; Seed = (C_INT32) getValue("EvolutionaryProgram2.RandomGenerator.Seed"); CRandom * pRand = CRandom::createGenerator(Type, Seed); assert(pRand); const double ** Minimum = mpOptProblem->getParameterMin().array(); const double ** Maximum = mpOptProblem->getParameterMax().array(); std::vector< UpdateMethod * > & Parameter = mpOptProblem->getCalculateVariableUpdateMethods(); double current_best_value, la; int i, j, last_update, u10, u30, u50; bool linear; // create array for function value of candidate CandidateValue = new double[2 * PopulationSize]; // create array for function value rate of candidate CandidateValueRate = new double[PopulationSize]; // create array for tournament competition strategy WinScore = new int[2 * PopulationSize]; // create array for crossover points CrossPoint = new int[NumParameter]; // create the population array individual.resize(2 * PopulationSize); // create the individuals for (i = 0; i < 2*PopulationSize; i++) individual[i].resize(NumParameter); // Prepare inital population //Generate the initial population at random for (i = 0; i < PopulationSize; i++) { for (j = 0; j < NumParameter; j++) { try { // determine if linear or log scale linear = FALSE; la = 1.0; if ((*Maximum[j] <= 0.0) || (*Minimum[j] < 0.0)) linear = TRUE; else { la = log10(*Maximum[j]) - log10(std::min(*Minimum[j], std::numeric_limits< C_FLOAT64 >::epsilon())); if (la < 1.8) linear = TRUE; } // set it to a random value within the interval if (linear) individual[i][j] = *Minimum[j] + pRand->getRandomCC() * (*Maximum[j] - *Minimum[j]); else individual[i][j] = *Minimum[j] * pow(10.0, la * pRand->getRandomCC()); } catch (int) { individual[i][j] = (*Maximum[j] - *Minimum[j]) * 0.5 + *Minimum[j]; } } try { // calculate its fitness value for (int kk = 0; kk < NumParameter; kk++) (*Parameter[kk])(individual[i][kk]); CandidateValue[i] = mpOptProblem->calculate(); } catch (int) { CandidateValue[i] = std::numeric_limits< C_FLOAT64 >::max(); } } // initialization ends //Set fitness map rate // double q=0.8; // double constant1=100; double constant1 = 100; // double constant2=20; double constant2 = 10; /* for (i=0; i<PopulationSize; i++) { CandidateValueRate[i]=1-q*pow((1-q),i); } */ // get the index of the fittest BestFoundSoFar = fittest(); // initialise the update registers last_update = 0; u10 = u30 = u50 = 0; // store that value current_best_value = Get_BestFoundSoFar_candidate(); std::ofstream finalout("debugopt.dat"); if (!finalout) { std::cout << "debugopt.dat cannot be opened!" << std::endl; exit(1); } finalout << "----------------------------- the best result at each generation---------------------" << std::endl; finalout << "Generation\t" << "Best candidate value for object function\t" << "Display " << NumParameter << " parameters" << std::endl; finalout << std::endl; finalout.close(); srand(time(NULL)); rand(); // Iterations of GA for (i = 0; i < NumGeneration; i++) { std::cout << std::endl; std::cout << "EP2 is processing at generation " << i << std::endl; TrackDataFile(i); select(5); //Mutating all parents for (int nn = PopulationSize; nn < 2*PopulationSize; nn++) { double mut; // mutate the parameters for (int j = 0; j < NumParameter; j++) { if (floor(10*rand() / RAND_MAX) > 5) { // if(i<NumGeneration*0.5) mut = individual[nn-PopulationSize][j]*(1 + pRand->getRandomCC()); if (i < NumGeneration*0.5) mut = individual[nn - PopulationSize][j] * (1 + sqrt(constant1 * CandidateValueRate[nn] + constant2) * pRand->getRandomCC()); else { if (i < NumGeneration*0.6) mut = individual[nn - PopulationSize][j] * (1 + sqrt(constant1 * CandidateValueRate[nn] + constant2) * 0.5 * pRand->getRandomCC()); else { if (i < NumGeneration*0.7) mut = individual[nn - PopulationSize][j] * (1 + sqrt(constant1 * CandidateValueRate[nn] + constant2) * 0.25 * pRand->getRandomCC()); else { if (i < NumGeneration*0.8) mut = individual[nn - PopulationSize][j] * (1 + sqrt(constant1 * CandidateValueRate[nn] + constant2) * 0.1 * pRand->getRandomCC()); else { if (i < NumGeneration*0.9) mut = individual[nn - PopulationSize][j] * (1 + sqrt(constant1 * CandidateValueRate[nn] + constant2) * 0.01 * pRand->getRandomCC()); else { if (i < NumGeneration*0.95) mut = individual[nn - PopulationSize][j] * (1 + sqrt(constant1 * CandidateValueRate[nn] + constant2) * 0.001 * pRand->getRandomCC()); else mut = individual[nn - PopulationSize][j] * (1 + sqrt(constant1 * CandidateValueRate[nn] + constant2) * 0.0001 * pRand->getRandomCC()); } } } } } } else { if (i < NumGeneration*0.5) mut = individual[nn - PopulationSize][j] * (1 - sqrt(constant1 * CandidateValueRate[nn] + constant2) * pRand->getRandomCC()); else { if (i < NumGeneration*0.6) mut = individual[nn - PopulationSize][j] * (1 - sqrt(constant1 * CandidateValueRate[nn] + constant2) * 0.5 * pRand->getRandomCC()); else { if (i < NumGeneration*0.7) mut = individual[nn - PopulationSize][j] * (1 - sqrt(constant1 * CandidateValueRate[nn] + constant2) * 0.25 * pRand->getRandomCC()); else { if (i < NumGeneration*0.8) mut = individual[nn - PopulationSize][j] * (1 - sqrt(constant1 * CandidateValueRate[nn] + constant2) * 0.1 * pRand->getRandomCC()); else { if (i < NumGeneration*0.9) mut = individual[nn - PopulationSize][j] * (1 - sqrt(constant1 * CandidateValueRate[nn] + constant2) * 0.01 * pRand->getRandomCC()); else { if (i < NumGeneration*0.95) mut = individual[nn - PopulationSize][j] * (1 - sqrt(constant1 * CandidateValueRate[nn] + constant2) * 0.001 * pRand->getRandomCC()); else mut = individual[nn - PopulationSize][j] * (1 - sqrt(constant1 * CandidateValueRate[nn] + constant2) * 0.0001 * pRand->getRandomCC()); } } } } } } // check boundary and force it to be within the bounds if (mut <= *Minimum[j]) mut = *Minimum[j] + std::numeric_limits< C_FLOAT64 >::epsilon(); else { if (mut < *Minimum[j]) mut = *Minimum[j]; } if (mut >= *Maximum[j]) mut = *Maximum[j] - std::numeric_limits< C_FLOAT64 >::epsilon(); else { if (mut > *Maximum[j]) mut = *Maximum[j]; } // store it individual[nn][j] = mut; } // evaluate the fitness for (int kk = 0; kk < NumParameter; kk++) (*Parameter[kk])(individual[nn][kk]); CandidateValue[nn] = mpOptProblem->calculate(); } // select approprate individuals as new population select(2); // get the index of the fittest BestFoundSoFar = fittest(); if (Get_BestFoundSoFar_candidate() != current_best_value) { last_update = i; current_best_value = Get_BestFoundSoFar_candidate(); } if (u10) u10--; if (u30) u30--; if (u50) u50--; if ((u50 == 0) && (i - last_update > 50)) { for (int mm = PopulationSize / 2; mm < PopulationSize; mm++) { for (int jj = 0; jj < NumParameter; jj++) { try { // determine if linear or log scale linear = FALSE; la = 1.0; if ((*Maximum[jj] <= 0.0) || (*Minimum[jj] < 0.0)) linear = TRUE; else { la = log10(*Maximum[jj]) - log10(std::min(*Minimum[jj], std::numeric_limits< C_FLOAT64 >::epsilon())); if (la < 1.8) linear = TRUE; } // set it to a random value within the interval if (linear) individual[mm][jj] = *Minimum[jj] + pRand->getRandomCC() * (*Maximum[jj] - *Minimum[jj]); else individual[mm][jj] = *Minimum[jj] * pow(10.0, la * pRand->getRandomCC()); } catch (int) { individual[mm][jj] = (*Maximum[jj] - *Minimum[jj]) * 0.5 + *Minimum[jj]; } } try { // calculate its fitness for (int kk = 0; kk < NumParameter; kk++) (*Parameter[kk])(individual[mm][kk]); CandidateValue[mm] = mpOptProblem->calculate(); } catch (int) { CandidateValue[mm] = std::numeric_limits< C_FLOAT64 >::max(); } } //end external for loop BestFoundSoFar = fittest(); u50 = 50; u30 = 30; u10 = 10; } else { if ((u30 == 0) && (i - last_update > 30)) { for (int mm = (int)floor(PopulationSize * 0.7); mm < PopulationSize; mm++) { for (int jj = 0; jj < NumParameter; jj++) { try { // determine if linear or log scale linear = FALSE; la = 1.0; if ((*Maximum[jj] <= 0.0) || (*Minimum[jj] < 0.0)) linear = TRUE; else { la = log10(*Maximum[jj]) - log10(std::min(*Minimum[jj], std::numeric_limits< C_FLOAT64 >::epsilon())); if (la < 1.8) linear = TRUE; } // set it to a random value within the interval if (linear) individual[mm][jj] = *Minimum[jj] + pRand->getRandomCC() * (*Maximum[jj] - *Minimum[jj]); else individual[mm][jj] = *Minimum[jj] * pow(10.0, la * pRand->getRandomCC()); } catch (int) { individual[mm][jj] = (*Maximum[jj] - *Minimum[jj]) * 0.5 + *Minimum[jj]; } } try { // calculate its fitness for (int kk = 0; kk < NumParameter; kk++) (*Parameter[kk])(individual[mm][kk]); CandidateValue[mm] = mpOptProblem->calculate(); } catch (int) { CandidateValue[mm] = std::numeric_limits< C_FLOAT64 >::max(); } } //end external for loop BestFoundSoFar = fittest(); u30 = 30; u10 = 10; } else { if ((u10 == 0) && (i - last_update > 10)) { for (int mm = (int) floor(PopulationSize * 0.9); mm < PopulationSize; mm++) { for (int jj = 0; jj < NumParameter; jj++) { try { // determine if linear or log scale linear = FALSE; la = 1.0; if ((*Maximum[jj] <= 0.0) || (*Minimum[jj] < 0.0)) linear = TRUE; else { la = log10(*Maximum[jj]) - log10(std::min(*Minimum[jj], std::numeric_limits< C_FLOAT64 >::epsilon())); if (la < 1.8) linear = TRUE; } // set it to a random value within the interval if (linear) individual[mm][jj] = *Minimum[jj] + pRand->getRandomCC() * (*Maximum[jj] - *Minimum[jj]); else individual[mm][jj] = *Minimum[jj] * pow(10.0, la * pRand->getRandomCC()); } catch (int) { individual[mm][jj] = (*Maximum[jj] - *Minimum[jj]) * 0.5 + *Minimum[jj]; } } try { // calculate its fitness for (int kk = 0; kk < NumParameter; kk++) (*Parameter[kk])(individual[mm][kk]); CandidateValue[mm] = mpOptProblem->calculate(); } catch (int) { CandidateValue[mm] = std::numeric_limits< C_FLOAT64 >::max(); } } //end external for loop BestFoundSoFar = fittest(); u10 = 10; //u10=50; } } } } // end iteration of generations //store the combination of the BestFoundSoFar parameter values found so far mpOptProblem->setSolutionVariables(individual[BestFoundSoFar]); //set the BestFoundSoFar function value mpOptProblem->setSolutionValue(CandidateValue[BestFoundSoFar]); //free memory space delete CrossPoint; delete WinScore; delete CandidateValue; std::cout << std::endl; std::cout << "GA has successfully done!" << std::endl; return 0; }
bool SparseMatrixTest(const size_t & size, const C_FLOAT64 & sparseness, const unsigned C_INT32 & seed, const bool & RMP, const bool & dgemmFlag, const bool & SMP, const bool & CCMP) { size_t i, j, l, loop = 1; CRandom * pRandom = CRandom::createGenerator(CRandom::mt19937, seed); // If the sparseness is not specified we expect 4 metabolites per reaction C_FLOAT64 Sparseness = sparseness; if (Sparseness == 0.0) Sparseness = 4.0 / size; CMatrix< C_FLOAT64 > M(size - 3, size); CSparseMatrix S(size - 3, size); CMatrix< C_FLOAT64 > MM(size, size + 3); CSparseMatrix Ss(size, size + 3); C_FLOAT64 tmp; for (i = 0; i < size - 3; i++) for (j = 0; j < size; j++) { if (pRandom->getRandomCC() < Sparseness) S(i, j) = (pRandom->getRandomCC() - 0.5) * 100.0; } for (i = 0; i < size; i++) for (j = 0; j < size + 3; j++) { if (pRandom->getRandomCC() < Sparseness) Ss(i, j) = (pRandom->getRandomCC() - 0.5) * 100.0; } M = S; MM = Ss; CCompressedColumnFormat C(S); CCompressedColumnFormat CC(Ss); std::cout << "Memory requirements for sparseness:\t" << Sparseness << std::endl; tmp = (C_FLOAT64) sizeof(CMatrix< C_FLOAT64 >) + size * size * sizeof(C_FLOAT64); std::cout << "Matrix(" << size << "x" << size << "):\t" << tmp << std::endl; C_FLOAT64 tmp2 = (C_FLOAT64) sizeof(CSparseMatrix) + 2 * size * sizeof(std::vector<CSparseMatrixElement *>) + 2 * size * sizeof(C_FLOAT64) + S.numNonZeros() * sizeof(CSparseMatrixElement); std::cout << "Sparse(" << size << "x" << size << "):\t" << tmp2 << std::endl; std::cout << "Sparse/Matrix:\t" << tmp2 / tmp << std::endl; tmp2 = (C_FLOAT64) sizeof(CCompressedColumnFormat) + 2 * C.numNonZeros() * sizeof(C_FLOAT64) + (size + 1) * sizeof(C_FLOAT64); std::cout << "CompressedColumnFormat(" << size << "x" << size << "):\t" << tmp2 << std::endl; std::cout << "CompressedColumnFormat/Matrix:\t" << tmp2 / tmp << std::endl << std::endl; CCopasiTimer CPU(CCopasiTimer::PROCESS); CCopasiTimer WALL(CCopasiTimer::WALL); if (RMP) { // Regular Matrix Product CPU.start(); WALL.start(); for (l = 0; l < loop; l++) { CMatrix< C_FLOAT64 > MR(M.numRows(), MM.numCols()); const C_FLOAT64 *pTmp1, *pTmp2, *pTmp4, *pTmp5; const C_FLOAT64 *pEnd1, *pEnd2, *pEnd4; C_FLOAT64 *pTmp3; size_t LDA = M.numCols(); size_t LDB = MM.numCols(); pTmp1 = M.array(); pEnd1 = pTmp1 + M.numRows() * LDA; pEnd2 = MM.array() + LDB; pTmp3 = MR.array(); for (; pTmp1 < pEnd1; pTmp1 += LDA) for (pTmp2 = MM.array(); pTmp2 < pEnd2; pTmp2++, pTmp3++) { *pTmp3 = 0.0; for (pTmp4 = pTmp1, pTmp5 = pTmp2, pEnd4 = pTmp4 + LDA; pTmp4 < pEnd4; pTmp4++, pTmp5 += LDB) * pTmp3 += *pTmp4 **pTmp5; } } CPU.calculateValue(); WALL.calculateValue(); std::cout << "Matrix * Matrix:\t"; CPU.print(&std::cout); std::cout << "\t"; WALL.print(&std::cout); std::cout << std::endl; } if (dgemmFlag) { CPU.start(); WALL.start(); for (l = 0; l < loop; l++) { CMatrix< C_FLOAT64 > dgemmR(M.numRows(), MM.numCols()); char T = 'N'; C_INT m = (C_INT) MM.numCols(); /* LDA, LDC */ C_INT n = (C_INT) M.numRows(); C_INT k = (C_INT) M.numCols(); /* LDB */ C_FLOAT64 Alpha = 1.0; C_FLOAT64 Beta = 0.0; dgemm_(&T, &T, &m, &n, &k, &Alpha, MM.array(), &m, M.array(), &k, &Beta, dgemmR.array(), &m); } /* for (i = 0; i < MR.numRows(); i++) for (j = 0; j < MR.numCols(); j++) assert(fabs(MR(i, j) - dgemmR(i, j)) <= 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon() * fabs(MR(i, j))); */ CPU.calculateValue(); WALL.calculateValue(); std::cout << "dgemm(Matrix, Matrix):\t"; CPU.print(&std::cout); std::cout << "\t"; WALL.print(&std::cout); std::cout << std::endl; } // Sparse Matrix Product if (SMP) { CPU.start(); WALL.start(); for (l = 0; l < loop; l++) { CSparseMatrix SR(S.numRows(), Ss.numCols()); C_FLOAT64 Tmp; std::vector< std::vector< CSparseMatrixElement * > >::const_iterator itRow; std::vector< std::vector< CSparseMatrixElement * > >::const_iterator endRow; std::vector< CSparseMatrixElement * >::const_iterator itRowElement; std::vector< CSparseMatrixElement * >::const_iterator endRowElement; std::vector< std::vector< CSparseMatrixElement * > >::const_iterator itCol; std::vector< std::vector< CSparseMatrixElement * > >::const_iterator endCol; std::vector< CSparseMatrixElement * >::const_iterator itColElement; std::vector< CSparseMatrixElement * >::const_iterator endColElement; for (itRow = S.getRows().begin(), endRow = S.getRows().end(); itRow != endRow; ++itRow) { endRowElement = itRow->end(); for (itCol = Ss.getColumns().begin(), endCol = Ss.getColumns().end(); itCol != endCol; ++itCol) { Tmp = 0; itRowElement = itRow->begin(); itColElement = itCol->begin(); endColElement = itCol->end(); while (itRowElement != endRowElement && itColElement != endColElement) { while (itRowElement != endRowElement && (*itRowElement)->col() < (*itColElement)->row()) ++itRowElement; if (itRowElement == endRowElement) break; while (itColElement != endColElement && (*itColElement)->row() < (*itRowElement)->col()) ++itColElement; if (itColElement == endColElement) break; if ((*itRowElement)->col() != (*itColElement)->row()) continue; Tmp += **itRowElement ***itColElement; ++itRowElement; ++itColElement; } if (fabs(Tmp) < SR.getTreshold()) continue; SR.insert((*itRow->begin())->row(), (*itCol->begin())->col(), Tmp); } } } CPU.calculateValue(); WALL.calculateValue(); std::cout << "Sparse * Sparse:\t"; CPU.print(&std::cout); std::cout << "\t"; WALL.print(&std::cout); std::cout << std::endl; /* for (i = 0; i < MR.numRows(); i++) for (j = 0; j < MR.numCols(); j++) assert(fabs(MR(i, j) - SR(i, j)) < SR.getTreshold()); */ } // Compressed Column Format Product if (CCMP) { CPU.start(); WALL.start(); for (l = 0; l < loop; l++) { CSparseMatrix TmpR(C.numRows(), CC.numCols()); CCompressedColumnFormat CR(C.numRows(), CC.numCols(), 0); C_FLOAT64 Tmp; size_t imax = CR.numRows(); size_t jmax = CR.numCols(); C_FLOAT64 * pColElement, * pEndColElement; size_t * pColElementRow, * pEndColElementRow; size_t * pColStart; CCompressedColumnFormat::const_row_iterator itRowElement; CCompressedColumnFormat::const_row_iterator endRowElement = C.endRow(0); for (j = 0, pColStart = CC.getColumnStart(); j < jmax; j++, pColStart++) { for (i = 0; i < imax; i++) { Tmp = 0; itRowElement = C.beginRow(i); pColElement = CC.getValues() + *pColStart; pEndColElement = CC.getValues() + *(pColStart + 1); pColElementRow = CC.getRowIndex() + *pColStart; pEndColElementRow = CC.getRowIndex() + *(pColStart + 1); while (itRowElement != endRowElement && pColElement != pEndColElement) { while (itRowElement != endRowElement && itRowElement.getColumnIndex() < *pColElementRow) ++itRowElement; if (!(itRowElement != endRowElement)) break; while (pColElement != pEndColElement && *pColElementRow < itRowElement.getColumnIndex()) { ++pColElement; ++pColElementRow; } if (pColElement == pEndColElement) break; if (itRowElement.getColumnIndex() != *pColElementRow) continue; Tmp += *itRowElement **pColElement; ++itRowElement; ++pColElement; ++pColElementRow; } if (fabs(Tmp) < TmpR.getTreshold()) continue; TmpR.insert(i, j, Tmp); } } CR = TmpR; } CPU.calculateValue(); WALL.calculateValue(); std::cout << "Compressed * Compressed:\t"; CPU.print(&std::cout); std::cout << "\t"; WALL.print(&std::cout); std::cout << std::endl; /* for (i = 0; i < MR.numRows(); i++) for (j = 0; j < MR.numCols(); j++) assert(fabs(MR(i, j) - TmpR(i, j)) < SR.getTreshold()); */ } std::cout << std::endl; std::cout << std::endl; return true; }
/** * This function produces a random layout. It first shufles around * metab glyphs and reaction centers, and finally corrects all ars */ void CCopasiSpringLayout::randomize() { CRandom* pRandom = CRandom::createGenerator(CRandom::mt19937, CRandom::getSystemSeed()); size_t i; //compartment glyphs //metab glyphs for (i = 0; i < mpLayout->getListOfMetaboliteGlyphs().size(); ++i) { CLMetabGlyph* pMetabGlyph = &mpLayout->getListOfMetaboliteGlyphs()[i]; const CMetab* pMetab = dynamic_cast<const CMetab*>(pMetabGlyph->getModelObject()); if (!pMetab) continue; //find the compartment glyph const CCompartment* pComp = pMetab->getCompartment(); if (!pComp) continue; const CLCompartmentGlyph* pCompGlyph = NULL; size_t j; for (j = 0; j < mpLayout->getListOfCompartmentGlyphs().size(); ++j) if (mpLayout->getListOfCompartmentGlyphs()[j].getModelObjectKey() == pComp->getKey()) { pCompGlyph = &mpLayout->getListOfCompartmentGlyphs()[j]; break; } if (pCompGlyph) randomlyPlaceGlyphInCompartmentGlyph(pMetabGlyph, pCompGlyph, pRandom); else randomlyPlaceGlyphInDimensions(pMetabGlyph, &mpLayout->getDimensions(), pRandom); } //reaction glyphs for (i = 0; i < mpLayout->getListOfReactionGlyphs().size(); ++i) { CLReactionGlyph* pReactionGlyph = &mpLayout->getListOfReactionGlyphs()[i]; CLPoint center(0, 0); size_t count; for (count = 0; count < pReactionGlyph->getListOfMetabReferenceGlyphs().size(); ++count) { CLMetabGlyph* metabGlyph = pReactionGlyph->getListOfMetabReferenceGlyphs()[count].getMetabGlyph(); if (metabGlyph == NULL) continue; center = center + metabGlyph->getBoundingBox().getCenter(); } center = center * (1.0 / pReactionGlyph->getListOfMetabReferenceGlyphs().size()); center = center + CLPoint(pRandom->getRandomCC() * 20 - 10, pRandom->getRandomCC() * 20 - 10); pReactionGlyph->setPosition(center); /*if (pCompGlyph) randomlyPlaceGlyphInCompartmentGlyph(pMetabGlyph, pCompGlyph); else randomlyPlaceGlyphInDimensions(pMetabGlyph, &mpCurrentLayout->getDimensions());*/ } placeTextGlyphs(mpLayout); delete pRandom; finalizeState(); }