Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/PermutationToRealVectorEncoder.cs @ 3348

Last change on this file since 3348 was 3348, checked in by mkofler, 12 years ago

Added project for particle swarm optimization algorithm #852 (WIP)

File size: 1.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Operators;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using HeuristicLab.Parameters;
9using HeuristicLab.Encodings.PermutationEncoding;
10using HeuristicLab.Encodings.RealVectorEncoding;
11using HeuristicLab.Collections;
12
13namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
14  public class PermutationToRealVectorEncoder : SingleSuccessorOperator, IRealVectorEncoder, IPermutationManipulator {
15
16    public ILookupParameter<RealVector> RealVectorParameter {
17      get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
18    }
19
20    public ILookupParameter<Permutation> PermutationParameter {
21      get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
22    }
23
24    public PermutationToRealVectorEncoder() : base() {
25      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The permutation to encode."));
26      Parameters.Add(new LookupParameter<RealVector>("RealVector", "The resulting real vector."));
27    }
28
29    public override IOperation Apply() {
30      Permutation permutation = PermutationParameter.ActualValue;
31
32      RealVector realVector = new RealVector(permutation.Length);
33      double max = permutation.Length;
34      for (int i = 0; i < permutation.Length; i++) {
35        realVector[permutation[i]] = max;
36        max = max - 1;
37      }
38      RealVectorParameter.ActualValue = realVector;
39      return base.Apply();
40    }
41
42    public override bool CanChangeName {
43      get { return false; }
44    }
45  }
46}
Note: See TracBrowser for help on using the repository browser.