Advertisement
lu4kedr

Untitled

Oct 1st, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8.  
  9. namespace BIA_Plotting
  10. {
  11.     public class HillClimbAlg
  12.     {
  13.         public float MinX { get; set; } = -100f;
  14.         public float MaxX { get; set; } = 100f;
  15.         public float MinY { get; set; } = -100f;
  16.         public float MaxY { get; set; } = 100f;
  17.         public float Step { get; set; } = 0.1f;
  18.         public int Iteration { get; set; } = 100;
  19.         public Func<float,float,float> Function { get; set; }
  20.  
  21.         private List<float[]> LookOut(float[] currentPos, int visiblity)
  22.         {
  23.             Random r = new Random();
  24.             List<float[]> rtn = new List<float[]>();
  25.             for (float i = currentPos[0]-(visiblity*Step); i < currentPos[0] + (visiblity * Step); i+=Step)
  26.             {
  27.                 if (i < MinX || i > MaxX) continue;
  28.  
  29.                 for (float j = currentPos[1] - (visiblity * Step); j < currentPos[1] + (visiblity * Step); j += Step)
  30.                 {
  31.                     if (j < MinY || j > MaxY) continue;
  32.                     float[] f = { i, j, Function(i, j) };
  33.                     rtn.Add(f);
  34.                 }
  35.             }
  36.  
  37.             return rtn;
  38.         }
  39.         public List<P3D>Compute(float startX, float startY, int visibility)
  40.         {
  41.             List<float[]> skip = new List<float[]>();
  42.             int iter = 1;
  43.             float actualZ = Function(startX, startY);
  44.             float[] tmpCoord = { startX, startY, actualZ };
  45.             P3D grb = new P3D(tmpCoord, Color.SteelBlue);
  46.             List<P3D> rtn = new List<P3D>() { grb };
  47.             while (iter < Iteration)
  48.             {
  49.                 iter++;
  50.                 if (skip.Contains(tmpCoord)) continue;
  51.                 var lo = LookOut(tmpCoord, visibility);
  52.                 if (lo != null&& lo.Count>0)
  53.                 {
  54.                     lo = lo.OrderBy(x => x[2]).ToList();
  55.                     if (lo.First()[2] > actualZ)
  56.                     {
  57.                         MessageBox.Show("Stuck");
  58.                         break;
  59.                     }
  60.                 }
  61.                 foreach (var it in lo)
  62.                 {
  63.                     if (skip.Contains(it)) continue;
  64.                     actualZ = it[2];
  65.                     rtn.Add(new P3D(it));
  66.                     skip.Add(tmpCoord);
  67.                     tmpCoord = it;
  68.  
  69.                     break;
  70.                 }
  71.  
  72.  
  73.             }
  74.             var lst = rtn.Last();
  75.             lst.Color = Color.IndianRed;
  76.             return rtn;
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement