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.MaxTakeProfit,
request.EligibleIndicators); request.EligibleIndicators);
// TODO: Trigger background genetic algorithm execution
// This will be implemented in the next step with a background service
return Ok(geneticRequest); return Ok(geneticRequest);
} }
catch (Exception ex) catch (Exception ex)

View File

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

View File

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

View File

@@ -203,7 +203,7 @@ public class GeneticService : IGeneticService
int populationSize, int populationSize,
int generations, int generations,
double mutationRate, double mutationRate,
string selectionMethod, GeneticSelectionMethod selectionMethod,
int elitismPercentage, int elitismPercentage,
double maxTakeProfit, double maxTakeProfit,
List<IndicatorType> eligibleIndicators) 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(), GeneticSelectionMethod.Tournament => new TournamentSelection(),
"roulette" => new RouletteWheelSelection(), GeneticSelectionMethod.Roulette => new RouletteWheelSelection(),
"fitness-weighted" => new RankSelection(), // Use rank selection as approximation GeneticSelectionMethod.FitnessWeighted => new RankSelection(), // Use rank selection as approximation
_ => new TournamentSelection() _ => new TournamentSelection()
}; };
} }

View File

@@ -416,4 +416,14 @@ public static class Enums
Moderate = 2, Moderate = 2,
Aggressive = 3 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 /// The selection method to use
/// </summary> /// </summary>
[Required] [Required]
public string SelectionMethod { get; set; } = "tournament"; public GeneticSelectionMethod SelectionMethod { get; set; } = GeneticSelectionMethod.Tournament;
/// <summary> /// <summary>
/// The percentage of elite individuals to preserve (1-50) /// 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 PopulationSize { get; set; }
public int Generations { get; set; } public int Generations { get; set; }
public double MutationRate { get; set; } public double MutationRate { get; set; }
public string SelectionMethod { get; set; } public GeneticSelectionMethod SelectionMethod { get; set; }
public int ElitismPercentage { get; set; } public int ElitismPercentage { get; set; }
public double MaxTakeProfit { get; set; } public double MaxTakeProfit { get; set; }
public List<IndicatorType> EligibleIndicators { get; set; } = new(); public List<IndicatorType> EligibleIndicators { get; set; } = new();

View File

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

View File

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

View File

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