public EntityArgs Convert(SquarePoint point) { return new BinaryEntityArgs { X = point.x, Y = point.y }; }
public IShlp GetShlpObject(SquarePoint point) { return new ShlpHyperCube( point, config.SideLength, config.DeltaSideLenth, config.IterationCount, config.InnerPointsCount, config.Bounds, config.Fn, config.IterationMode); }
public static Square GeneratePointsFromLeftCorner(SquarePoint startPoint, float sideLength, int innerpointsCount, SquarePoint[] bounds) { startPoint.Number = 1; var points = new List<SquarePoint> { startPoint }; points.Add(new SquarePoint(startPoint.x, startPoint.y + sideLength, 2)); points.Add(new SquarePoint(startPoint.x + sideLength, startPoint.y + sideLength, 3)); points.Add(new SquarePoint(startPoint.x + sideLength, startPoint.y, 4)); var square = new Square { BoundPoints = points }; square.CreateInnerPoints(innerpointsCount, bounds); return square; }
public NelderMid( MKT_Point startPoint, Func<float, float, float> fn, SquarePoint[] bounds, MktIterationMode iterationMode) { this.startPoint = startPoint; this.fn = fn; _points = new List<MKT_Point>(); this.bounds = bounds; currentPoints = new MKT_Point[3]; maxPoints = iterationMode == MktIterationMode.Full ? 75 : 6; }
public HyperCubeWrapper(Func<float,float,float> fn, SquarePoint[] bounds, MktIterationMode iterationMode) { this.config = new ShlpHyperCubeConfig { Fn = fn, DeltaSideLenth = 1.1f, InnerPointsCount = 5, SideLength = 1, IterationCount = 15, Bounds = bounds, IterationMode = iterationMode }; }
public void TestMethod1() { var startPoint = new SquarePoint(1, 1); var bounds = new[] { new SquarePoint(0, 0), new SquarePoint(4.2f, 6.4f), }; var hc = new ShlpHyperCube(startPoint, 1, 1.1f, 15, 10, bounds, MultiplicationCoord, MktIterationMode.Limited); var nm = new NelderMid(new MKT_Point(startPoint.x, startPoint.y, MultiplicationCoord), MultiplicationCoord, bounds, MktIterationMode.Limited); var watch1 = Stopwatch.StartNew(); var hsResult = hc.Calculate(); watch1.Stop(); var watch2 = Stopwatch.StartNew(); var nmResult = nm.Calculate(); watch2.Stop(); var nm_r = MultiplicationCoord(nmResult.x, nmResult.y); var hs_r = MultiplicationCoord(hsResult.x, hsResult.y); Assert.IsTrue(Math.Abs(nm_r - hs_r) <1); }
public HyperCube( SquarePoint startpoint, float sideLength, float deltaSideLenth, int iterationCount, int innerPointsCount, SquarePoint[] bounds, MktIterationMode iterationMode, bool isDebugMode = false) { this.deltaSideLength = deltaSideLenth; this.startPoint = startpoint; this.sideLength = sideLength; this.iterationCount = iterationMode == MktIterationMode.Full ? iterationCount : 2; this.innerPointsCount = innerPointsCount; this.isDebugMode = isDebugMode; this.bounds = bounds; }
public void TestMethod2() { Expression<Func<float, float, float>> fn_exp = (x, y) => (float)(Math.Pow((y - Math.Pow(x, 2)), 2) + Math.Pow((1 - x), 2)); var fn = fn_exp.Compile(); var startPoint = new SquarePoint(1, 1); var bounds = new[] { new SquarePoint(0, 0), new SquarePoint(4.2f, 6.4f), }; var hc = new ShlpHyperCube(startPoint, 1, 1.1f, 25, 10, bounds, fn, MktIterationMode.Limited); var nm = new NelderMid(new MKT_Point(startPoint.x, startPoint.y, fn), fn, bounds, MktIterationMode.Limited); var watch1 = Stopwatch.StartNew(); var hsResult = hc.Calculate(); watch1.Stop(); var watch2 = Stopwatch.StartNew(); var nmResult = nm.Calculate(); watch2.Stop(); var nm_r = fn(nmResult.x, nmResult.y); var hs_r = fn(hsResult.x, hsResult.y); Assert.IsTrue(Math.Abs(nm_r - hs_r) < 0.01); }
public void CreateInnerPoints(int innerpointsCount, SquarePoint[] bounds) { var points = new List<SquarePoint>(); var point3 = this.BoundPoints.Single(x => x.Number == 3); var maxX = Math.Min(point3.x, bounds[1].x); var maxY = Math.Min(point3.y, bounds[1].y); var point1 = this.BoundPoints.Single(x => x.Number == 1); var minX = Math.Max(point1.x, bounds[0].x); var minY = Math.Max(point1.y, bounds[0].y); var rnd = new Random(DateTime.Now.GetHashCode()); for (int i = 0; i < innerpointsCount; i++) { var rndX = minX + rnd.NextDouble() * (maxX - minX); var rndY = minY + rnd.NextDouble() * (maxY - minY); points.Add(new SquarePoint((float)rndX, (float)rndY, i)); } InnerPoints = points; }
public ShlpHyperCube( SquarePoint startpoint, float sideLength, float deltaSideLenth, int iterationCount, int innerPointsCount, SquarePoint[] bounds, Func<float, float, float> fn, MktIterationMode iterationMode) : base(startpoint, sideLength, deltaSideLenth, iterationCount, innerPointsCount, bounds, iterationMode, false) { this.fn = fn; }
private SquarePoint Iteration() { do { var isComressionNeeded = false; currentPoints = currentPoints.OrderByDescending(x => x.Value).ToArray(); var xh = currentPoints[hi]; var xg = currentPoints[gi]; var xl = currentPoints[li]; if (float.IsInfinity(xl.Value)) { break; } var xc = new SquarePoint( (xg.x + xl.x) / 2, (xg.y + xl.y) / 2); var xr = new MKT_Point( Xr(xc.x, xh.x), Xr(xc.y, xh.y), fn); if (xr.Value < xl.Value) { var xe = new MKT_Point( Xe(xc.x, xr.x), Xe(xc.y, xr.y), fn); if (xe.Value <= xr.Value) { SetupCurrentPoint(xe, hi); } else { SetupCurrentPoint(xr, hi); } } else if (xl.Value < xr.Value && xr.Value < xh.Value) { SetupCurrentPoint(xr, hi); } else if (xg.Value < xr.Value && xr.Value < xh.Value) { var buf_xr = xr; xr = xh; xh = buf_xr; isComressionNeeded = true; } else { isComressionNeeded = true; } if (isComressionNeeded) { var xs = new MKT_Point(Xs(xh.x, xc.x), Xs(xh.y, xc.y), fn); if (xs.Value < xh.Value) { SetupCurrentPoint(xs, hi); } else { var xi_xh = new MKT_Point(Xi(xh.x, xl.x), Xi(xh.y, xl.y), fn); SetupCurrentPoint(xi_xh, hi); var xi_xg = new MKT_Point(Xi(xg.x, xl.x), Xi(xg.y, xl.y), fn); SetupCurrentPoint(xi_xg, hi); } } } while (OneMoreIteration()); var suitablePoints = _points.Where(x => x.x <= bounds[1].x && x.x >= bounds[0].x && x.y >= bounds[0].y && x.y <= bounds[1].y); return suitablePoints .FirstOrDefault(x => Math.Abs(x.Value - suitablePoints.Min(c => c.Value)) < 0.01); }
public IShlp GetShlpObject(SquarePoint point) { return new NelderMid((MKT_Point)point, fn, bounds, iterationMode); }
public NelderMidWrapper(Func<float, float, float> fn, SquarePoint[] bounds, MktIterationMode iterationMode) { this.fn = fn; this.bounds = bounds; this.iterationMode = iterationMode; }
void AnalizeInternalTriagle(Buffer <int> buffer, Square square, int x, int y, float moveVertextToCorner = 0.3f) { SquarePoint startPoint = SquarePoint.Center; SquareBaseShape shape = SquareBaseShape.Triangle; int value; if (buffer.Get(x, y - 1, out value) && value >= DefaultValue && buffer.Get(x + 1, y, out value) && value >= DefaultValue) { startPoint = SquarePoint.BottomRight; square.Init(startPoint, shape); var point1 = SquarePoint.BottomLeft; var point2 = SquarePoint.TopRight; var pointOffset1 = Vector3.right * moveVertextToCorner; var pointOffset2 = Vector3.down * moveVertextToCorner; var pointPos1 = square.GetAbsolutePosition(point1) + pointOffset1; var pointPos2 = square.GetAbsolutePosition(point2) + pointOffset2; square.GetNode(point1).Vertex.Pos.x = pointPos1.x; square.GetNode(point2).Vertex.Pos.y = pointPos2.y; } if (buffer.Get(x, y + 1, out value) && value >= DefaultValue && buffer.Get(x - 1, y, out value) && value >= DefaultValue) { startPoint = SquarePoint.TopLeft; square.Init(startPoint, shape); var point1 = SquarePoint.BottomLeft; var point2 = SquarePoint.TopRight; var pointOffset1 = Vector3.up * moveVertextToCorner; var pointOffset2 = Vector3.left * moveVertextToCorner; var pointPos1 = square.GetAbsolutePosition(point1) + pointOffset1; var pointPos2 = square.GetAbsolutePosition(point2) + pointOffset2; square.GetNode(point1).Vertex.Pos.y = pointPos1.y; square.GetNode(point2).Vertex.Pos.x = pointPos2.x; } if (buffer.Get(x, y - 1, out value) && value >= DefaultValue && buffer.Get(x - 1, y, out value) && value >= DefaultValue) { startPoint = SquarePoint.BottomLeft; square.Init(startPoint, shape); var point1 = SquarePoint.TopLeft; var point2 = SquarePoint.BottomRight; var pointOffset1 = Vector3.down * moveVertextToCorner; var pointOffset2 = Vector3.left * moveVertextToCorner; var pointPos1 = square.GetAbsolutePosition(point1) + pointOffset1; var pointPos2 = square.GetAbsolutePosition(point2) + pointOffset2; square.GetNode(point1).Vertex.Pos.y = pointPos1.y; square.GetNode(point2).Vertex.Pos.x = pointPos2.x; } if (buffer.Get(x, y + 1, out value) && value >= DefaultValue && buffer.Get(x + 1, y, out value) && value >= DefaultValue) { startPoint = SquarePoint.TopRight; square.Init(startPoint, shape); var point1 = SquarePoint.TopLeft; var point2 = SquarePoint.BottomRight; var pointOffset1 = Vector3.right * moveVertextToCorner; var pointOffset2 = Vector3.up * moveVertextToCorner; var pointPos1 = square.GetAbsolutePosition(point1) + pointOffset1; var pointPos2 = square.GetAbsolutePosition(point2) + pointOffset2; square.GetNode(point1).Vertex.Pos.x = pointPos1.x; square.GetNode(point2).Vertex.Pos.y = pointPos2.y; } }
private static SectorEdge BuildEdge(Position square, EdgeSegmentId id, SquarePoint left, SquarePoint right) => SectorEdge.FromPosition( square, new EdgeSegment( id, new SectorDescription(1), SectorDescription.OutsideLevel, Left: left, Right: right));