<commit_before>module SequentialOptimizer

open PSO
open Models
open System

  type Config = {
    MaxIterations : int
  }

  let solve problem config (log: Log): Solution =
    let logLabeled tag msg = log (sprintf "%s | %s" problem.Description tag) msg 

    let startTime = DateTime.Now

    let iterParticleOnProblem = problem |> Particle.itterate
    
    let updateSingleParticle particle {GlobalBest = currentBest} = iterParticleOnProblem currentBest particle  
    
    let updateSingleAndApplyToSwarm swarm particle =
      let updatedSingleParticle = particle |> updateSingleParticle <| swarm
      let updatedSwarm = Swarm.update swarm updatedSingleParticle.LocalBest
      { updatedSwarm with Particles = (updatedSingleParticle::swarm.Particles) }
    
    let singleIterationOverWholeSwarm ({GlobalBest = globalBest; Particles = particles}) : Swarm = 
      Seq.fold updateSingleAndApplyToSwarm {GlobalBest = globalBest;Particles = List.empty} particles
      
    let itterationWithIndex swarm i = 
      let updatedSwarm = singleIterationOverWholeSwarm swarm
      
      let avgVelocity = updatedSwarm.Particles |> List.averageBy (fun p -> (Array.averageBy abs p.Velocity))
      logLabeled (sprintf "average Velocity (%d)" i) (sprintf "%A" avgVelocity)
      
      updatedSwarm
      
    let swarm = Swarm.create problem
    
    let swarmAfterMaxIterations = Seq.fold itterationWithIndex swarm [1 .. config.MaxIterations]
    
    let endTime = DateTime.Now
    let duration = (endTime - startTime).TotalSeconds


    logLabeled (sprintf "duration for %d iterations" config.MaxIterations) (sprintf "%A" duration)
    logLabeled "final Positions" (sprintf "%A" swarmAfterMaxIterations.Particles)
    
    swarmAfterMaxIterations.GlobalBest<commit_msg>Fix fold usage. (#5)<commit_after>module SequentialOptimizer

open PSO
open Models
open System

  type Config = {
    MaxIterations : int
  }

  let solve problem config (log: Log): Solution =
    let logLabeled tag msg = log (sprintf "%s | %s" problem.Description tag) msg 

    let startTime = DateTime.Now

    let iterParticleOnProblem = problem |> Particle.itterate
    
    let updateSingleParticle particle {GlobalBest = currentBest} = iterParticleOnProblem currentBest particle  
    
    let updateSingleAndApplyToSwarm swarm particle =
      let updatedSingleParticle = particle |> updateSingleParticle <| swarm
      let updatedSwarm = Swarm.update swarm updatedSingleParticle.LocalBest
      { updatedSwarm with Particles = (updatedSingleParticle::swarm.Particles) }
    
    let singleIterationOverWholeSwarm ({GlobalBest = globalBest; Particles = particles}) : Swarm = 
      ({GlobalBest = globalBest; Particles = List.empty}, particles) ||> Seq.fold  updateSingleAndApplyToSwarm
      
    let itterationWithIndex swarm i = 
      let updatedSwarm = singleIterationOverWholeSwarm swarm
      
      let avgVelocity = updatedSwarm.Particles |> List.averageBy (fun p -> (Array.averageBy abs p.Velocity))
      logLabeled (sprintf "average Velocity (%d)" i) (sprintf "%A" avgVelocity)
      
      updatedSwarm
      
    let initialSwarm = Swarm.create problem
    
    let swarmAfterMaxIterations = (initialSwarm, [1 .. config.MaxIterations]) ||> Seq.fold itterationWithIndex 
    
    let endTime = DateTime.Now
    let duration = (endTime - startTime).TotalSeconds


    logLabeled (sprintf "duration for %d iterations" config.MaxIterations) (sprintf "%A" duration)
    logLabeled "final Positions" (sprintf "%A" swarmAfterMaxIterations.Particles)
    
    swarmAfterMaxIterations.GlobalBest
{'fraction_non_alphanumeric': 0.07546115148127445, 'fraction_numerical': 0.0008384572386808273, 'mean_word_length': 3.624031007751938, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2861763', 'n_tokens_mistral': 1055, 'n_tokens_neox': 1010, 'n_words': 279}