Percentage.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using Island.StandardLib.Storage;
  2. using System;
  3. namespace Island.StandardLib.Math
  4. {
  5. /// <summary>
  6. /// 表示一个总和为1的平衡容器
  7. /// </summary>
  8. public class Percentage : IStorable
  9. {
  10. StorableFixedArray<Key> percentages;
  11. public int KeyLength => percentages.Length;
  12. public float Maxium
  13. {
  14. get
  15. {
  16. float maxium = percentages[0].Value;
  17. for (int i = 1; i < percentages.Count; i++)
  18. if (percentages[i].Value > maxium) maxium = percentages[i].Value;
  19. return maxium;
  20. }
  21. }
  22. public int MaxiumId
  23. {
  24. get
  25. {
  26. float maxium = percentages[0].Value;
  27. int id = 0;
  28. for (int i = 1; i < percentages.Count; i++)
  29. if (percentages[i].Value > maxium)
  30. {
  31. maxium = percentages[i].Value;
  32. id = i;
  33. }
  34. return id;
  35. }
  36. }
  37. public float Minium
  38. {
  39. get
  40. {
  41. float minium = percentages[0].Value;
  42. for (int i = 1; i < percentages.Count; i++)
  43. if (percentages[i].Value < minium) minium = percentages[i].Value;
  44. return minium;
  45. }
  46. }
  47. public int MiniumId
  48. {
  49. get
  50. {
  51. float minium = percentages[0].Value;
  52. int id = 0;
  53. for (int i = 1; i < percentages.Count; i++)
  54. if (percentages[i].Value < minium)
  55. {
  56. minium = percentages[i].Value;
  57. id = i;
  58. }
  59. return id;
  60. }
  61. }
  62. public Percentage() { }
  63. /// <summary>
  64. /// 初始化容器
  65. /// </summary>
  66. /// <param name="sourceValue">初始数据</param>
  67. public Percentage(params float[] sourceValue)
  68. {
  69. percentages = new StorableFixedArray<Key>();
  70. if (sourceValue.Length == 0) throw new Exception();
  71. float val = 0;
  72. for (int i = 0; i < sourceValue.Length; i++)
  73. {
  74. val += sourceValue[i];
  75. percentages.Add(new Key(sourceValue[i]));
  76. }
  77. if (val != 1f)
  78. AdjustBalance();
  79. }
  80. /// <summary>
  81. /// 获取和设置数据。其中,设置数据后会自动按比例平衡容器,保证容器总和为1
  82. /// </summary>
  83. /// <param name="index">数据序号</param>
  84. /// <returns></returns>
  85. public float this[int index]
  86. {
  87. get => percentages[index].Value;
  88. set => AdjustKeep(index, value);
  89. }
  90. public float[] SelectBalance(params int[] selectedIndex)
  91. {
  92. float[] values = new float[selectedIndex.Length];
  93. for (int i = 0; i < selectedIndex.Length; i++)
  94. values[i] = percentages[selectedIndex[i]].Value;
  95. float total = 0f;
  96. for (int i = 0; i < values.Length; i++)
  97. total += values[i];
  98. float px = 1 / total;
  99. for (int i = 0; i < values.Length; i++)
  100. values[i] *= px;
  101. return values;
  102. }
  103. public int Random(int seed = 0)
  104. {
  105. Random rd = seed == 0 ? new Random() : new Random(seed);
  106. double select = rd.NextDouble();
  107. float thisMin = 0f;
  108. for (int i = 0; i < KeyLength; i++)
  109. {
  110. float thisMax = thisMin + this[i];
  111. if (select >= thisMin && select < thisMax)
  112. return i;
  113. thisMin = thisMax;
  114. }
  115. return -1;
  116. }
  117. public long Total(int index, long totalValue)
  118. {
  119. return (long)(this[index] * totalValue);
  120. }
  121. void AdjustBalance()
  122. {
  123. float total = 0f;
  124. for (int i = 0; i < KeyLength; i++)
  125. total += this[i];
  126. float px = 1 / total;
  127. for (int i = 0; i < KeyLength; i++)
  128. percentages[i].Value *= px;
  129. }
  130. void AdjustKeep(int keepIndex, float newValue)
  131. {
  132. AdjustBalance();
  133. float distance = this[keepIndex] - newValue;
  134. percentages[keepIndex].Value = newValue;
  135. float[] gains = new float[KeyLength - 1];
  136. int g = 0;
  137. for (int i = 0; i < KeyLength; i++)
  138. {
  139. if (keepIndex == i)
  140. continue;
  141. gains[g] = this[i]; g++;
  142. }
  143. Percentage p = new Percentage(gains);
  144. g = 0;
  145. for (int i = 0; i < KeyLength; i++)
  146. {
  147. if (keepIndex == i)
  148. continue;
  149. percentages[i].Value += p[g] * distance; g++;
  150. }
  151. }
  152. public override string ToString()
  153. {
  154. string s = "Percentage [";
  155. float total = 0f;
  156. for (int i = 0; i < KeyLength; i++)
  157. {
  158. total += this[i];
  159. if (i == KeyLength - 1)
  160. s += this[i].ToString("P");
  161. else s += this[i].ToString("P") + ", ";
  162. }
  163. return s + "]";
  164. }
  165. public void ReadFromData(DataStorage data)
  166. {
  167. data.Read(out percentages);
  168. }
  169. public void WriteToData(DataStorage data)
  170. {
  171. data.Write(percentages);
  172. }
  173. class Key : IStorable
  174. {
  175. public float Value;
  176. public Key() => Value = 0f;
  177. public Key(float sourceValue) => Value = sourceValue;
  178. public void ReadFromData(DataStorage data) => data.Read(out Value);
  179. public void WriteToData(DataStorage data) => data.Write(Value);
  180. }
  181. }
  182. }