Exemplo n.º 1
0
/*
void mutation(vector<Gene> &genes) {
    for ( int i = 1 ; i < genes.size() ; i++ ) 
        for ( int j = 0 ; j < genes[i].m ; j++ ) 
            for ( int k = 0 ; k < genes[i].n ; k++ ) {
                double r = (double)rand()/RAND_MAX;
                if ( r < MUTATION_CHANCE ) 
                    genes[i].g[j][k]^=1;
                bool eq = false;
                for ( int jj = 0 ; jj < genes[i].m ; jj++ ) {
                    if ( jj == j ) continue;
                    bool now = true;
                    for ( int kk = 0 ; kk < genes[i].n ; kk++ ) 
                        if ( genes[i].g[j][k] != genes[i].g[jj][kk] ) 
                            now = false;
                    if ( now ) {
                        genes[i].g[j][k] ^= 1;
                        break;
                    }
                }
            }
}
*/
void maxCutForAdjustPoints(int generation,Gene p,vector<vector<int> > &now,int targetNumber,vector<int> realCutData) {
    if ( (int)changeVertex.size() == targetNumber ) return;
    if ( (int)now[0].size()+(int)changeVertex.size() <= targetNumber ) {
        for ( int i = 0 ; i < (int)now[0].size() ; i++ ) 
            changeVertex.push_back(realCutData[now[0][i]]);
    }
    if ( (int)now[1].size()+(int)changeVertex.size() <= targetNumber) {
        for ( int i = 0 ; i < (int)now[1].size() ; i++ ) 
            changeVertex.push_back(realCutData[now[1][i]]);
    }
    if ( (int)changeVertex.size() == targetNumber ) return;
    Gene np1(p.n,now[0].size());
    Gene np2(p.n,now[1].size());

    int pos = 0;
    for ( int i = 0 ; i < now[0].size() ; i++,pos++ ) 
        for ( int j = 0 ; j < np1.n ; j++ ) 
            np1.g[pos][j] = p.g[realCutData[now[0][i]]][j];
    pos = 0;
    for ( int i = 0 ; i < now[1].size() ; i++,pos++ ) 
        for ( int j = 0 ; j < np2.n ; j++ ) 
            np2.g[pos][j] = p.g[realCutData[now[1][i]]][j];
    vector<Graph> ng1,ng2;
    ng1 = makeGraph(np1);
    ng2 = makeGraph(np2);
    vector<vector<int> > next[2];
    next[0] = maxCut(generation,np1.m,ng1,realCutData);
    next[1] = maxCut(generation,np2.m,ng2,realCutData);

    maxCutForAdjustPoints(generation,np1,next[0],targetNumber,realCutData);
    maxCutForAdjustPoints(generation,np2,next[1],targetNumber,realCutData);
}
Exemplo n.º 2
0
void ReflowPage::wideLine(Panel::LcdPanel& gl,const Point& p1,const Point& p2,Panel::tCOLOUR cr) const {

    // set the line colour

    gl.setForeground(cr);

    // draw the first line

    gl.drawLine(p1,p2);

    // draw an adjacent line that's offset by 1px in the X direction if the line is steeper than
    // it is flat or in the Y direction if it's flatter than it is tall.

    Point np1(p1),np2(p2);

    if(Abs(p2.X-p1.X)>Abs(p2.Y-p1.Y)) {
        np1.Y++;
        np2.Y++;
    }
    else {
        np1.X++;
        np2.X++;
    }
    gl.drawLine(np1,np2);
}
Exemplo n.º 3
0
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;
}