Exemplo n.º 1
0
//Runs the genetic algorithm
void Solver::runAStar()
{
    for(int i=0;i<gateLimit;++i)
    {
        circuits.push_back(Circuit());
    }
    bool go = true;
    int loc = -1;
    unsigned long long int generation=0;
    do
    {
        //algorithm adds new random gate every generation, and reverts if the fitness value goes lower
        ++generation;
        addGatesAstar();
        calcFitness();
        revert();
        runSortFitness();
        cull();
        loc = checkSolution();
        if(generation%1000 == 0)
        {
            cout<<"Generation: "<<generation<<"\n";
            cout<<"Num Gates:  "<<circuits[0].getGateNum()<<"\n";
        }
        if(loc != -1)
        {
            solutionLoc = loc;
            cout<<"Generation: "<<generation<<"\n";
            go = false;
        }
    }while(go);
}
Exemplo n.º 2
0
//Runs the genetic algorithm
//Note: Longer than 24 lines due to extensive commenting needed
void Solver::runGenetic()
{
    //Uses top 20% of children
    int keep = gateLimit/5;
    for(int i=0;i<keep;++i)
    {
        circuits.push_back(Circuit());
    }
    addNewGates(keep);
    bool go = true;
    int loc = -1;
    unsigned long long int generation=0;
    do
    {
        ++generation;
        //Randomly deletes a gate every 100 generations
        if(generation%100==0)
        {
            randomDelete();
        }
        //Gets all crosses for top 20% of children every 20 generations
        if(generation%20==0)
        {
            crossover(keep);
        }
        //Mutates a Random Gate every 10 generations
        if(generation%10==0)
        {
            mutate();
        }
        //Adds a new random Gate every generation
        addNewGates(keep);
        calcFitness();
        runSortFitness();
        shrink();
        cull();
        loc = checkSolution();
        if(generation%1000 == 0)
        {
            cout<<"Generation: "<<generation<<"\n";
            cout<<"Num Gates:  "<<circuits[0].getGateNum()<<"\n";
        }
        if(loc != -1)
        {
            solutionLoc = loc;
            cout<<"Generation: "<<generation<<"\n";
            go = false;
        }
    }while(go);
}
// This routine starts with some solution (current best or a random generated
// solution) and iteratively perturb changing some nodes and applying 2OPT.
bool TSP_Perturb2OPT(TSP_Data &tsp)
{
  ListGraph *g;
  int nchanges,i,j;
  g = &tsp.g;
  vector<Node> Circuit(tsp.NNodes), BestCircuit(tsp.NNodes);
  double BestCircuitValue;
  //nchanges = 2 (tests with some instances indicate that nchanges=1 is better
  nchanges = 1;

  // Start with a initial solution (if there is no solution, generate any sequence)
  if (tsp.BestCircuitValue < DBL_MAX) {
    BestCircuitValue = tsp.BestCircuitValue;
    for (int i=0; i<tsp.NNodes; i++) BestCircuit[i] = tsp.BestCircuit[i];
  } else {
    int i=0;
    BestCircuitValue = 0.0;
    for (ListGraph::NodeIt v(*g); v!=INVALID; ++v) BestCircuit[i++]=v;
    for (i=0;i<tsp.NNodes;i++) 
      BestCircuitValue += 
	tsp.AdjMat.Cost(BestCircuit[i] , BestCircuit[(i+1)%tsp.NNodes]);
  }

  for (int it=0;it<tsp.max_perturb2opt_it;it++) {
    if (!(it%100)) printf("[Heuristic: Perturbation+2OPT] it = %d (of %d)\n",it+1,tsp.max_perturb2opt_it);
    for (int k=0;k<tsp.NNodes;k++) Circuit[k] = BestCircuit[k];
    for (int nc=0;nc<nchanges;nc++) {
      i = (int) (drand48()*tsp.NNodes);  // get two random nodes and exchange
      j = (int) (drand48()*tsp.NNodes);  // their positions
      if (i!=j) ChangeNode(Circuit[i],Circuit[j]);
    }
    Heuristic_2_OPT(tsp.AdjMat,Circuit,BestCircuitValue,tsp.NNodes);
    if (BestCircuitValue < tsp.BestCircuitValue) { //update the best circuit used
      tsp.BestCircuitValue = BestCircuitValue;     // by the heuristic
      for (int i=0;i<tsp.NNodes;i++) {
	BestCircuit[i] = Circuit[i];
	tsp.BestCircuit[i] = Circuit[i];
      }
    }
  }
  return(true);
}
Exemplo n.º 4
0
Circuit Netlist::create_mna_circuit() {
    return Circuit(_nodes, _voltage_sources);
}
Exemplo n.º 5
0
//Crosses over all pairs in the top 20% of solutions
//EX: 1 and 2, 3 and 4, etc.
//Note longer than 24 lines due to the extensive data modification needed to collect all 
//10 possible crosses resulting from 2 solutions
void Solver::crossover(int k)
{
    if(k%2 == 0)
    {
        ++k;
    }
    for(int i=0;i<k;i+=2)
    {
        vector<Gate> g1 = circuits[i].getGates();
        vector<Gate> g2 = circuits[i+1].getGates();
        if(g1.size() >= 2 && g2.size() >= 2)
        {
            vector<Gate> g1h1;
            vector<Gate> g1h2;
            vector<Gate> g2h1;
            vector<Gate> g2h2;
            for(int j=0;j<g1.size()/2;++j)
            {
                g1h1.push_back(g1[j]);
            }
            for(int j=g1.size()-1;j>=g1.size()/2;--j)
            {
                g1h2.push_back(g1[j]);
            }
            for(int j=0;j<g2.size()/2;++j)
            {
                g2h1.push_back(g2[j]);
            }
            for(int j=g2.size()-1;j>=g2.size()/2;--j)
            {
                g2h2.push_back(g2[j]);
            }
            vector<vector<Gate>> crosses;
            crosses.push_back(vector<Gate>(g1h1));
            crosses.push_back(vector<Gate>(g2h1));
            crosses.push_back(vector<Gate>(g2h1));
            crosses.push_back(vector<Gate>(g1h1));
            crosses.push_back(vector<Gate>(g2h2));
            crosses.push_back(vector<Gate>(g2h1));
            crosses.push_back(vector<Gate>(g1h1));
            crosses.push_back(vector<Gate>(g1h2));
            crosses.push_back(vector<Gate>(g2h2));
            crosses.push_back(vector<Gate>(g1h2));
            for(int j=0;j<g1h1.size();++j)
            {
                crosses[5].push_back(g1h1[j]);
                crosses[9].push_back(g1h1[j]);
            }
            for(int j=0;j<g1h2.size();++j)
            {
                crosses[1].push_back(g1h2[j]);
                crosses[2].push_back(g1h2[j]);
                crosses[4].push_back(g1h2[j]);
            }
            for(int j=0;j<g2h1.size();++j)
            {
                crosses[6].push_back(g2h1[j]);
                crosses[8].push_back(g2h1[j]);
            }
            for(int j=0;j<g2h2.size();++j)
            {
                crosses[0].push_back(g2h2[j]);
                crosses[3].push_back(g2h2[j]);
                crosses[7].push_back(g2h2[j]);
            }
            for(int j=0;j<crosses.size();++j)
            {
                circuits.push_back(Circuit(crosses[j]));
            }
        }
    }
}