-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.cs
194 lines (168 loc) · 4.8 KB
/
Helpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using Marmot;
using Rhino.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
namespace marmot
{
internal class Helpers
{
internal class Dissection
{
public List<int> Nodes { get; set; }
public List<List<int>> Edges { get; set; }
public List<List<List<int>>> Rooms { get; set; }
}
internal static double ConstraintProportion(double a, double b)
{
// disincentivize long, narrow rooms
if (a == 0) a = 0.01;
if (b == 0) b = 0.01;
return Math.Max(a, b) / Math.Min(a, b) - 2;
}
internal static double ConstraintDistanceRoomToPoint(Graph graph, List<string> rooms, List<Point3d> points, List<double> xDims, List<double> yDims)
{
// Finds distances of room centers and preferred positions
double distance = 0;
for (int i = 0; i < rooms.Count; i++)
{
int fixedRoomIndex = graph.Nodes.IndexOf(rooms[i]);
var roomSpacing = graph.Rooms[fixedRoomIndex];
Point3d point = points[i];
double xCentreRoom = (xDims.GetRange(0, roomSpacing.Item1[0]).Sum() + xDims.GetRange(0, roomSpacing.Item1.Last() + 1).Sum()) / 2;
double yCentreRoom = (yDims.GetRange(0, roomSpacing.Item2[0]).Sum() + yDims.GetRange(0, roomSpacing.Item2.Last() + 1).Sum()) / 2;
distance += Math.Pow(Math.Sqrt(Math.Pow(Math.Abs(xCentreRoom - point.X), 2) + Math.Pow(Math.Abs(yCentreRoom - point.Y), 2)), 2);
}
return distance;
}
internal static Tuple<List<double>, List<double>> CalculateSpacing(
double[] zVals,
int xLen,
int yLen,
double width,
double height
)
{
// Split values in x and y distances
List<double> xVals = zVals.Take(xLen).ToList();
List<double> yVals = zVals.Skip(xLen).Take(yLen).ToList();
double totalX = xVals.Sum();
double totalY = yVals.Sum();
xVals = xVals.Select(x => width * x / totalX).ToList();
yVals = yVals.Select(y => height * y / totalY).ToList();
return new Tuple<List<double>, List<double>>(xVals, yVals);
}
public static Tuple<List<double>, double> NelderMead(
Func<List<double>, double> f,
List<double> xStart,
double step = 0.1,
double noImproveThr = 10E-6,
int noImprovBreak = 10,
int maxIter = 0,
double alpha = 1.0,
double gamma = 2.0,
double rho = -0.5,
double sigma = 0.5,
double prevBestScore = 0)
{
int dim = xStart.Count;
double prevBest = f(xStart);
int noImprov = 0;
List<List<double>> res = new List<List<double>>();
List<double> temp;
for (int i = 0; i <= dim; i++)
{
if (i == 0)
{
temp = new List<double>(xStart);
temp.Add(prevBest);
res.Add(temp);
}
else
{
temp = new List<double>(xStart);
temp[i - 1] += step;
double score = f(temp);
temp.Add(score);
res.Add(temp);
}
}
int iter = 0;
while (true)
{
res = res.OrderBy(x => x.Last()).ToList();
double best = res[0].Last();
if (prevBestScore != 0 && iter >= noImprovBreak && best >= prevBestScore)
{
return new Tuple<List<double>, double>(res[0].Take(dim).ToList(), res[0].Last());
}
if (maxIter != 0 && iter >= maxIter)
{
return new Tuple<List<double>, double>(res[0].Take(dim).ToList(), res[0].Last());
}
iter++;
if (best < prevBest - noImproveThr)
{
noImprov = 0;
prevBest = best;
}
else
{
noImprov++;
}
if (noImprov >= noImprovBreak)
{
return new Tuple<List<double>, double>(res[0].Take(dim).ToList(), res[0].Last());
}
List<double> x0 = new List<double>(new double[dim]);
for (int i = 0; i < dim; i++)
{
x0[i] = res.Take(dim).Average(x => x[i]);
}
List<double> xr = x0.Zip(res.Last(), (xi, xLasti) => xi + alpha * (xi - xLasti)).ToList();
double rscore = f(xr);
if (res[0].Last() <= rscore && rscore < res[res.Count - 2].Last())
{
res[res.Count - 1] = xr;
res[res.Count - 1].Add(rscore);
continue;
}
if (rscore < res[0].Last())
{
List<double> xe = x0.Zip(res.Last(), (xi, xLasti) => xi + gamma * (xi - xLasti)).ToList();
double escore = f(xe);
if (escore < rscore)
{
res[res.Count - 1] = xe;
res[res.Count - 1].Add(escore);
continue;
}
else
{
res[res.Count - 1] = xr;
res[res.Count - 1].Add(rscore);
continue;
}
}
List<double> xc = x0.Zip(res.Last(), (xi, xLasti) => xi + rho * (xi - xLasti)).ToList();
double cscore = f(xc);
if (cscore < res.Last().Last())
{
res[res.Count - 1] = xc;
res[res.Count - 1].Add(cscore);
continue;
}
List<double> x1 = res[0].Take(dim).ToList();
List<List<double>> nres = new List<List<double>>();
foreach (List<double> tup in res)
{
List<double> redx = x1.Zip(tup, (x1i, tupi) => x1i + sigma * (tupi - x1i)).ToList();
double score = f(redx);
redx.Add(score);
nres.Add(redx);
}
res = nres;
}
}
}
}