33
44#include " genetic_strategy.h"
55
6- #include < fcntl.h>
7-
86#include < algorithm>
7+ #include < cstddef>
8+ #include < iterator>
9+ #include < random>
10+ #include < ranges>
911#include < unordered_set>
12+ #include < utility>
1013#include < vector>
1114
1215#include " boost/container_hash/hash.hpp"
1518#include " db_sta/dbSta.hh"
1619#include " gia.h"
1720#include " rsz/Resizer.hh"
18- #include " sta/Graph.hh"
19- #include " sta/MinMax.hh"
20- #include " sta/Search.hh"
21- #include " utils.h"
21+ #include " slack_tuning_strategy.h"
2222#include " utl/Logger.h"
2323#include " utl/deleter.h"
2424#include " utl/unique_name.h"
@@ -31,7 +31,7 @@ static void removeDuplicates(std::vector<SolutionSlack>& population,
3131{
3232 struct HashVector final
3333 {
34- size_t operator ()(const Solution & sol) const
34+ size_t operator ()(const SolutionSlack::Type & sol) const
3535 {
3636 size_t res = 0 ;
3737 for (const auto & item : sol) {
@@ -43,20 +43,19 @@ static void removeDuplicates(std::vector<SolutionSlack>& population,
4343 }
4444 };
4545
46- std::unordered_set<Solution , HashVector> taken;
46+ std::unordered_set<SolutionSlack::Type , HashVector> taken;
4747 population.erase (
48- std::remove_if (
49- population.begin (),
50- population.end (),
48+ std::ranges::begin (std::ranges::remove_if (
49+ population,
5150 [&taken, logger](const SolutionSlack& s) {
52- if (!taken.insert (s.solution ).second ) {
51+ if (!taken.insert (s.solution_ ).second ) {
5352 debugPrint (
5453 logger, RMP, " genetic" , 2 , " Removing: " + s.toString ());
5554 return true ;
5655 }
5756 debugPrint (logger, RMP, " genetic" , 2 , " Keeping: " + s.toString ());
5857 return false ;
59- }),
58+ })) ,
6059 population.end ());
6160}
6261
@@ -77,25 +76,15 @@ std::vector<GiaOp> GeneticStrategy::RunStrategy(
7776 " Generating and evaluating the initial population" );
7877 std::vector<SolutionSlack> population (pop_size_);
7978 for (auto & ind : population) {
80- ind.solution .reserve (initial_ops_);
79+ ind.solution_ .reserve (initial_ops_);
8180 for (size_t i = 0 ; i < initial_ops_; i++) {
82- ind.solution .push_back (all_ops[random_ () % all_ops.size ()]);
81+ ind.solution_ .push_back (all_ops[random_ () % all_ops.size ()]);
8382 }
8483 }
8584
86- logger->info (RMP,
87- 62 ,
88- " Resynthesis: starting genetic algorithm, Worst slack is {}" ,
89- GetWorstSlack (sta, corner_));
90-
9185 for (auto & candidate : population) {
92- EvaluateSlack (candidate,
93- candidate_vertices,
94- abc_library,
95- corner_,
96- sta,
97- name_generator,
98- logger);
86+ candidate.Evaluate (
87+ candidate_vertices, abc_library, corner_, sta, name_generator, logger);
9988
10089 debugPrint (logger, RMP, " genetic" , 1 , candidate.toString ());
10190 }
@@ -111,51 +100,50 @@ std::vector<GiaOp> GeneticStrategy::RunStrategy(
111100 if (rand1 == rand2) {
112101 continue ;
113102 }
114- Solution & parent1_sol = population[rand1].solution ;
115- Solution & parent2_sol = population[rand2].solution ;
116- Solution child_sol (parent1_sol. begin (),
117- parent1_sol.begin () + parent1_sol.size () / 2 );
103+ SolutionSlack::Type & parent1_sol = population[rand1].solution_ ;
104+ SolutionSlack::Type & parent2_sol = population[rand2].solution_ ;
105+ SolutionSlack::Type child_sol (
106+ parent1_sol. begin (), parent1_sol.begin () + parent1_sol.size () / 2 );
118107 child_sol.insert (child_sol.end (),
119108 parent2_sol.begin () + parent2_sol.size () / 2 ,
120109 parent2_sol.end ());
121110 SolutionSlack child_sol_slack;
122- child_sol_slack.solution = std::move (child_sol);
111+ child_sol_slack.solution_ = std::move (child_sol);
123112 population.emplace_back (child_sol_slack);
124113 }
125114 // Mutations
126115 unsigned mut_size = std::max<unsigned >(mut_prob_ * generation_size, 1 );
127116 for (unsigned j = 0 ; j < mut_size; j++) {
128117 SolutionSlack sol_slack;
129118 auto rand = random_ () % generation_size;
130- sol_slack.solution
131- = RandomNeighbor ( population[rand].solution , all_ops, logger);
119+ sol_slack.solution_
120+ = population[rand].RandomNeighbor ( all_ops, logger, random_ );
132121 population.emplace_back (sol_slack);
133122 }
134123 removeDuplicates (population, logger);
135124 // Evaluation
136125 for (auto & sol_slack : population) {
137- if (sol_slack.worst_slack ) {
126+ if (sol_slack.worst_slack_ ) {
138127 continue ;
139128 }
140- EvaluateSlack (sol_slack,
141- candidate_vertices,
142- abc_library,
143- corner_,
144- sta,
145- name_generator,
146- logger);
129+ sol_slack.Evaluate (candidate_vertices,
130+ abc_library,
131+ corner_,
132+ sta,
133+ name_generator,
134+ logger);
147135 }
148136 // Selection
149- std::sort (population. begin (), population. end () );
137+ std::ranges:: sort (population, std::less{} );
150138 std::vector<SolutionSlack> newPopulation;
151139 newPopulation.reserve (pop_size_);
152140 for (int j = 0 ; j < pop_size_; j++) {
153141 std::vector<size_t > tournament (tourn_size_);
154142 std::generate_n (tournament.begin (), tourn_size_, [&]() {
155143 return random_ () % population.size ();
156144 });
157- std::sort (tournament. begin (), tournament. end () );
158- tournament.erase (std::unique (tournament. begin (), tournament. end ( )),
145+ std::ranges:: sort (tournament);
146+ tournament.erase (std::ranges:: begin (std::ranges::unique (tournament )),
159147 tournament.end ());
160148 std::bernoulli_distribution bern_dist{tourn_prob_};
161149 for (const auto & candidateId : tournament) {
@@ -173,11 +161,11 @@ std::vector<GiaOp> GeneticStrategy::RunStrategy(
173161 }
174162 }
175163
176- auto best_it = std::min_element (population. begin (), population. end () );
164+ auto best_it = std::ranges:: min_element (population, std::less{} );
177165 logger->info (RMP,
178166 66 ,
179167 " Resynthesis: Best result is of individual {}" ,
180168 std::distance (population.begin (), best_it));
181- return best_it->solution ;
169+ return best_it->solution_ ;
182170}
183171} // namespace rmp
0 commit comments