Add enum for Selection method

This commit is contained in:
2025-07-11 17:15:11 +07:00
parent f720cb7321
commit c62570e15d
10 changed files with 59 additions and 39 deletions

View File

@@ -306,9 +306,6 @@ public class BacktestController : BaseController
request.MaxTakeProfit,
request.EligibleIndicators);
// TODO: Trigger background genetic algorithm execution
// This will be implemented in the next step with a background service
return Ok(geneticRequest);
}
catch (Exception ex)

View File

@@ -50,7 +50,7 @@ public class RunGeneticRequest
/// <summary>
/// The selection method to use
/// </summary>
public string SelectionMethod { get; set; } = "tournament";
public GeneticSelectionMethod SelectionMethod { get; set; }
/// <summary>
/// The percentage of elite individuals to preserve (1-50)

View File

@@ -36,7 +36,7 @@ public interface IGeneticService
int populationSize,
int generations,
double mutationRate,
string selectionMethod,
GeneticSelectionMethod selectionMethod,
int elitismPercentage,
double maxTakeProfit,
List<IndicatorType> eligibleIndicators);

View File

@@ -203,7 +203,7 @@ public class GeneticService : IGeneticService
int populationSize,
int generations,
double mutationRate,
string selectionMethod,
GeneticSelectionMethod selectionMethod,
int elitismPercentage,
double maxTakeProfit,
List<IndicatorType> eligibleIndicators)
@@ -422,13 +422,13 @@ public class GeneticService : IGeneticService
}
}
private ISelection GetSelection(string selectionMethod)
private ISelection GetSelection(GeneticSelectionMethod selectionMethod)
{
return selectionMethod.ToLower() switch
return selectionMethod switch
{
"tournament" => new TournamentSelection(),
"roulette" => new RouletteWheelSelection(),
"fitness-weighted" => new RankSelection(), // Use rank selection as approximation
GeneticSelectionMethod.Tournament => new TournamentSelection(),
GeneticSelectionMethod.Roulette => new RouletteWheelSelection(),
GeneticSelectionMethod.FitnessWeighted => new RankSelection(), // Use rank selection as approximation
_ => new TournamentSelection()
};
}

View File

@@ -416,4 +416,14 @@ public static class Enums
Moderate = 2,
Aggressive = 3
}
/// <summary>
/// Selection methods for genetic algorithm optimization
/// </summary>
public enum GeneticSelectionMethod
{
Tournament,
Roulette,
FitnessWeighted
}
}

View File

@@ -110,7 +110,7 @@ public class GeneticRequest
/// The selection method to use
/// </summary>
[Required]
public string SelectionMethod { get; set; } = "tournament";
public GeneticSelectionMethod SelectionMethod { get; set; } = GeneticSelectionMethod.Tournament;
/// <summary>
/// The percentage of elite individuals to preserve (1-50)

View File

@@ -19,7 +19,7 @@ namespace Managing.Infrastructure.Databases.MongoDb.Collections
public int PopulationSize { get; set; }
public int Generations { get; set; }
public double MutationRate { get; set; }
public string SelectionMethod { get; set; }
public GeneticSelectionMethod SelectionMethod { get; set; }
public int ElitismPercentage { get; set; }
public double MaxTakeProfit { get; set; }
public List<IndicatorType> EligibleIndicators { get; set; } = new();

View File

@@ -3721,7 +3721,7 @@ export interface GeneticRequest {
populationSize: number;
generations: number;
mutationRate: number;
selectionMethod: string;
selectionMethod: GeneticSelectionMethod;
elitismPercentage: number;
maxTakeProfit: number;
eligibleIndicators: IndicatorType[];
@@ -3743,6 +3743,12 @@ export enum GeneticRequestStatus {
Cancelled = "Cancelled",
}
export enum GeneticSelectionMethod {
Tournament = "Tournament",
Roulette = "Roulette",
FitnessWeighted = "FitnessWeighted",
}
export interface RunGeneticRequest {
ticker?: Ticker;
timeframe?: Timeframe;
@@ -3752,7 +3758,7 @@ export interface RunGeneticRequest {
populationSize?: number;
generations?: number;
mutationRate?: number;
selectionMethod?: string | null;
selectionMethod?: GeneticSelectionMethod;
elitismPercentage?: number;
maxTakeProfit?: number;
eligibleIndicators?: IndicatorType[] | null;

View File

@@ -668,7 +668,7 @@ export interface GeneticRequest {
populationSize: number;
generations: number;
mutationRate: number;
selectionMethod: string;
selectionMethod: GeneticSelectionMethod;
elitismPercentage: number;
maxTakeProfit: number;
eligibleIndicators: IndicatorType[];
@@ -690,6 +690,12 @@ export enum GeneticRequestStatus {
Cancelled = "Cancelled",
}
export enum GeneticSelectionMethod {
Tournament = "Tournament",
Roulette = "Roulette",
FitnessWeighted = "FitnessWeighted",
}
export interface RunGeneticRequest {
ticker?: Ticker;
timeframe?: Timeframe;
@@ -699,7 +705,7 @@ export interface RunGeneticRequest {
populationSize?: number;
generations?: number;
mutationRate?: number;
selectionMethod?: string | null;
selectionMethod?: GeneticSelectionMethod;
elitismPercentage?: number;
maxTakeProfit?: number;
eligibleIndicators?: IndicatorType[] | null;

View File

@@ -7,6 +7,7 @@ import {
type Backtest,
BacktestClient,
type GeneticRequest,
GeneticSelectionMethod,
IndicatorType,
type RunGeneticRequest,
Ticker,
@@ -45,7 +46,7 @@ interface GeneticBundleFormData {
populationSize: number
generations: number
mutationRate: number
selectionMethod: string
selectionMethod: GeneticSelectionMethod
elitismPercentage: number
maxTakeProfit: number
eligibleIndicators: IndicatorType[]
@@ -75,7 +76,7 @@ const BacktestGeneticBundle: React.FC = () => {
populationSize: 10,
generations: 5,
mutationRate: 0.3,
selectionMethod: 'tournament',
selectionMethod: GeneticSelectionMethod.Tournament,
elitismPercentage: 10,
maxTakeProfit: 2.0,
eligibleIndicators: ALL_INDICATORS,
@@ -153,7 +154,7 @@ const BacktestGeneticBundle: React.FC = () => {
setValue('populationSize', 10)
setValue('generations', 5)
setValue('mutationRate', 0.3)
setValue('selectionMethod', 'tournament')
setValue('selectionMethod', GeneticSelectionMethod.Tournament)
setValue('elitismPercentage', 10)
setValue('maxTakeProfit', 2.0)
setSelectedIndicators(ALL_INDICATORS)
@@ -411,9 +412,9 @@ const BacktestGeneticBundle: React.FC = () => {
className="select select-bordered w-full"
{...register('selectionMethod')}
>
<option value="tournament">Tournament Selection</option>
<option value="roulette">Roulette Wheel</option>
<option value="fitness-weighted">Fitness Weighted</option>
<option value={GeneticSelectionMethod.Tournament}>Tournament Selection</option>
<option value={GeneticSelectionMethod.Roulette}>Roulette Wheel</option>
<option value={GeneticSelectionMethod.FitnessWeighted}>Fitness Weighted</option>
</select>
</div>