Update fitness stagnation and fitness calculation based on score
This commit is contained in:
@@ -317,9 +317,12 @@ public class GeneticService : IGeneticService
|
||||
GetCrossover(request.CrossoverMethod),
|
||||
GetMutation(request.MutationMethod))
|
||||
{
|
||||
Termination = new GenerationNumberTermination(request.Generations),
|
||||
Termination = new OrTermination(
|
||||
new GenerationNumberTermination(request.Generations),
|
||||
new FitnessStagnationTermination(15)
|
||||
),
|
||||
MutationProbability = (float)request.MutationRate,
|
||||
CrossoverProbability = 0.7f, // Fixed crossover rate as in frontend
|
||||
CrossoverProbability = 0.75f, // Fixed crossover rate as in frontend
|
||||
TaskExecutor = new ParallelTaskExecutor
|
||||
{
|
||||
MinThreads = 4,
|
||||
@@ -897,15 +900,6 @@ public class TradingBotFitness : IFitness
|
||||
// Get current generation number (default to 0 if not available)
|
||||
var currentGeneration = _geneticAlgorithm?.GenerationsNumber ?? 0;
|
||||
|
||||
// Log the indicators being tested (only in first few generations to avoid spam)
|
||||
if (currentGeneration < 5)
|
||||
{
|
||||
var selectedIndicators = tradingBotChromosome.GetSelectedIndicators();
|
||||
var indicatorNames = string.Join(", ", selectedIndicators.Select(i => i.Type.ToString()));
|
||||
_logger.LogDebug("Generation {Generation}: Testing indicators: {Indicators}",
|
||||
currentGeneration, indicatorNames);
|
||||
}
|
||||
|
||||
// Run backtest
|
||||
var backtest = _backtester.RunTradingBotBacktest(
|
||||
config,
|
||||
@@ -941,45 +935,7 @@ public class TradingBotFitness : IFitness
|
||||
// Calculate base fitness from backtest score
|
||||
var baseFitness = backtest.Score;
|
||||
|
||||
// Apply constraint penalty if needed (this will guide the GA toward valid solutions)
|
||||
var constraintPenalty = CalculateConstraintPenalty(config);
|
||||
|
||||
// Return penalized fitness (constraint violations reduce fitness)
|
||||
return baseFitness * constraintPenalty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates a penalty factor for constraint violations (1.0 = no penalty, < 1.0 = penalty)
|
||||
/// </summary>
|
||||
private double CalculateConstraintPenalty(TradingBotConfig config)
|
||||
{
|
||||
var takeProfit = Convert.ToDouble(config.MoneyManagement.TakeProfit);
|
||||
var stopLoss = Convert.ToDouble(config.MoneyManagement.StopLoss);
|
||||
|
||||
// Check 1.1:1 risk-reward ratio constraint
|
||||
var minStopLoss = Math.Max(0.2, takeProfit / 1.1);
|
||||
var maxStopLoss = takeProfit - 0.1;
|
||||
|
||||
// If stop loss is within valid range, no penalty
|
||||
if (stopLoss >= minStopLoss && stopLoss <= maxStopLoss)
|
||||
{
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
// Calculate penalty based on how far outside the valid range the stop loss is
|
||||
var violation = 0.0;
|
||||
if (stopLoss < minStopLoss)
|
||||
{
|
||||
violation = (minStopLoss - stopLoss) / minStopLoss;
|
||||
}
|
||||
else if (stopLoss > maxStopLoss)
|
||||
{
|
||||
violation = (stopLoss - maxStopLoss) / maxStopLoss;
|
||||
}
|
||||
|
||||
// Apply penalty: 0.5 for severe violations, 0.8 for minor violations
|
||||
var penalty = Math.Max(0.5, 1.0 - (violation * 0.5));
|
||||
|
||||
return penalty;
|
||||
// Return base fitness (no penalty for now)
|
||||
return baseFitness;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user