Free cookie consent management tool by TermsFeed Policy Generator

Ticket #2319: Grouping Benchmark Problem LLE.cs

File Grouping Benchmark Problem LLE.cs, 2.0 KB (added by abeham, 9 years ago)

Code for a benchmark grouping problem using LLE

Line 
1using System;
2using System.Linq;
3using System.Collections.Generic;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Encodings.LinearLinkageEncoding;
8using HeuristicLab.Optimization;
9using HeuristicLab.Problems.Programmable;
10
11namespace HeuristicLab.Problems.Programmable {
12  public class CompiledSingleObjectiveProblemDefinition : CompiledProblemDefinition, ISingleObjectiveProblemDefinition {
13    private const int ProblemSize = 100;
14    public bool Maximization { get { return false; } }
15
16    private bool[,] allowedTogether;
17     
18    public override void Initialize() {
19      var encoding = new LinearLinkageEncoding("lle", length: ProblemSize);
20      allowedTogether = new bool[encoding.Length, encoding.Length];
21      var random = new System.Random(13);
22      for (var i = 0; i < encoding.Length - 1; i++)
23        for (var j = i + 1; j < encoding.Length; j++)
24          allowedTogether[i, j] = allowedTogether[j, i] = random.Next(2) == 0;
25     
26      Encoding = encoding;
27    }
28
29    public double Evaluate(Individual individual, IRandom random) {
30      var penalty = 0;
31      var groups = individual.LinearLinkage("lle").GetGroups().ToList();
32      for (var i = 0; i < groups.Count; i++) {
33        for (var j = 0; j < groups[i].Count; j++)
34          for (var k = j + 1; k < groups[i].Count; k++)
35            if (!allowedTogether[groups[i][j], groups[i][k]]) penalty++;
36      }
37      if (penalty > 0) return penalty + ProblemSize;
38      else return groups.Count;
39    }
40
41    public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
42
43    public IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
44      foreach (var move in ExhaustiveSwap2MoveGenerator.Generate(individual.LinearLinkage("lle"))) {
45        var neighbor = individual.Copy();
46        var lle = neighbor.LinearLinkage("lle");
47        Swap2MoveMaker.Apply(lle, move);
48        yield return neighbor;
49      }
50    }
51  }
52}
53