public Solution Solve(KnapsackData data) { _data = data; long start = DateTime.UtcNow.Millisecond; var current = new BinarySolution(data.AvailableItems.Count); var best = current; current.UpdateFitness(data, Alpha); while (_temperature > _endingTemperature) { for (var m = 0; m < _samplingSize; m++) { current = GetNextState(current); if (current.Fitness < best.Fitness) { best = current; } } Cool(); } long end = DateTime.UtcNow.Millisecond; var pickedItem = GenerateSolution(data, best); return(new Solution(pickedItem, end - start)); }
private BinarySolution GetNeighbour(BinarySolution current) { var mutated = new BinarySolution(current); var rnd = new Random(); var x = rnd.Next(current.GetSize()); mutated.Flip(x); mutated.UpdateFitness(_data, Alpha); return(mutated); }
public List <Item> GenerateSolution(KnapsackData knapsackData, BinarySolution solution) { var pickedItem = new List <Item>(); for (var i = 0; i < knapsackData.AvailableItems.Count; i++) { if (solution.GetBit(i) == 1) { pickedItem.Add(knapsackData.AvailableItems[i]); } } return(pickedItem); }
private BinarySolution GetNextState(BinarySolution current) { var newSolution = GetNeighbour(current); var delta = newSolution.Fitness - current.Fitness; if (delta < 0) { return(newSolution); } var rnd = new Random(); var x = rnd.NextDouble(); if (x < Math.Exp(-delta / _temperature)) { return(newSolution); } return(current); }
public BinarySolution(BinarySolution other) { _chromosome = other._chromosome; Fitness = other.Fitness; Weight = other.Weight; }