Program.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using Azure;
  2. using HTEX.Test.Service;
  3. using MathNet.Numerics;
  4. using System.Configuration;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Text.RegularExpressions;
  8. using TEAMModelOS.SDK;
  9. using TEAMModelOS.SDK.DI;
  10. using TEAMModelOS.SDK.Extension;
  11. using TEAMModelOS.SDK.Models;
  12. using static HTEX.Test.Controllers.LessonRecordController;
  13. namespace HTEX.Test
  14. {
  15. public class Program
  16. {
  17. public static void Main(string[] args)
  18. {
  19. Test();
  20. var builder = WebApplication.CreateBuilder(args);
  21. // Add services to the container.
  22. builder.Services.AddControllers();
  23. builder.Services.AddAzureStorage(builder.Configuration.GetValue<string>("Azure:Storage:ConnectionString"));
  24. builder.Services.AddAzureRedis(builder.Configuration.GetValue<string>("Azure:Redis:ConnectionString"));
  25. builder.Services.AddAzureCosmos(builder.Configuration.GetValue<string>("Azure:Cosmos:ConnectionString"));
  26. builder.Services.AddAzureServiceBus(builder.Configuration.GetValue<string>("Azure:ServiceBus:ConnectionString"));
  27. builder.Services.AddAzureSignalR(builder.Configuration.GetValue<string>("Azure:SignalR:ConnectionString"));
  28. builder.Services.AddSnowflakeId(Convert.ToInt64(builder.Configuration.GetValue<string>("Option:LocationNum")), 1);
  29. builder.Services.AddHttpClient();
  30. var app = builder.Build();
  31. // Configure the HTTP request pipeline.
  32. app.UseHttpsRedirection();
  33. app.UseAuthorization();
  34. app.MapControllers();
  35. app.Run();
  36. }
  37. public static string Test2()
  38. {
  39. // 创建一个包含三万个元素的数组
  40. int[] data = Enumerable.Range(1, 30000).Select(i => new Random().Next(1, 31)).ToArray();
  41. // 设置聚类的数量
  42. int k = 2;
  43. // 初始化质心,可以选择数据中的k个随机点作为初始质心
  44. List<int> centroids = new List<int>();
  45. Random rand = new Random();
  46. for (int i = 0; i < k; i++)
  47. {
  48. centroids.Add(rand.Next(1, 31));
  49. }
  50. centroids.Sort(); // 对质心进行排序以避免重复
  51. Dictionary<int, List<int>> clustersD= new Dictionary<int, List<int>>();
  52. // 迭代次数
  53. int maxIterations = 100;
  54. for (int iteration = 0; iteration < maxIterations; iteration++)
  55. {
  56. // 将数据点分配给最近的质心
  57. Dictionary<int, List<int>> clusters = new Dictionary<int, List<int>>();
  58. foreach (int point in data)
  59. {
  60. int nearestCentroid = centroids.OrderBy(c => Math.Abs(point - c)).First();
  61. if (!clusters.ContainsKey(nearestCentroid))
  62. {
  63. clusters[nearestCentroid] = new List<int>();
  64. }
  65. clusters[nearestCentroid].Add(point);
  66. }
  67. // 更新质心位置
  68. List<int> newCentroids = new List<int>();
  69. foreach (KeyValuePair<int, List<int>> cluster in clusters)
  70. {
  71. int sum = cluster.Value.Sum();
  72. int count = cluster.Value.Count;
  73. int newCentroid = sum / count; // 计算均值
  74. newCentroids.Add(newCentroid);
  75. // 输出当前聚类的信息
  76. Console.WriteLine($"Cluster with centroid {cluster.Key}:");
  77. Console.WriteLine($"Min: {cluster.Value.Min()}, Max: {cluster.Value.Max()}, Average: {sum / count}");
  78. }
  79. // 检查质心是否改变
  80. newCentroids.Sort();
  81. clustersD=clusters;
  82. if (newCentroids.SequenceEqual(centroids))
  83. {
  84. break;
  85. }
  86. centroids = newCentroids;
  87. }
  88. return "";
  89. }
  90. public static string Test()
  91. {
  92. // Test2();
  93. //var n = 2.0;
  94. //var w = 2.0;
  95. //var r = 1.0;
  96. //var p = InteractWeight.TT;
  97. //var j = InteractWeight.T1;
  98. //var data = 190/(1+Math.Exp(-(n/4*(w*w/n+p/j * w/n * r/w *r))/n))-95;
  99. var n = 3.0;
  100. var m = 5.6;
  101. var w = 3.0;
  102. var r = 3.0;
  103. var p = InteractWeight.TT;
  104. var j = InteractWeight.T1;
  105. //var data = 190.0/(1+Math.Exp(-((n/m.avg)*(w*(w/n)+(p/j)* (w/n)* (r/w) *r))/n))-95.0;
  106. var data = 190.0/(1+Math.Exp(-((n/m)*(w*(w/n)+(p/j)* (r/n) *r))/n))-95.0;
  107. //int[] a = new int[] { 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 };
  108. //int [] b = new int[] { 2192, 2379, 2070, 1813, 1662, 1421, 1134, 1043, 861, 763, 608, 563, 451, 358, 291, 252, 213, 170, 152, 133, 132, 95, 82, 71, 73, 63, 43 };
  109. //var lib= b.Sum();
  110. //List<int > ds= new List<int>();
  111. //for(int i = 0; i<a.Length; i++)
  112. //{
  113. // int d= b[i]* a[i];
  114. // ds.Add(d);
  115. //}
  116. //var q = ds.Sum() *1.0/lib;
  117. //Console.WriteLine(q);
  118. // var per= GetPersent(array,3);
  119. // Console.WriteLine(per);
  120. //var data = MLService.GetNormalCluster(array.ToArray(),3,0.3);
  121. // var group = array.GroupBy(x=>x).Select(x=>new { key = x.Key, list = x.ToList()}).OrderBy(x=>x.key);
  122. // Console.WriteLine(group.Select(x => x.key).ToJsonString());
  123. //Console.WriteLine(group.Select(x => x.list.Count()).ToJsonString());
  124. //int count = 10;
  125. //int score = 10;
  126. //for (int i = 0; i < 10; i++)
  127. //{
  128. // int work = Random.Shared.Next(1, count);
  129. // int right = Random.Shared.Next(1, work);
  130. // double xIRS = (work+right);
  131. // var exsIRS = 150/(1+Math.Exp(-(xIRS/2)))-75;
  132. // Console.WriteLine($"{i}=》答题 {work}-----{right}: {exsIRS}");
  133. // double xPIC = (work+right*3);
  134. // var exsPIC = 40/(1+Math.Exp(-(xPIC/5)))-20;
  135. // Console.WriteLine($"{i}=》挑人 {work}-----{right}: {exsPIC}");
  136. // double xBUS = (work+right*3+(work-right));
  137. // var exsBUZ = 80/(1+Math.Exp(-(xBUS/3)))-40;
  138. // Console.WriteLine($"{i}=》抢权 {work}-----{right}: {exsBUZ}");
  139. // int pscr = Random.Shared.Next(1, 10)*10;
  140. // int gscr = Random.Shared.Next(1, 10)*10;
  141. // double xSCR = (pscr + gscr );
  142. // var exsSCR= 60/(1+Math.Exp(-(xSCR/8)))-30;
  143. // Console.WriteLine($"{i}=》计分 {pscr}-----{gscr}: {exsSCR}");
  144. // double xWRK = (work);
  145. // var exsWRK = 150/(1+Math.Exp(-(xWRK)))-75;
  146. // Console.WriteLine($"{i}=》作品 {work}-----{right}: {exsBUZ}");
  147. // int vt = Random.Shared.Next(1, 3);
  148. // int sc = Random.Shared.Next(1, 3);
  149. // int cm = Random.Shared.Next(1, 3);
  150. // int pt = Random.Shared.Next(1, 3);
  151. // double xCMT = (vt + sc+cm*2+pt);
  152. // var exsCMT = 100/(1+Math.Exp(-(xSCR/8)))-50;
  153. // Console.WriteLine($"{i}=》评分 {vt}-----{sc}---{cm}---{pt}: {exsCMT}");
  154. // int b = Random.Shared.Next(1, 100)/20;
  155. // int m = Random.Shared.Next(1, 100)/20;
  156. // int g = Random.Shared.Next(1, 10)/2;
  157. // int t = Random.Shared.Next(1, 10)/2;
  158. // double xXZ = (b + m+g+t);
  159. // var exsXZ= 150/(1+Math.Exp(-(xSCR/8)))-75;
  160. // Console.WriteLine($"{i}=》协作 {b}-----{m}---{g}---{t}: {exsXZ}");
  161. // var xALL = xXZ+xCMT+xWRK+xSCR+xBUS+exsPIC+xIRS;
  162. // var exsALL = 190/(1+Math.Exp(-(xALL/25)))-95;
  163. // Console.WriteLine($"{i}=》所有: {exsALL}");
  164. //}
  165. //List<int> nums = new List<int>();
  166. //for (int i = 0; i < 100; i++)
  167. //{
  168. // nums.Add(Random.Shared.Next(1,5));
  169. //}
  170. //for (int i = 0; i < 100; i++)
  171. //{
  172. // nums.Add(Random.Shared.Next(4, 8));
  173. //}
  174. //for (int i = 0; i < 100; i++)
  175. //{
  176. // nums.Add(Random.Shared.Next(5, 20));
  177. //}
  178. //for (int i = 0; i < 50; i++)
  179. //{
  180. // nums.Add(Random.Shared.Next(15, 36));
  181. //}
  182. //var per = GetPersent(nums, 11);
  183. //bool[] anomalies = MarkAnomalies(nums);
  184. //for (int i = 0; i < nums.Count; i++)
  185. //{
  186. // if (anomalies[i]) { Console.WriteLine($"Number{ i}: {nums[i]} is an anomaly."); }
  187. //}
  188. return "Hello World!";
  189. }
  190. static bool[] MarkAnomalies(List<int> array)
  191. {
  192. if (array.Count == 0) return new bool[0];
  193. double average = array.Average();
  194. double variance = array.Average(x => Math.Pow(x - average, 2));
  195. double standardDeviation = Math.Sqrt(variance);
  196. // 定义异常值的阈值,这里使用2倍标准差
  197. double threshold =3* standardDeviation;
  198. bool[] anomalies = new bool[array.Count];
  199. for (int i = 0; i < array.Count; i++)
  200. {
  201. double deviation = Math.Abs(array[i] - average);
  202. anomalies[i] = deviation > threshold;
  203. }
  204. return anomalies;
  205. }
  206. /// <summary>
  207. /// 计算当前元素在集合中超过了多少百分比的值
  208. /// </summary>
  209. /// <param name="nums"></param>
  210. /// <param name="curr"></param>
  211. /// <returns></returns>
  212. public static double GetPersent(IEnumerable<double> nums, int curr)
  213. {
  214. int count = 0;
  215. foreach (var op in nums.OrderBy(x => x))
  216. {
  217. if (op < curr)
  218. {
  219. count++;
  220. }
  221. else if (op == curr)
  222. {
  223. count++;
  224. }
  225. else
  226. {
  227. break;
  228. }
  229. }
  230. return count *1.0/ nums.Count() * 100;
  231. }
  232. public class KeyCount
  233. {
  234. public int count { get; set;}
  235. public int key { get; set;}
  236. }
  237. }
  238. }