Skip to content

Commit 9d98376

Browse files
committed
Merged IBPSA with correction for kMeans and for wind cut-out speed
1 parent e2b490a commit 9d98376

6 files changed

Lines changed: 57 additions & 25 deletions

File tree

Buildings/Electrical/BaseClasses/WindTurbine/PartialWindTurbine.mo

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ protected
1515
Modelica.Blocks.Tables.CombiTable1Ds per(
1616
final tableOnFile=tableOnFile,
1717
final table=cat(1, cat(1, [0, 0], table),
18-
[vOut+10*Modelica.Constants.eps, 0;
19-
vOut+20*Modelica.Constants.eps, 0]),
18+
[vOut+1E-9, 0;
19+
vOut+2E-9, 0]),
2020
final tableName=tableName,
2121
final fileName=fileName,
2222
final columns=2:2,
@@ -170,10 +170,15 @@ Below and above these wind speeds, the generated power is zero.
170170
</html>", revisions="<html>
171171
<ul>
172172
<li>
173+
April 9, 2025, by Michael Wetter:<br/>
174+
Increased small number to extend table beyond cut-out wind speed.<br/>
175+
<a href=\"https://github.com/lbl-srg/modelica-Buildings/issues/1996\">IBPSA, issue 1996</a>.
176+
</li>
177+
<li>
173178
March 1, 2016, by Michael Wetter:<br/>
174179
Removed test for equality of <code>Real</code> variables.
175180
This is for
176-
<a href=\"https://github.com/lbl-srg/modelica-Buildings/issues/493\">issue 493</a>.
181+
<a href=\"https://github.com/lbl-srg/modelica-buildings/issues/493\">issue 493</a>.
177182
</li>
178183
<li>
179184
January 10, 2013, by Michael Wetter:<br/>

Buildings/Fluid/Geothermal/Borefields/BaseClasses/HeatTransfer/ThermalResponseFactors/Validation/ClusterBoreholes_100boreholes.mo

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ model ClusterBoreholes_100boreholes
1515
parameter Integer cluSiz[k](each fixed=false) "Size of the clusters";
1616

1717
parameter Integer labelsExp[nBor]=
18-
{3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 4, 4, 2, 2, 2, 2, 4, 4, 3, 3, 4, 2, 2, 1,
19-
1, 2, 2, 4, 3, 4, 2, 2, 1, 1, 1, 1, 2, 2, 4, 4, 2, 1, 1, 1, 1, 1, 1, 2, 4,
20-
4, 2, 1, 1, 1, 1, 1, 1, 2, 4, 4, 2, 2, 1, 1, 1, 1, 2, 2, 4, 3, 4, 2, 2, 1,
21-
1, 2, 2, 4, 3, 3, 4, 4, 2, 2, 2, 2, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3}
18+
{1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 4, 4, 4, 4, 4, 4, 2, 1, 2, 4, 4, 3,
19+
3, 3, 3, 4, 4, 2, 2, 4, 3, 3, 3, 3, 3, 3, 4, 2, 2, 4, 3, 3, 3, 3, 3, 3,
20+
4, 2, 2, 4, 3, 3, 3, 3, 3, 3, 4, 2, 2, 4, 3, 3, 3, 3, 3, 3, 4, 2, 2, 4,
21+
4, 3, 3, 3, 3, 4, 4, 2, 1, 2, 4, 4, 4, 4, 4, 4, 2, 1, 1, 1, 2, 2, 2, 2,
22+
2, 2, 1, 1}
2223
"Expected cluster labels";
2324

2425
// Comparison result

Buildings/UsersGuide/ReleaseNotes/Version_12_0_1.mo

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,25 @@ have been <b style=\"color:blue\">improved</b> in a
4040
<b style=\"color:blue\">backward compatible</b> way:
4141
</p>
4242
<table class=\"releaseTable\" summary=\"summary\" border=\"1\" cellspacing=\"0\" cellpadding=\"2\" style=\"border-collapse:collapse;\">
43-
<tr><td colspan=\"2\"><b>xxx</b>
43+
<tr><td colspan=\"2\"><b>Buildings.Fluid.Geothermal</b><br/>
44+
Buildings.Utilities.Clustering
4445
</td>
4546
</tr>
46-
<tr><td valign=\"top\">xxx
47+
<tr><td colspan=\"2\"><b>Buildings.Electrical</b>
4748
</td>
48-
<td valign=\"top\">xxx.
49+
</tr>
50+
<tr><td valign=\"top\">Buildings.Electrical.BaseClasses.WindTurbine.PartialWindTurbine
51+
</td>
52+
<td valign=\"top\">Changed model to avoid a rounding error that occurs due to the revised definition of <ocde>eps</code>
53+
in the development version of the Modelica Standard Library 4.1.0.<br/>
54+
This is for <a href=\"https://github.com/ibpsa/modelica-ibpsa/issues/1996\">IBPSA, #1996</a>.
55+
</td>
56+
</tr>
57+
<tr><td valign=\"top\">Buildings.Utilities.Clustering.KMeans
58+
</td>
59+
<td valign=\"top\">Improved code to avoid an error during initialization of certain borefield geometries.<br/>
60+
This is for <a href=\"https://github.com/ibpsa/modelica-ibpsa/issues/1985\">IBPSA, #1985</a> and
61+
<a href=\"https://github.com/ibpsa/modelica-ibpsa/issues/1976\">IBPSA, #1976</a>.
4962
</td>
5063
</tr>
5164
<tr><td colspan=\"2\"><b>xxx</b>
@@ -113,4 +126,4 @@ xxx
113126
</li>
114127
</ul>
115128
</html>"));
116-
end Version_12_0_1;
129+
end Version_12_0_1;

Buildings/Utilities/Clustering/KMeans.mo

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ impure function KMeans "k-means clustering algorithm"
66
input Integer n_samples "Number of samples";
77
input Integer n_features "Number of features";
88
input Real relTol=1e-5 "Relative tolerance on cluster positions";
9+
input Real absTol=1e-8 "Absolute tolerance on cluster positions";
910
input Integer max_iter=500 "Maximum number of k-means iterations";
1011
input Integer n_init=10 "Number of runs with randomized centroid seeds";
1112
input Integer n_cluster_size=0 "Length of the cluster_size output vector";
@@ -16,7 +17,8 @@ impure function KMeans "k-means clustering algorithm"
1617
protected
1718
Real old_centroids[n_clusters,n_features] "Previous iteration centroids";
1819
Real new_centroids[n_clusters,n_features] "Next iteration centroids";
19-
Real delta_centroids "Maximum relative displacement of cluster centroids between two k-means iterations";
20+
Real relDelta_centroids "Maximum relative displacement of cluster centroids between two k-means iterations";
21+
Real absDelta_centroids "Maximum absolute displacement of cluster centroids between two k-means iterations";
2022
Integer new_labels[n_samples] "Next iteration cluster labels";
2123
Real new_inertia "Inertia of the samples during the current run";
2224
Real inertia "Minimum inertia of the samples since first run";
@@ -31,30 +33,30 @@ algorithm
3133
id := Modelica.Math.Random.Utilities.initializeImpureRandom(seed);
3234

3335
// ---- Perform n_init successive runs of the k-means algorithm
34-
for run in 1:n_init loop
36+
inertia := Modelica.Constants.inf;
37+
for run in 1:n_init loop
3538
// ---- Select initial centroids at random
3639
// Select 3 non-repeated data points in the data set
3740
n := Modelica.Math.Random.Utilities.impureRandomInteger(id,1,n_samples);
3841
old_centroids[1,:] := data[n,:];
3942
for i in 2:n_clusters loop
40-
n := Modelica.Math.Random.Utilities.impureRandomInteger(id,1,n_samples);
41-
old_centroids[i,:] := data[n,:];
42-
min_dis := Modelica.Math.Vectors.norm(old_centroids[i,:]-old_centroids[1,:], p=2)^2;
43+
min_dis := 0;
4344
while min_dis < Modelica.Constants.eps loop
4445
n := Modelica.Math.Random.Utilities.impureRandomInteger(id,1,n_samples);
4546
old_centroids[i,:] := data[n,:];
4647
min_dis := Modelica.Math.Vectors.norm(old_centroids[i,:]-old_centroids[1,:], p=2)^2;
47-
for j in 1:i-1 loop
48-
dis := Modelica.Math.Vectors.norm(old_centroids[j,:]-old_centroids[i,:], p=2)^2;
48+
for j in 2:i-1 loop
49+
dis := Modelica.Math.Vectors.norm(old_centroids[i,:]-old_centroids[j,:], p=2)^2;
4950
min_dis := min(dis, min_dis);
5051
end for;
5152
end while;
5253
end for;
5354

5455
// ---- k-means iterations
5556
k_iter := 0;
56-
delta_centroids := 2*relTol;
57-
while k_iter < max_iter and delta_centroids > relTol loop
57+
relDelta_centroids := 2*relTol;
58+
absDelta_centroids := 2*absTol;
59+
while k_iter < max_iter and (relDelta_centroids > relTol or absDelta_centroids > absTol) loop
5860
k_iter := k_iter + 1;
5961

6062
// Find centroid closest to each data point
@@ -71,7 +73,8 @@ algorithm
7173
end for;
7274

7375
// Re-evaluate position of the centroids
74-
delta_centroids := 0;
76+
relDelta_centroids := 0;
77+
absDelta_centroids := 0;
7578
for j in 1:n_clusters loop
7679
n := sum(if new_labels[i]==j then 1 else 0 for i in 1:n_samples);
7780
new_centroids[j,:] := zeros(n_features);
@@ -84,7 +87,8 @@ algorithm
8487
else
8588
new_centroids[j,:] := old_centroids[j,:];
8689
end if;
87-
delta_centroids := max(delta_centroids, sum((new_centroids[j,:] - old_centroids[j,:])./old_centroids[j,:]));
90+
relDelta_centroids := max(relDelta_centroids, sum(abs(new_centroids[j,:] - old_centroids[j,:]) ./ (abs(old_centroids[j,:]) .+ Modelica.Constants.eps)));
91+
absDelta_centroids := max(absDelta_centroids, sum(abs(new_centroids[j,:] - old_centroids[j,:])));
8892
end for;
8993
old_centroids := new_centroids;
9094
end while;
@@ -93,7 +97,7 @@ algorithm
9397
new_inertia := 0;
9498
for i in 1:n_samples loop
9599
dis := Modelica.Math.Vectors.norm(data[i,:]-centroids[new_labels[i],:], p=2)^2;
96-
new_inertia := inertia + dis;
100+
new_inertia := new_inertia + dis;
97101
end for;
98102

99103
// Keep run results if inertia is minimum
@@ -129,6 +133,15 @@ modifying the constant <code>seed</code>.
129133
</html>", revisions="<html>
130134
<ul>
131135
<li>
136+
March 18, 2025 by Massimo Cimmino<br/>
137+
Added absolute tolerance. The algorithm stops when any of the relative and
138+
absolute tolerances is satisfied. This fixes errors that occur when a centroid
139+
has a value close to zero on any of its axes. See
140+
<a href=\"https://github.com/ibpsa/modelica-ibpsa/issues/1985\">#1985</a>.
141+
Fixed the initial selection of centroids to avoid repeated centroids. See also
142+
<a href=\"https://github.com/ibpsa/modelica-ibpsa/issues/1976\">#1976</a>.
143+
</li>
144+
<li>
132145
February 1, 2023, by Michael Wetter:<br/>
133146
Added <code>impure</code> declaration which is needed for compliance with the Modelica Language Specification,
134147
and is required by Optimica.

Buildings/Utilities/Clustering/Validation/KMeans_1d.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ model KMeans_1d "Model that verifies the k-means clustering function for 1d data
1515
"Size of the clusters";
1616

1717
parameter Integer labelsExp[nDat]=
18-
{2,2,2,1,3}
18+
{3,2,2,1,1}
1919
"Expected cluster labels";
2020

2121
// Comparison result

Buildings/Utilities/Clustering/Validation/KMeans_2d.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ model KMeans_2d "Model that verifies the k-means clustering function for 2d data
2222
"Size of the clusters";
2323

2424
parameter Integer labelsExp[nDat]=
25-
{2,2,1,2,1,3}
25+
{3,3,2,2,1,1}
2626
"Expected cluster labels";
2727

2828
// Comparison result

0 commit comments

Comments
 (0)