void GenerationWidget::paintEvent(QPaintEvent*) { QPainter p(this); p.fillRect(rect(), Qt::black); if (mImage.isNull() || qFuzzyIsNull(mImageAspectRatio) || qFuzzyIsNull(mImageAspectRatio)) return; if (mWindowAspectRatio < mImageAspectRatio) { const int h = int(width() / mImageAspectRatio); mDestRect = QRect(0, (height()-h)/2, width(), h); } else { const int w = int(height() * mImageAspectRatio); mDestRect = QRect((width()-w)/2, 0, w, height()); } p.drawImage(mDestRect, mImage); const qreal invScale = 1 / qSqrt(mDestRect.width() * mDestRect.height()); p.translate(mDestRect.x(), mDestRect.y()); p.scale(mDestRect.width(), mDestRect.height()); p.setBrush(Qt::transparent); p.setRenderHint(QPainter::Antialiasing); if (mSplicedGene.color().isValid() && !mSplices.empty()) { p.setPen(QPen(Qt::green, 1.2 * invScale)); for (QVector<Gene>::const_iterator g = mSplices.constBegin(); g != mSplices.constEnd(); ++g) p.drawPolygon(g->polygon()); p.setPen(QPen(Qt::red, 1.3 * invScale)); p.drawPolygon(mSplicedGene.polygon()); mSplicedGene = Gene(); mSplices.clear(); } if (!mHighlighted.isEmpty()) { p.setPen(QPen(QColor(255, 0, 200), 1.3 * invScale)); p.setBrush(Qt::transparent); p.drawPolygon(mHighlighted); } }
vector<Gene> crossOver(Gene p1,Gene p2,vector<vector<int> > &V1,vector<vector<int> > &V2) { vector<Gene> ret; ret.push_back(Gene(p1.n,p1.m)); ret.push_back(Gene(p2.n,p2.m)); vector<int> samePointsPosition[2]; for ( int i = 0 ; i < p1.m ; i++ ) if ( isSameVantagePointsInGene(p1,p2,i) ) samePointsPosition[0].push_back(i); if ( ~noCut1 ) samePointsPosition[0].push_back(noCut1); for ( int i = 0 ; i < p2.m ; i++ ) if ( isSameVantagePointsInGene(p2,p1,i) ) samePointsPosition[1].push_back(i); if ( ~noCut2 ) samePointsPosition[1].push_back(noCut2); int pos = 0; for ( int i = 0 ; i < V1[0].size() ; i++,pos++ ) for ( int j = 0 ; j < p1.n ; j++ ) ret[0].g[pos][j] = p1.g[V1[0][i]][j]; for ( int i = 0 ; i < V2[1].size() ; i++,pos++ ) for ( int j = 0 ; j < p2.n ; j++ ) ret[0].g[pos][j] = p2.g[V2[1][i]][j]; for ( int i = 0 ; i < samePointsPosition[0].size() ; i++,pos++ ) for ( int j = 0 ; j < p1.n ; j++ ) ret[0].g[pos][j] = p1.g[samePointsPosition[0][i]][j]; pos = 0; for ( int i = 0 ; i < V1[1].size() ; i++,pos++ ) for ( int j = 0 ; j < p1.n ; j++ ) ret[1].g[pos][j] = p1.g[V1[1][i]][j]; for ( int i = 0 ; i < V2[0].size() ; i++,pos++ ) for ( int j = 0 ; j < p2.n ; j++ ) ret[1].g[pos][j] = p2.g[V2[0][i]][j]; for ( int i = 0 ; i < samePointsPosition[1].size() ; i++,pos++ ) for ( int j = 0 ; j < p2.n ; j++ ) ret[1].g[pos][j] = p2.g[samePointsPosition[1][i]][j]; for ( int i = 0 ; i < 2 ; i++ ) samePointsPosition[i].clear(); return ret; }
void SVGReader::readPath(void) { Q_ASSERT(mXml.isStartElement() && mXml.name() == "path"); QColor color; int pos; bool ok = false; // <path style="fill-opacity:0.230442;fill:rgb(14,9,206)" d="M 0.845225 0.845225 L 0.431106 0.585496 L 0.0788198 0.4925 L 0.0861273 0.692974 Z" /> const QString& style = mXml.attributes().value("style").toString(); color = getRGB(QRegExp("fill\\s*:\\s*rgb\\((\\d+),\\s*(\\d+),\\s*(\\d+)\\)"), style, mXml, &ok); if (!color.isValid()) { // fallback to v0.4 format const QString& fill = mXml.attributes().value("fill").toString(); color = getRGB(QRegExp("(\\d+),\\s*(\\d+),\\s*(\\d+)"), fill, mXml, &ok); } if (!ok) { mXml.raiseError(QObject::tr("fill not found or invalid")); return; } QRegExp fo_re("fill-opacity\\s*:\\s*([-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?)"); pos = fo_re.indexIn(style); qreal alpha = (-1 != pos)? fo_re.capturedTexts().at(1).toDouble(&ok) : color.alphaF(); if (!ok) { mXml.raiseError(QObject::tr("fill-opacity (%1): not found or invalid").arg(fo_re.capturedTexts().at(1))); return; } color.setAlphaF(alpha); QPolygonF polygon; // ... 0.0788198 0.4925 ... 3.687e-4 -0.91112 ... QRegExp coords_re("([-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?)\\s+([-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?)"); QString d = mXml.attributes().value("d").toString(); while ((pos = coords_re.indexIn(d)) != -1) { const QStringList& xy = coords_re.capturedTexts(); const qreal x = xy.at(1).toDouble(&ok); if (!ok) { mXml.raiseError(QObject::tr("invalid x coordinate in \"%1\"").arg(xy.at(0))); return; } const qreal y = xy.at(3).toDouble(&ok); if (!ok) { mXml.raiseError(QObject::tr("invalid y coordinate in \"%1\"").arg(xy.at(0))); return; } polygon << QPointF(x, y); d = d.right(d.size() - pos - xy.at(0).size() + 1); } mDNA.append(Gene(polygon, color)); while (mXml.readNextStartElement()) { if (mXml.name() == "path") readPath(); else mXml.skipCurrentElement(); } }
//// opens the input file and feeds lines to the Gene class void GeneList::readfile( std::string const filename ) { std::ifstream file; file.open( filename.c_str() ); if ( outputlevel_ >= NORMAL ) std::cout << "\nReading sequence file " << filename << std::endl; // read input file // using a char array is faster than reading in/parsing strings char linebuff[4096]; while ( file.getline( linebuff, 4096 ) ) { if ( linebuff[0] == '>' ) { // FASTA header genes_.push_back( Gene( linebuff ) ); continue; } genes_.back().readline( linebuff, file.gcount() ); } finalize(); }
Gene Gene::cross(Gene* father, Gene* mother, bool mutate) { double perfectChild[100]; double* fatherDist = father->getDist(); double* motherDist = mother->getDist(); for(int i = 0; i < 100; i++) perfectChild[i] = (fatherDist[i] + motherDist[i]) / 2; delete[] fatherDist; delete[] motherDist; double imaginary[100]; for(int i = 0; i < 100; imaginary[i++] = 0); bool succeed = DFT(1, 100, perfectChild, imaginary); if(!succeed) exit(0); int* sines = selectTop(imaginary); int* cosines = selectTop(perfectChild); Harmonic waves[WAVECOUNT]; for(int i = 0; i < WAVECOUNT; i++) { double alpha = mutate && randFloat() <= MUTATION_RATE ? MAX_OF_SCALED_SIN_PLUS_COS - 2 * randFloat() * MAX_OF_SCALED_SIN_PLUS_COS : imaginary[sines[i]]; double beta = mutate && randFloat() <= MUTATION_RATE ? MAX_OF_SCALED_SIN_PLUS_COS - 2 * randFloat() * MAX_OF_SCALED_SIN_PLUS_COS : perfectChild[cosines[i]]; int mu = mutate && randFloat() <= MUTATION_RATE ? (rand() % 48) + 1 : sines[i]; int omega = mutate && randFloat() <= MUTATION_RATE ? (rand() % 48) + 1 : cosines[i]; waves[i] = Harmonic(alpha, mu, beta, omega); } double magnitude = mutate && randFloat() <= MUTATION_RATE ? randFloat() * (father->magnitude + mother->magnitude) : ((father->magnitude + mother->magnitude) / 2); return Gene(magnitude, waves); }
int main(int argc,char *argv[]) { srand((unsigned)time(NULL)); int n,m; /* * make 2^n optimal vantage points set * vector<Gene> twoPowOptimalPoints */ makeTwoPowOptimalPoints(); #ifdef DEBUG for ( int i = 0 ; i < MAX_DIMENSION_POW ; i++ ) { puts(""); for ( int j = 0 ; j < twoPowOptimalPoints[i].m ; j++ ) { for ( int k = 0 ; k < twoPowOptimalPoints[i].n ; k++ ) printf("%d ",twoPowOptimalPoints[i].g[j][k]); puts(""); } } #endif for ( int tot = 11 ; tot <= 35; tot+= 1 ) { /* system("rm graph/g*.max"); system("rm graph/g*.out"); system("rm graph/g*"); */ n = m = tot; // scanf("%d %d",&n,&m); /* * make genes * vector<Gene> genes; */ vector<Gene> genes; for ( int i = 0 ; i < INIT_SIZE ; i++ ) genes.push_back(initializingGene(n,m,i)); #ifdef DEBUG for ( int i = 0 ; i < INIT_SIZE ; i++ ) genes[i].printGene(stdout); #endif for ( int generation = 1 ; generation <= MAX_GENERATION ; generation++ ) { printf("current generation : %d\n",generation); /* * print now generation information */ char *filename; FILE *fp; bool printOption = ( !(generation%50) ); // bool printOption = true; // bool printOption = ( !(generation%1000) || (1 <= generation && generation <= 10) || generation == MAX_GENERATION); if ( printOption ) { filename = (char*)malloc(sizeof(char)*222); sprintf(filename,"result/g_%d_%d.vp",n,generation); fp = fopen(filename,"w"); fprintf(fp,"now generation = %d tot f = %lf\n",generation,calculateNowGenesF(genes)); for ( int i = 0 ; i < genes.size() ; i++ ) genes[i].printGene(fp); fclose(fp); sprintf(filename,"result/g_%d_%d.result",n,generation); fp = fopen(filename,"w"); fprintf(fp,"now generation = %d tot f = %lf\n",generation,calculateNowGenesF(genes)); } /* * min heap based on ff */ priority_queue<Gene,vector<Gene>,greater<Gene> > pq; for ( int i = 0 ; i < genes.size() ; i++ ) { Gene now(genes[i].n,genes[i].m); for ( int j = 0 ; j < genes[i].m ; j++ ) for ( int k = 0 ; k < genes[i].n ; k++ ) now.g[j][k] = genes[i].g[j][k]; pq.push(now); } genes.clear(); if ( printOption ) { fprintf(fp,"min f = %lf\n",f(pq.top().g,pq.top().n,pq.top().m)); fclose(fp); free(filename); } for ( int i = 0 ; i < INIT_SIZE*DOMINANCE_SIZE ; i++ ) { Gene now = pq.top();pq.pop(); Gene insertGene(now.n,now.m); for ( int j = 0 ; j < now.m ; j++ ) for ( int k = 0 ; k < now.n ; k++ ) insertGene.g[j][k] = now.g[j][k]; genes.push_back(insertGene); } while ( !pq.empty() ) { if ( (int)pq.size() == 1 ) { Gene tp1 = pq.top();pq.pop(); Gene p1(tp1.n,tp1.m); for ( int i = 0 ; i < tp1.m; i++ ) for ( int j = 0 ; j < tp1.n ; j++ ) p1.g[i][j] = tp1.g[i][j]; genes.push_back(p1); continue; } Gene tp1 = pq.top();pq.pop(); Gene tp2 = pq.top();pq.pop(); Gene p1(tp1.n,tp1.m); Gene p2(tp2.n,tp2.m); for ( int i = 0 ; i < tp1.m ; i++ ) for ( int j = 0 ; j < tp1.n ; j++ ) p1.g[i][j] = tp1.g[i][j]; for ( int i = 0 ; i < tp2.m ; i++ ) for ( int j = 0 ; j < tp2.n ; j++ ) p2.g[i][j] = tp2.g[i][j]; vector<Gene> nextGenes; nextGenes.push_back(Gene(p1.n,p1.m)); nextGenes.push_back(Gene(p2.n,p2.m)); int pos[2]={}; Gene np1(p1.n,p1.m); Gene np2(p2.n,p2.m); int t_pos[2]={}; for ( int i = 0 ; i < p1.m ; i++ ) { if ( isSameVantagePointsInGene(p1,p2,i) ) { for ( int j = 0 ; j < p1.n ; j++ ) nextGenes[0].g[pos[0]][j] = p1.g[i][j]; pos[0]++; } else { for ( int j = 0 ; j < p1.n; j++ ) np1.g[t_pos[0]][j] = p1.g[i][j]; t_pos[0]++; } } np1.m = t_pos[0]; for ( int i = 0 ; i < p2.m ; i++ ) { if ( isSameVantagePointsInGene(p2,p1,i) ) { for ( int j = 0 ; j < p2.n ; j++ ) nextGenes[1].g[pos[1]][j] = p2.g[i][j]; pos[1]++; } else { for ( int j = 0 ; j < p2.n ; j++ ) np2.g[t_pos[1]][j] = p2.g[i][j]; t_pos[1]++; } } np2.m = t_pos[1]; vector<Graph> g1,g2; g1 = makeGraph(np1); vector<vector<int> > V1,V2; V1 = maxCut(generation,np1.m,g1); for ( int i = 0 ; i < V1[0].size() ; i++ ) { for ( int j = 0 ; j < np1.n ; j++ ) nextGenes[0].g[pos[0]][j] = np1.g[V1[0][i]][j]; pos[0]++; } for ( int i = 0 ; i < V1[1].size() ; i++ ) { for ( int j = 0 ; j < np2.n ; j++ ) nextGenes[1].g[pos[1]][j] = np1.g[V1[1][i]][j]; pos[1]++; } g2 = makeGraph(np2); V2 = maxCut(generation,np2.m,g2); for ( int i = 0 ; i < V2[0].size() ; i++ ) { for ( int j = 0 ; j < np2.n ; j++ ) nextGenes[0].g[pos[0]][j] = np2.g[V2[0][i]][j]; pos[0]++; } Gene now(np2.n,V2[1].size()); for ( int i = 0 ; i < V2[1].size() ; i++ ) { for ( int j = 0 ; j < np2.n; j++ ) now.g[i][j] = np2.g[V2[1][i]][j]; } if ( pos[0] == m ) { for ( int i = 0 ; i < now.m ; i++ ) { for ( int j = 0 ; j < now.n ; j++ ) nextGenes[1].g[pos[1]][j] = now.g[i][j]; pos[1]++; } } while ( pos[0] < m ) { vector<Graph> ng = makeGraph(now); vector<vector<int> > nV; nV = maxCut(generation,now.m,ng); for ( int i = 0 ; i < nV[0].size() ; i++ ) { if ( pos[0] < m ) { for ( int j = 0 ; j < now.n ; j++ ) nextGenes[0].g[pos[0]][j] = now.g[nV[0][i]][j]; pos[0]++; } else { nV[1].push_back(nV[0][i]); } } if ( pos[0] == m ) { for ( int i = 0 ; i < nV[1].size() ; i++ ) { for ( int j = 0 ; j < now.n ; j++ ) nextGenes[1].g[pos[1]][j] = now.g[nV[1][i]][j]; pos[1]++; } break; } else { Gene next(now.n,nV[1].size()); int nPos = 0; for ( int i = 0 ; i < nV[1].size() ; i++ ) { for ( int j = 0 ; j < now.n ; j++ ) next.g[nPos][j] = now.g[nV[1][i]][j]; nPos++; } now.m = nV[1].size(); for ( int i = 0 ; i < next.m ; i++ ) for ( int j = 0 ; j < now.n ; j++ ) now.g[i][j] = next.g[i][j]; } } if ( f(nextGenes[0].g,nextGenes[0].n,nextGenes[0].m) <= f(nextGenes[1].g,nextGenes[1].n,nextGenes[1].m) ) { genes.push_back(nextGenes[0]); pq.push(nextGenes[1]); } else { genes.push_back(nextGenes[1]); pq.push(nextGenes[0]); } g1.clear(); g2.clear(); for ( int i = 0 ; i < V1.size() ; i++ ) V1[i].clear(); V1.clear(); for ( int i = 0 ; i < V2.size() ; i++ ) V2[i].clear(); V2.clear(); } mutation(genes); } } return 0; }
Gene Gene::randomMutation() const { return Gene(false); }