public static EvaluatedConfiguration EvaluateConfiguration(this Instance instance, long configuration) { var weight = instance.GetWeight(configuration); var res = new EvaluatedConfiguration(instance, configuration, weight); return(res); }
private EvaluatedConfiguration GetRandomConfiguration(Instance instance, EvaluatedConfiguration currentConfiguration) { var variableIndex = rand.Next(instance.VariableCount); var variableBitArray = 1L << variableIndex; var newConfiguration = currentConfiguration.Configuration ^ variableBitArray; var newWeight = instance.GetWeight(newConfiguration); return(new EvaluatedConfiguration(instance, newConfiguration, newWeight)); }
public EvaluatedConfiguration Solve(Instance instance) { var bestConfiguration = new EvaluatedConfiguration(instance, 0, 0); var max = 1L << instance.VariableCount; for (var configuation = 0L; configuation < max; configuation++) { var weight = instance.GetWeight(configuation); if (weight > bestConfiguration.Weight) { bestConfiguration.Configuration = configuation; bestConfiguration.Weight = weight; } } return(bestConfiguration); }
public EvaluatedConfiguration Solve(Instance instance) { var currentConfiguration = new EvaluatedConfiguration(instance, 0, 0); var bestConfiguration = currentConfiguration; for (var temperature = initTemperature; IsFrozen(temperature); temperature *= coolingCoeficient) { currentConfiguration = bestConfiguration; // it is good to go back to best result sometimes for (var innerCycle = 0; Equilibrium(instance, innerCycle); ++innerCycle) { currentConfiguration = GetNextConfiguration(instance, temperature, currentConfiguration); OnNewConfiguration(currentConfiguration); if (currentConfiguration.Weight > bestConfiguration.Weight) { bestConfiguration = currentConfiguration; } } } OnNewConfiguration(bestConfiguration); return(bestConfiguration); }
protected virtual void OnNewConfiguration(EvaluatedConfiguration e) { NewConfiguration?.Invoke(this, e); }
private EvaluatedConfiguration GetNextConfiguration(Instance instance, double temperature, EvaluatedConfiguration currentConfiguration) { // get random state (only fitting in knapsack) var newConfiguration = GetRandomConfiguration(instance, currentConfiguration); // when new state is better if (newConfiguration.Weight > currentConfiguration.Weight) { return(newConfiguration); } else // when new state is worse { var random = rand.NextDouble(); var delta = currentConfiguration.Weight - newConfiguration.Weight; var useWorse = random < Math.Exp(-delta / temperature); return(useWorse ? newConfiguration : currentConfiguration); } }