private double SingleSwap([NotNull, ItemNotNull] ICollection <SwapPair> swaps) { SwapPair maxPair = null; double maxGain = double.MinValue; foreach (TVertex vertexFromA in _unSwappedSetA) { foreach (TVertex vertexFromB in _unSwappedSetB) { bool foundEdge = FindEdge(vertexFromA, vertexFromB, out TEdge edge); double edgeCost = foundEdge ? edge.Tag : 0.0; double gain = GetVertexCost(vertexFromA) + GetVertexCost(vertexFromB) - 2 * edgeCost; if (gain > maxGain) { maxPair = new SwapPair(vertexFromA, vertexFromB); maxGain = gain; } } } if (maxPair is null) { throw new InvalidOperationException("Must find a swap."); } SwapVertices(_vertexSetA, maxPair.Vertex1, _vertexSetB, maxPair.Vertex2); swaps.Add(maxPair); _unSwappedSetA.Remove(maxPair.Vertex1); _unSwappedSetB.Remove(maxPair.Vertex2); return(GetCutCost()); }
private Partition <TVertex> DoAllSwaps() { var swaps = new List <SwapPair>(); double minCost = double.MaxValue; int minId = -1; for (int i = 0; i < _partitionSize; ++i) { double cost = SingleSwap(swaps); if (cost < minCost) { minCost = cost; minId = i; } } // Back to swap step with min cut cost while (swaps.Count - 1 > minId) { SwapPair pair = swaps.Last(); swaps.Remove(pair); SwapVertices(_vertexSetA, pair.Vertex2, _vertexSetB, pair.Vertex1); } return(new Partition <TVertex>(_vertexSetA, _vertexSetB, minCost)); }
/// <summary>Delegate method invoked by dcSwap.</summary> private void ExecuteSwap() { if (CanExecuteSwap()) { Drive SwapDrive = _SourceDrive; _SourceDrive = _DestinationDrive; _DestinationDrive = SwapDrive; string sSwapDirectory = _sSourceDirectory; _sSourceDirectory = _sDestinationDirectory; _sDestinationDirectory = sSwapDirectory; _SourceDrive.isSource = true; _DestinationDrive.isSource = false; foreach (PairOfFiles SwapPair in _ltPairs) { SwapPair.SwapSourceAndDestination(); } RequeryDisplayedPairs(true); NotifyPropertyChanged("sSourceDirectory"); NotifyPropertyChanged("sDestinationDirectory"); } }
static IEnumerable <SwapPair> PossibleSwaps(Matchspace space) { var surfaceNoMatch = from Matchable match in space.grid where !match.Hidden() && !match.AnyMatch() select match; foreach (Matchable first in surfaceNoMatch.Where(first => first.AnyAlmost())) { foreach (Matchable second in surfaceNoMatch) { if (SwapPair.MatchingSwap(first, second)) { yield return(new SwapPair(first, second)); } } } }