Advertisement
Guest User

Untitled

a guest
Mar 28th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using ZedGraph;
  9. namespace ISE_Z1
  10. {
  11. public partial class Form1 : Form
  12. {
  13. //lista z opisem współczynników funkcji dopasowywanych
  14. public List<string> population = new List<string>();
  15. //lista z przebiegiem funkcji dopasowywanych
  16. public List<PointPairList> populationList = new List<PointPairList>();
  17. //kolory poszczególnych przebiegów
  18. public List<Color> populationColor = new List<Color>();
  19.  
  20. public Form1()
  21. {
  22. InitializeComponent();
  23. graph.GraphPane.Title.IsVisible = false;
  24. CreateBasicFunction();
  25. }
  26.  
  27. /// <summary>
  28. /// Inicjalizuje funkcje
  29. /// </summary>
  30. private void CreateBasicFunction()
  31. {
  32. population.Add( "0,05 0,04 0,1 2 0,03, 0,0002" );
  33. population.Add( "0,010 0,0014 0,0003 0,0006 0,00005 0,55" );
  34. population.Add( "0,004 0,00002 0,000008 0,00000055 1 0,00088" );
  35. population.Add( "0,00003 0,02 0,008 0,009 0,00003 0,000000001" );
  36. population.Add( "0,0000000006 0,000000001 0,008 0,009 0,00003 0,55" );
  37. population.Add( "0,0000000003 0,0000000002 0,008 0,009 0,00003 0,1 " );
  38.  
  39. populationColor.Add( Color.HotPink );
  40. populationColor.Add( Color.IndianRed );
  41. populationColor.Add( Color.Khaki );
  42. populationColor.Add( Color.LightBlue );
  43. populationColor.Add( Color.Gray );
  44. populationColor.Add( Color.Gold );
  45. populationColor.Add( Color.DarkGreen );
  46. populationColor.Add( Color.Blue );
  47. populationColor.Add( Color.DarkSlateBlue );
  48. populationColor.Add( Color.Orange );
  49.  
  50. }
  51.  
  52. //ilość punktów na osi x
  53. int pointAmount = 1000;
  54. //generator liczb pseudolosowych
  55. Random rand = new Random( 1000 );
  56. //lista punktów przebiegu do którego dopasowujemy
  57. PointPairList ppl = new PointPairList();
  58.  
  59. //tworzy funkcję do której dopasowujemy
  60. private void buttonCreateFunction_Click( object sender, EventArgs e )
  61. {
  62. ppl.Clear();
  63. double previousValue = 0;
  64. double multip = 3000000000;
  65. int direction = 1;
  66. for ( int i = 0; i < pointAmount; i++ )
  67. {
  68. direction = ( rand.NextDouble() < 0.5 ) ? 1 : -1;
  69. previousValue += direction * multip * rand.NextDouble();
  70. PointPair pp = new PointPair( i, previousValue );
  71. ppl.Add( pp );
  72. }
  73. graph.GraphPane.CurveList.Clear();
  74. graph.GraphPane.AddCurve( "Funkcja", ppl, Color.Red, SymbolType.None );
  75. RefreshGraph();
  76. }
  77.  
  78. private void buttonRun_Click( object sender, EventArgs e )
  79. {
  80. if ( !backgroundWorker.IsBusy )
  81. {
  82. backgroundWorker.RunWorkerAsync();
  83. }
  84. else
  85. {
  86. backgroundWorker.CancelAsync();
  87. }
  88. }
  89.  
  90. private void backgroundWorker_DoWork( object sender, DoWorkEventArgs e )
  91. {
  92. //wyczyść wykres
  93. graph.GraphPane.CurveList.Clear();
  94. //dodaj przebieg funkcji do której dopasowujemy
  95. graph.GraphPane.AddCurve( "Wykres", ppl, Color.Red, SymbolType.None );
  96. CreateGeneration();
  97. //dodaj wykresy fukcji dopasowywanych
  98. for ( int i = 0; i < population.Count; i++ )
  99. {
  100. //pobierz współczynniki funkji
  101. string[] coefficientString = population[ i ].Split( new string[] { " " }, StringSplitOptions.RemoveEmptyEntries );
  102. List<double> coefficient = new List<double>();
  103. for ( int j = 0; j < coefficientString.Length; j++ )
  104. {
  105. coefficient.Add( Convert.ToDouble( coefficientString[ j ] ) );
  106. }
  107.  
  108. PointPairList popPairList = new PointPairList();
  109. //stwórz przebieg
  110. for ( int j = 0; j < pointAmount; j++ )
  111. {
  112. popPairList.Add( j, GetYValue( coefficient, j ) );
  113. }
  114. //dodaj do listy
  115. graph.GraphPane.AddCurve( i.ToString(), popPairList, populationColor[ i ], SymbolType.None );
  116. }
  117.  
  118. if ( ( (BackgroundWorker)sender ).CancellationPending )
  119. {
  120. e.Cancel = true;
  121. return;
  122. }
  123. //odśwież
  124. RefreshGraph();
  125. }
  126.  
  127. private void CreateGeneration()
  128. {
  129. List<string> newPopulationList = new List<string>();
  130. List<List<double>> os = new List<List<double>>();
  131. //TODO wypełnić
  132.  
  133. for (int l = 0; l < 30; l++)
  134. {
  135. string[] ele = population[rand.Next(population.Count)].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
  136.  
  137. List<double> dd1 = new List<double>();
  138. List<double> dd2 = new List<double>();
  139.  
  140. for (int i = 0; i < ele.Length; i++)
  141. {
  142. dd1.Add(Double.Parse(ele[i]));
  143. }
  144.  
  145.  
  146. ele = population[rand.Next(population.Count)].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
  147. for (int i = 0; i < ele.Length; i++)
  148. {
  149. dd2.Add(Double.Parse(ele[i]));
  150. }
  151.  
  152.  
  153. List<double> nowy = new List<double>();
  154. int podz = rand.Next(dd1.Count);
  155. for (int i = 0; i < podz; i++)
  156. {
  157. nowy.Add(dd1[i]);
  158. }
  159. for (int i = 0; i < dd2.Count; i++)
  160. {
  161. nowy.Add(dd2[i]);
  162.  
  163. }
  164. os.Add(nowy);
  165. double wynik=0;
  166. for (int i = 0; i < 100; i++)
  167. {
  168. wynik+= Math.Abs(GetYValue(nowy,i)-graph.GraphPane.CurveList[0][i].Y);
  169.  
  170. }
  171.  
  172.  
  173. }
  174.  
  175. population = newPopulationList;
  176.  
  177. }
  178.  
  179. /// <summary>
  180. /// pobieranie wartości funkcji dla danego punktu,
  181. /// </summary>
  182. /// <param name="coefficient">lista współczynników funkcji</param>
  183. /// <param name="j">punkt</param>
  184. /// <returns></returns>
  185. private double GetYValue(List<double> coefficient, int j)
  186. {
  187. double temp = 0;
  188. for ( int i = 0; i < coefficient.Count; i++ )
  189. {
  190. temp += Math.Pow( j, coefficient.Count - i ) * coefficient[ i ];
  191. }
  192. return temp;
  193. }
  194.  
  195. //odświeżanie wykresu
  196. private void RefreshGraph()
  197. {
  198. if ( graph.InvokeRequired )
  199. {
  200. graph.Invoke( new RefreshGraphDelegate( RefreshGraph ) );
  201. }
  202. else
  203. {
  204. graph.AxisChange();
  205. graph.Invalidate();
  206. }
  207. }
  208. private delegate void RefreshGraphDelegate();
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement