Genetic algorithm (Preference based solver) #414
Wilmar-Smit
started this conversation in
BACKEND
Replies: 1 comment
-
Problem
Solution
DecisionsLanguage decision
Library decision
Positives
Middle grounsWe need to implement the following
void init_genes(
GeneType& p,
const std::function<double(void)>& rnd01
)
bool eval_solution(
const GeneType& p,
MiddleCostType& c
)
GeneType mutate(
const GeneType& p,
const std::function<double(void)>& rnd01,
double mutation_rate,
double shrink_scale
)
GeneType crossover(
const GeneType& X1,
const GeneType& X2,
const std::function<double(void)>& rnd01
)
AI Example of main for inferenceint main() {
// Instantiate the genetic engine using native vector containers directly
EA::Genetic<std::vector<int>, std::vector<double>> ga;
// --- STRAND 1: FUNCTION REGISTRATION ---
// Registers the function pointers to the genetic operator implementations
ga.init_genes = init_genes;
ga.eval_solution = eval_solution;
ga.mutate = mutate;
ga.crossover = crossover;
// --- STRAND 2: ALGORITHM MODES ---
ga.multi_objective = true; // Explicitly turn on Multi-Objective mode (NSGA-III)
ga.objective_number = 2; // Tells openGA your cost vector contains exactly 2 parameters
// --- STRAND 3: POPULATION & TIMING GENERATIONS ---
ga.population_number = 100; // 100 active solutions tracked per generation pool
ga.generation_number = 500; // Hard loop termination limit
// --- STRAND 4: PROBABILITY RATES ---
ga.crossover_rate = 0.85; // 85% probability that selected parent structures undergo a crossover
ga.mutation_rate = 0.10; // 10% base probability passed down to mutate individual gene points
// --- STRAND 5: SYSTEM HYPERPARAMETERS ---
ga.idle_mutation_rate = 0.25; // Boosts mutation scale automatically if population fitness flatlines
ga.dynamic_threading = true; // Automatically spreads evaluations across available CPU cores
// --- STRAND 6: EXECUTION ---
std::cout << "Launching genetic optimization engine..." << std::endl;
ga.solve(); // Blocks main thread execution until generation target is complete
std::cout << "Optimization loop complete!" << std::endl;
// --- STRAND 7: RESULTS EXTRACTION ---
// Pull the final raw results directly out of ga.last_generation using native array indexing
std::cout << "Best result score 1: " << ga.last_generation.best_middle_cost[0] << std::endl;
std::cout << "Best result score 2: " << ga.last_generation.best_middle_cost[1] << std::endl;
return 0;
}Design decisionsFactors we must assess. Pure failures
Value of solution
How we package the data we give the GA
Data package
Pure failure functionsC(n) Count constraint
P(n) pattern constraint.
Pre-optimisation
O(n) overlap constraint.
Pre-optimisation
{
std::vector<std::vector<bool>>
//(an insane optimisation from std vec is if you make a bool array it works with individual bits instead of a bool == byte)
}
Solution value functionV(n) value function
Diagramgraph LR
Start(["Start f(n)"]) --> C_n{"c(n) Count Constraint"}
C_n -- Pass --> P_n{"p(n) Pattern Constraint"}
P_n -- Pass --> H_n{"h(n) Overlap Constraint"}
H_n -- Pass --> V_n["v(n) Value Function"]
C_n -- Fail --> Penalty["Assign Penalty Score"]
P_n -- Fail --> Penalty
H_n -- Fail --> Penalty
Penalty --> Exit(["Exit evaluation"])
V_n --> Exit
Final optimisation
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
TLDR
Beta Was this translation helpful? Give feedback.
All reactions