-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
187 lines (163 loc) · 6.83 KB
/
Program.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
namespace GardenSolver
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
PlantTypeLibrary ptl = new PlantTypeLibrary();
Planter planter1 = new Planter(NutritionRequirementsEnum.LOW);
planter1.SetChoosenPlantTypes(new List<string>() { "Buschbohne", "Erbse", "Knoblauch", "Schnittlauch", "Petersilie", "Rosmarin", "Basilikum", "Thymian" });
Planter planter2 = new Planter(NutritionRequirementsEnum.MEDIUM);
planter2.SetChoosenPlantTypes(new List<string>() { "Möhre", "Salat", "Tomatillo", "Rote Bete", "Spinat", "Kohlrabi", "Zwiebel" });
Planter planter3 = new Planter(NutritionRequirementsEnum.HIGH);
planter3.SetChoosenPlantTypes(new List<string>() { "Gurke", "Kartoffel", "Mais", "Paprika", "Tomate", "Zucchini", "Aubergine", "Kohl", "Lauch" /*, "Süßkartoffel" */ });
ApplicationConfiguration.Initialize();
SATest sATest = new SATest();
Application.Run(sATest);
}
public static SizeF PlanterTest = new(5, 1.2f);
public static Graph mainGraph = new();
public static void AddEdges(ref Graph graph, List<Edge> edges)
{
foreach (var edge in edges)
{
graph.edges.Add(edge);
if (!graph.nodes.Contains(edge.plant1))
{
graph.nodes.Add(edge.plant1);
}
if (!graph.nodes.Contains(edge.plant2))
{
graph.nodes.Add(edge.plant2);
}
}
}
public static void AddNodes(ref Graph graph, List<Node> nodes)
{
foreach (var node in nodes)
{
if (!graph.nodes.Contains(node))
{
graph.nodes.Add(node);
}
}
}
public static void NormalizeSizes(float totalSize, ref Graph graph)
{
float SumOfNodes = 0;
for (int i = 0; i < graph.nodes.Count; i++)
{
SumOfNodes += graph.nodes[i].size;
}
//float SumOfEdges = 0;
//for (int i = 0; i < graph.edges.Count; i++)
//{
// SumOfEdges += graph.edges[i].weight;
//}
for (int i = 0; i < graph.nodes.Count; i++)
{
Node node = graph.nodes[i];
node.size = node.size / SumOfNodes * totalSize;
}
}
public static void InitSolve(ref Graph graph)
{
for (int i = 0; i < graph.nodes.Count; i++)
{
Node node = graph.nodes[i];
node.Pos = new PointF((float)Random.Shared.NextDouble() * (float)PlanterTest.Width, (float)Random.Shared.NextDouble() * (float)PlanterTest.Height);
}
}
public static void Solve(ref Graph graph)
{
PointF[] newPositions = new PointF[graph.nodes.Count];
for (int i = 0; i < graph.nodes.Count; i++)
{
newPositions[i] = graph.nodes[i].Pos;
}
for (int i = 0; i < graph.edges.Count; i++)
{
Edge edge = graph.edges[i];
var diff = edge.plant2.Pos.sub(edge.plant1.Pos);
var node1Index = graph.nodes.IndexOf(edge.plant1);
var node2Index = graph.nodes.IndexOf(edge.plant2);
float spacer = 1 / MathF.Sqrt(diff.mag());
spacer = 0.01f;
newPositions[node1Index] = newPositions[node1Index].add(diff.mul(edge.weight * spacer));
newPositions[node2Index] = newPositions[node2Index].add(diff.mul(edge.weight * -spacer));
}
for (int i = 0; i < graph.nodes.Count; i++)
{
Node node = graph.nodes[i];
if (i == graph.nodes.Count - 1)
{
continue;
}
for (int j = i + 1; j < graph.nodes.Count; j++)
{
Node node2 = graph.nodes[j];
PointF diff1 = node2.Pos.sub(node.Pos);
if (diff1.mag() < 2)
{
float spacer = 1f / MathF.Pow(diff1.mag(), 2f);
spacer /= 110;
newPositions[i] = newPositions[i].add(diff1.mul(-spacer));
newPositions[j] = newPositions[j].add(diff1.mul(spacer));
}
}
}
for (int i = 0; i < graph.nodes.Count; i++)
{
graph.nodes[i].Pos = newPositions[i];
}
}
public static PointF add(this PointF a, PointF b) => new(a.X + b.X, a.Y + b.Y);
public static PointF sub(this PointF a, PointF b) => new(a.X - b.X, a.Y - b.Y);
public static PointF mul(this PointF a, PointF b) => new(a.X * b.X, a.Y * b.Y);
public static PointF div(this PointF a, PointF b) => new(a.X / b.X, a.Y / b.Y);
public static PointF mul(this PointF a, float b) => new(a.X * b, a.Y * b);
public static PointF div(this PointF a, float b) => new(a.X / b, a.Y / b);
public static PointF add(this PointF a, float b) => new(a.X + b, a.Y + b);
public static PointF sub(this PointF a, float b) => new(a.X - b, a.Y - b);
public static float mag(this PointF a) => MathF.Sqrt(a.X * a.X + a.Y * a.Y);
public class Edge
{
public Node plant1;
public Node plant2;
public float weight; // positive: plant 1 is good for plant 2
public Edge(Node plant1, Node plant2, float weight)
{
this.plant1 = plant1;
this.plant2 = plant2;
this.weight = weight;
}
}
public class Node
{
public float size = 1;
public string name;
private PointF _pos = new(0, 0);
public Node(string name)
{
this.name = name;
}
public PointF Pos { get { return _pos; } set { _pos = new(MathF.Max(0, MathF.Min(PlanterTest.Width, value.X)), MathF.Max(0, MathF.Min(PlanterTest.Height, value.Y))); } }
public override string ToString() => name + " {" + Pos.X + ";" + Pos.Y + "}";
}
public class Graph
{
public string name = string.Empty;
public List<Node> nodes = [];
public List<Edge> edges = [];
public Graph()
{
}
}
}
}