public override bool Try(SimulatedAnnealingSolver solverInstance, ref SatConfiguration currentConfiguration) { SatConfiguration triedConfiguration; double randomChoice = random.NextDouble(); if (randomChoice < randomNeighborProb) { triedConfiguration = GetRandomNeighbor(solverInstance, currentConfiguration); } else if (randomChoice < randomNeighborProb + randomNewProb) { triedConfiguration = SatConfiguration.RandomConfiguration(solverInstance.SatInstance, random); } else if (randomChoice < randomNeighborProb + randomNewProb + improveScoreProb) { triedConfiguration = GetBetterScoreNeighbor(solverInstance, currentConfiguration); } else { triedConfiguration = GetBetterSatisfiabilityNeighbor(solverInstance, currentConfiguration); } triedConfiguration.Score = solverInstance.Options.ScoreStrategy.CalculateScore(triedConfiguration, solverInstance); if (Accept(triedConfiguration, currentConfiguration, solverInstance.CurrentTemperature)) { currentConfiguration = triedConfiguration; return(true); } return(false); }
public SatResult Solve() { movesHistory = new List <DataPoint>(); currentConfiguration = Options.StartingPositionStrategy.GetStartingPosition(this); BestConfiguration = new SatConfiguration { Instance = SatInstance, Score = 0, Valuations = new List <bool>(new bool[SatInstance.Literals.Count]) }; CurrentTemperature = Options.BaseStartingTemperature * SatInstance.GetSumOfWeights(); ScaledMinTemperature = Options.MinimalTemperature * SatInstance.GetSumOfWeights(); NumberOfSteps = 0; var solutionLoopCount = 0; while (!RunSolutionLoop() && solutionLoopCount < MAX_RESTART_COUNT - 1) { solutionLoopCount++; CurrentTemperature = Options.BaseStartingTemperature * SatInstance.GetSumOfWeights(); AcceptedDuringEquilibrium = 0; EquilibriumSteps = 0; //Options.BaseEquilibriumSteps = (int) (Options.BaseEquilibriumSteps * EQUILIBRIUM_RESTART_MULTIPLIER); } return(new SatResult { Configuration = BestConfiguration, SatInstance = SatInstance, NumberOfSteps = this.NumberOfSteps, MovesHistory = movesHistory, RestartCount = solutionLoopCount, BestConfigurationFoundAt = bestConfigurationFoundAt, NumberOfUnsatisfiedClauses = BestConfiguration.NumberOfUnsatisfiedClauses() }); }
public float CalculateScore(SatConfiguration configuration, SimulatedAnnealingSolver solver) { //Base score is the optimalization value float score = configuration.GetOptimalizationValue(); //Penalize the score based on the number of unsatisfied clauses score -= configuration.NumberOfUnsatisfiedClauses() * solver.Options.PenaltyMultiplier; return(score); }
private SatConfiguration GetRandomNeighbor(SimulatedAnnealingSolver solverInstance, SatConfiguration currentConfiguration) { var flipIndex = random.Next(0, solverInstance.SatInstance.Literals.Count); var triedConfiguration = new SatConfiguration(currentConfiguration); triedConfiguration.FlipValuation(flipIndex); return(triedConfiguration); }
public float CalculateScore(SatConfiguration configuration, SimulatedAnnealingSolver solver) { //Base score is the optimalization value float score = configuration.GetOptimalizationValue(); //Make the score negative if it is unsatisfiable if (!configuration.IsSatisfiable()) { score -= configuration.Instance.GetSumOfWeights(); } //Penalize the score further based on the number of unsatisfied clauses score -= configuration.NumberOfUnsatisfiedClauses() * solver.Options.PenaltyMultiplier; return(score); }
private SatConfiguration GetBetterSatisfiabilityNeighbor(SimulatedAnnealingSolver solverInstance, SatConfiguration currentConfiguration) { var candidateIndex = currentConfiguration.IndexOfRandomToImproveSatisfiability(random); if (candidateIndex > 0) { var triedConfiguration = new SatConfiguration(currentConfiguration); triedConfiguration.FlipValuation(candidateIndex); return(triedConfiguration); } else { return(GetBetterScoreNeighbor(solverInstance, currentConfiguration)); } }
protected bool Accept(SatConfiguration triedConfiguration, SatConfiguration currentConfiguration, float currentTemperature) { //Tried configuration is better if (triedConfiguration.Score >= currentConfiguration.Score) { return(true); } var delta = triedConfiguration.Score - currentConfiguration.Score; //The configuration is not better, but will be accepted under the given probability if (random.NextDouble() < Math.Exp(delta / currentTemperature)) { return(true); } return(false); }
private SatConfiguration GetBetterScoreNeighbor(SimulatedAnnealingSolver solverInstance, SatConfiguration currentConfiguration) { var triedConfiguration = new SatConfiguration(currentConfiguration); var flipIndex = currentConfiguration.IndexOfRandomToImproveScore(random); //Nothing to improve (all valuations set to 1), reset the configuration entirely if (flipIndex < 0) { triedConfiguration = SatConfiguration.RandomConfiguration(solverInstance.SatInstance, random); } else { triedConfiguration.FlipValuation(flipIndex); } return(triedConfiguration); }
//Performs a random bit-flip public override bool Try(SimulatedAnnealingSolver solverInstance, ref SatConfiguration currentConfiguration) { var bitToFlip = random.Next(0, solverInstance.SatInstance.Literals.Count - 1); var triedConfiguration = new SatConfiguration(currentConfiguration); triedConfiguration.Valuations[bitToFlip] = !triedConfiguration.Valuations[bitToFlip]; triedConfiguration.Score = solverInstance.Options.ScoreStrategy.CalculateScore(triedConfiguration, solverInstance); if (Accept(triedConfiguration, currentConfiguration, solverInstance.CurrentTemperature)) { currentConfiguration = triedConfiguration; return(true); } return(false); }
private bool RunSolutionLoop() { while (!Options.FrozenStrategy.Frozen(this)) { AcceptedDuringEquilibrium = 0; EquilibriumSteps = 0; while (Options.EquilibriumStrategy.Equilibrium(this)) { EquilibriumSteps++; if (Options.SavePlotInfo) { movesHistory.Add(new DataPoint((int)NumberOfSteps + EquilibriumSteps, currentConfiguration.GetOptimalizationValue())); } //Try to accept a new state if (Options.TryStrategy.Try(this, ref currentConfiguration)) { AcceptedDuringEquilibrium++; } //Check if new maximum has been found if (currentConfiguration.GetOptimalizationValue() > BestConfiguration.GetOptimalizationValue() && currentConfiguration.IsSatisfiable()) { BestConfiguration = currentConfiguration; bestConfigurationFoundAt = NumberOfSteps + (ulong)EquilibriumSteps; } } CurrentTemperature = Options.CoolStrategy.Cool(this); NumberOfSteps += (ulong)EquilibriumSteps; } if (BestConfiguration.IsSatisfiable()) { return(true); } return(false); }
public abstract bool Try(SimulatedAnnealingSolver solverInstance, ref SatConfiguration currentConfiguration);
public SatConfiguration GetStartingPosition(SimulatedAnnealingSolver solverInstance) { return(SatConfiguration.RandomConfiguration(solverInstance.SatInstance, random)); }