Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace BIA_Plotting
- {
- public class HillClimbAlg
- {
- public float MinX { get; set; } = -100f;
- public float MaxX { get; set; } = 100f;
- public float MinY { get; set; } = -100f;
- public float MaxY { get; set; } = 100f;
- public float Step { get; set; } = 0.1f;
- public int Iteration { get; set; } = 100;
- public Func<float,float,float> Function { get; set; }
- private List<float[]> LookOut(float[] currentPos, int visiblity)
- {
- Random r = new Random();
- List<float[]> rtn = new List<float[]>();
- for (float i = currentPos[0]-(visiblity*Step); i < currentPos[0] + (visiblity * Step); i+=Step)
- {
- if (i < MinX || i > MaxX) continue;
- for (float j = currentPos[1] - (visiblity * Step); j < currentPos[1] + (visiblity * Step); j += Step)
- {
- if (j < MinY || j > MaxY) continue;
- float[] f = { i, j, Function(i, j) };
- rtn.Add(f);
- }
- }
- return rtn;
- }
- public List<P3D>Compute(float startX, float startY, int visibility)
- {
- List<float[]> skip = new List<float[]>();
- int iter = 1;
- float actualZ = Function(startX, startY);
- float[] tmpCoord = { startX, startY, actualZ };
- P3D grb = new P3D(tmpCoord, Color.SteelBlue);
- List<P3D> rtn = new List<P3D>() { grb };
- while (iter < Iteration)
- {
- iter++;
- if (skip.Contains(tmpCoord)) continue;
- var lo = LookOut(tmpCoord, visibility);
- if (lo != null&& lo.Count>0)
- {
- lo = lo.OrderBy(x => x[2]).ToList();
- if (lo.First()[2] > actualZ)
- {
- MessageBox.Show("Stuck");
- break;
- }
- }
- foreach (var it in lo)
- {
- if (skip.Contains(it)) continue;
- actualZ = it[2];
- rtn.Add(new P3D(it));
- skip.Add(tmpCoord);
- tmpCoord = it;
- break;
- }
- }
- var lst = rtn.Last();
- lst.Color = Color.IndianRed;
- return rtn;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement