public AnalysisResultsPage(List<AlgorithmResult> algorithmResults) { InitializeComponent(); foreach (var algRes in algorithmResults.Where(result => result.Results != null)) { algRes.Results = algRes.Results.Select(_preprocessAlgResults).ToList(); } foreach ( var finalRes in algorithmResults.SelectMany(result => result.FinalResults)) { finalRes.WritableHelperData = _preprocessAlgResults(finalRes.HelperData); } _presenter = new ResultsPresenter(algorithmResults); DataContext = _presenter; }
private static int[] PruneAutosavesSprtGetDeletes(List<int>[] deletableSets, int numToDelete) { if (deletableSets.Sum(o => o.Count) == numToDelete) { // Everything in deletable sets needs to be deleted, so just return them all instead of taking the expense of load balancing return deletableSets.SelectMany(o => o).ToArray(); } List<int> retVal = new List<int>(); for (int cntr = 0; cntr < numToDelete; cntr++) { // Get the largest sets int maxSize = deletableSets.Max(o => o.Count); List<int>[] largest = deletableSets.Where(o => o.Count == maxSize).ToArray(); // Pick a random set, and a random index within that set int setIndex = StaticRandom.Next(largest.Length); int innerIndex = StaticRandom.Next(largest[setIndex].Count); // Store the value pointed to retVal.Add(largest[setIndex][innerIndex]); // Now remove this from the set largest[setIndex].RemoveAt(innerIndex); } return retVal.ToArray(); }
public static Point3D[] GetVoronoiCtrlPoints(ShotHit[] hits, ITriangle[] convexHull, int maxCount = 33) { const int MIN = 5; Random rand = StaticRandom.GetRandomForThread(); #region examine hits Tuple<int, double>[] hitsByLength = hits. Select((o, i) => Tuple.Create(i, (o.Hit.Item2 - o.Hit.Item1).Length)). OrderByDescending(o => o.Item2). ToArray(); double totalLength = hitsByLength.Sum(o => o.Item2); Tuple<int, double>[] hitsByPercentLength = hitsByLength. Select(o => Tuple.Create(o.Item1, o.Item2 / totalLength)). ToArray(); #endregion #region define control point cones var aabb = Math3D.GetAABB(convexHull); double aabbLen = (aabb.Item2 - aabb.Item1).Length; double entryRadius = aabbLen * .05; double exitRadius = aabbLen * .35; double maxAxisLength = aabbLen * .75; int count = hits.Length * 2; count += (totalLength / (aabbLen * .1)).ToInt_Round(); if (count > maxCount) { count = maxCount; } #endregion #region randomly pick control points // Keep adding rings around shots until the count is exceeded var sets = new List<Tuple<int, List<Point3D>>>(); int runningSum = 0; // Make sure each hit line gets some points for (int cntr = 0; cntr < hits.Length; cntr++) { AddToHitline(ref runningSum, sets, cntr, hits[cntr].Hit, entryRadius, exitRadius, maxAxisLength, rand); } // Now that all hit lines have points, randomly choose lines until count is exceeded while (runningSum < count) { var pointsPerLength = hitsByLength. Select(o => { var pointsForIndex = sets.FirstOrDefault(p => p.Item1 == o.Item1); int countForIndex = pointsForIndex == null ? 0 : pointsForIndex.Item2.Count; return Tuple.Create(o.Item1, countForIndex / o.Item2); }). OrderBy(o => o.Item2). ToArray(); double sumRatio = pointsPerLength.Sum(o => o.Item2); var pointsPerLengthNormalized = pointsPerLength. Select(o => Tuple.Create(o.Item1, o.Item2 / sumRatio)). ToArray(); int index = UtilityCore.GetIndexIntoList(rand.NextPow(3), pointsPerLengthNormalized); AddToHitline(ref runningSum, sets, index, hits[index].Hit, entryRadius, exitRadius, maxAxisLength, rand); } #endregion #region remove excessive points while (runningSum > count) { Tuple<int, double>[] fractions = sets. Select((o, i) => Tuple.Create(i, o.Item2.Count.ToDouble() / runningSum.ToDouble())). OrderByDescending(o => o.Item2). ToArray(); int fractionIndex = UtilityCore.GetIndexIntoList(rand.NextPow(1.5), fractions); //nextPow will favor the front of the list, which is where the rings with the most points are int setIndex = fractions[fractionIndex].Item1; sets[setIndex].Item2.RemoveAt(UtilityCore.GetIndexIntoList(rand.NextDouble(), sets[setIndex].Item2.Count)); runningSum--; } #endregion #region ensure enough for voronoi algorithm List<Point3D> retVal = new List<Point3D>(); retVal.AddRange(sets.SelectMany(o => o.Item2)); // The voronoi algrorithm fails if there aren't at least 5 points (really should fix that). So add points that are // way away from the hull. This will make the voronoi algorithm happy, and won't affect the local results if (count < MIN) { retVal.AddRange( Enumerable.Range(0, MIN - count). Select(o => Math3D.GetRandomVector_Spherical_Shell(aabbLen * 20).ToPoint()) ); } #endregion return retVal.ToArray(); }
private void btnFindVLANs_Click(object sender, RoutedEventArgs e) { var results = new List<List<Edge>>(); Action<HashSet<int>> action = nodeIds => { var nodes = _nodes .Where(p => nodeIds.Contains(p.NodeId)) .ToList(); var edges = _edges .Where(p => nodeIds.Contains(p.FirstNode.NodeId) && nodeIds.Contains(p.SecondNode.NodeId)) .ToList(); var mst = FindMinSpanTree(new NodesWithEdges { Nodes = nodes, Edges = edges }); results.Add(mst); foreach (var edge in mst) { PaintEdge(edge); PaintNode(edge.FirstNode); PaintNode(edge.SecondNode); } }; foreach (var vlan in _vlans) { _visitedBrush = new SolidColorBrush(vlan.Color); _offset += 3; action(new HashSet<int>(vlan.NodeIds)); } _offset = 0; var resultEdges = results .SelectMany(p => p) .ToList(); var resultNodeIds = resultEdges .Select(p => p.FirstNode) .Concat(resultEdges.Select(p => p.SecondNode)) .GroupBy(p => p.NodeId) .ToDictionary(p => p.Key, p => p.Count() > 1); var nonUniqueNodeIds = resultNodeIds .Where(p => p.Value) .Select(p => p.Key) .ToList(); var messages = new List<string>(); foreach (int nodeId in nonUniqueNodeIds) { foreach (var edge in resultEdges) { if (edge.FirstNode.NodeId == nodeId || edge.SecondNode.NodeId == nodeId) { int secondNodeId = edge.FirstNode.NodeId == nodeId ? edge.SecondNode.NodeId : edge.FirstNode.NodeId; messages.Add(nonUniqueNodeIds.Contains(secondNodeId) ? $"Open on commutator '{nodeId}' trunk-port for node '{secondNodeId}'" : $"Open on commutator '{nodeId}' access-port for host '{secondNodeId}'"); } } } foreach (var msg in messages.Distinct()) { lbxResults.Items.Add(msg); } }
private static void PruneBrainLinks_Merge(TriangleIndexed triangle, TriangleEdge edge, List<Tuple<int[], int[]>> links) { // Figure out which indexes to look for int[] pair = new[] { triangle.GetIndex(edge, true), triangle.GetIndex(edge, false) }; int other = triangle.IndexArray.First(o => !pair.Contains(o)); // Extract the affected links out of the list List<Tuple<int[], int[]>> affected = new List<Tuple<int[], int[]>>(); int index = 0; while (index < links.Count) { var current = links[index]; if (current.Item1.Contains(other) && current.Item2.Any(o => pair.Contains(o))) { affected.Add(current); links.RemoveAt(index); } else if (current.Item2.Contains(other) && current.Item1.Any(o => pair.Contains(o))) { affected.Add(Tuple.Create(current.Item2, current.Item1)); // reversing them so that Item1 is always other links.RemoveAt(index); } else { index++; } } // Combine the affected links (there shouldn't be more than two) var merged = Tuple.Create( affected.SelectMany(o => o.Item1).Distinct().ToArray(), affected.SelectMany(o => o.Item2).Distinct().ToArray()); links.Add(merged); }
public void ApplyForce2(BodyApplyForceAndTorqueArgs e) { _lines.Clear(); if (!_isUpPressed && !_isDownPressed) { return; } _bot.Fuel.QuantityCurrent = _bot.Fuel.QuantityMax; double elapsedTime = 1d; DateTime curTick = DateTime.UtcNow; if (_lastTick != null) { elapsedTime = (curTick - _lastTick.Value).TotalSeconds; } _lastTick = curTick; // Keeping each button's contribution in a set so they are easier to normalize List<Tuple<Thruster, int, double>[]> thrusterSets = new List<Tuple<Thruster, int, double>[]>(); Vector3D direction; if (_isUpPressed) { direction = new Vector3D(0, 0, 1); if (_useSimple) { thrusterSets.Add(FireThrustLinear1(e, elapsedTime, direction).ToArray()); } else { thrusterSets.Add(FireThrustLinear3(e, elapsedTime, direction).ToArray()); } } if (_isDownPressed) { direction = new Vector3D(0, 0, -1); if (_useSimple) { thrusterSets.Add(FireThrustLinear1(e, elapsedTime, direction).ToArray()); } else { thrusterSets.Add(FireThrustLinear3(e, elapsedTime, direction).ToArray()); } } //TODO: Normalize these so no thruster will fire above 100% (keep the ratios though) #region Fire them foreach (var thruster in thrusterSets.SelectMany(o => o)) { double percent = thruster.Item3; Vector3D? force = thruster.Item1.Fire(ref percent, thruster.Item2, elapsedTime); if (force != null) { Vector3D bodyForce = e.Body.DirectionToWorld(force.Value); Point3D bodyPoint = e.Body.PositionToWorld(thruster.Item1.Position); e.Body.AddForceAtPoint(bodyForce, bodyPoint); Vector3D lineVect = GetThrustLine(bodyForce, _itemOptions.Thruster_StrengthRatio); // this returns a vector in the opposite direction, so the line looks like a flame Point3D lineStart = bodyPoint + (lineVect.ToUnit() * thruster.Item1.ThrustVisualStartRadius); _lines.AddLine(lineStart, lineStart + lineVect); } else { int seven = -2; } } #endregion }