internal double CalEstFitness(EnvironmentVar enVar) { DenseMatrix m = new DenseMatrix((int)enVar.pmProblem["NumOfCE"], _testSizeOfCEx); DenseMatrix mInputVectors = new DenseMatrix(_testSizeOfCEx, _dimension); Vector <double> vCoverPointCounts = Vector.Build.Dense((int)enVar.pmProblem["NumOfCE"]); int estNumOfCEInSamples = 0; int duplicateVectsInSamples = 0; for (int i = 0; i < _testSizeOfCEx; i++) { Vector <double> vRandVect = GetRandVect(enVar); if (mInputVectors.EnumerateRows().Contains(vRandVect)) { duplicateVectsInSamples = duplicateVectsInSamples = 1; } else { mInputVectors.SetRow(i, vRandVect); } double[] coverPoints = null; lock (SUT._locker) { coverPoints = SUT.RunSUT(enVar, vRandVect.ToArray <double>()); } if ((int)coverPoints[ceIndex] == 1) { estNumOfCEInSamples = estNumOfCEInSamples + 1; } } double f1 = estNumOfCEInSamples * 1.0 / (_testSizeOfCEx); //Question: Should I consider Duplicates return(f1); }
private void button1_Click(object sender, EventArgs e) { int numOfRuns = (int)numericUpDown1.Value; chart1.Series.Remove(chart1.Series[0]); Random rnd = new Random(); for (int i = 0; i < numOfRuns; i++) { chart1.Series.Add("Fitness_" + i.ToString()); chart1.Series[i].ChartType = System.Windows.Forms.DataVisualization .Charting.SeriesChartType.Point; chart1.Series[i].Color = Color.FromArgb( rnd.Next(1, 255), rnd.Next(1, 255), rnd.Next(1, 255)); } timer1.Start(); globalWatch.Start(); record = new Record(numOfRuns); EnvironmentVar[] enVars = new EnvironmentVar[numOfRuns]; App frontApp = new App(); frontApp.Use <SUTInitialization>((int)numericUpDown2.Value, record); frontApp.Use <TaskDistributor>(enVars, numOfRuns, record, Convert.ToInt16(textBox2.Text)); frontApp.Use <Monitor>(record, enVars); frontApp.GetAppFunc().Invoke(null); }
internal Vector <double> GetRandVect(EnvironmentVar enVar) { int[] selIndexArry = new int[_dimension]; double[] lowbounds = new double[_dimension]; double[] highbounds = new double[_dimension]; for (int o = 0; o < _dimension; o++) { lowbounds[o] = ((List <Tuple <int, int> >)enVar.pmProblem["Bound"])[o].Item1; highbounds[o] = ((List <Tuple <int, int> >)enVar.pmProblem["Bound"])[o].Item2; } // First, Select a block for (int i = 0; i < _dimension; i++) { double lowIndex = 0.0; double sumOfWeight = weights.ColumnSums()[i]; double rndNum = enVar.rnd.NextDouble() * sumOfWeight; for (int j = 0; j < _numOfParams; j++) { if (rndNum >= lowIndex && rndNum <= lowIndex + weights[j, i]) { selIndexArry[i] = j; break; } lowIndex = lowIndex + weights[j, i]; } } double[] result = new double[_dimension]; for (int i = 0; i < _dimension; i++) { if (selIndexArry[i] == 0) { result[i] = enVar.rnd.Next((int)lowbounds[i], (int)thetaDelta[i, 0]); } else if (selIndexArry[i] == _numOfParams) { double min = thetaDelta.Column(i).Sum() - thetaDelta.Row(_numOfParams - 1).ElementAt(i); result[i] = enVar.rnd.Next((int)min, (int)highbounds[i]); } else { double min = thetaDelta.Column(i) .Select((x, k) => { if (k <= selIndexArry[i] - 1) { return(x); } return(0); }).Sum(); double max = min + thetaDelta.Column(i).ElementAt(selIndexArry[i]); result[i] = enVar.rnd.Next((int)min, (int)max); } } return(Vector.Build.Dense(result)); }
public GAEncoding(int numOfParams, int dimension, EnvironmentVar enVar) { _numOfParams = numOfParams; _dimension = dimension; _enVar = enVar; thetaDelta = Matrix.Build.Dense(numOfParams, dimension); weights = Matrix.Build.Dense(numOfParams + 1, dimension); pathRecord = new Dictionary <string, double>(); }
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (webBrowser.ReadyState == WebBrowserReadyState.Complete && e.Url.ToString() == _pageUrl) { Console.WriteLine(e.Url.ToString()); webBrowser.Document.InvokeScript("setExecModeForSeat"); webBrowser.Document.InvokeScript("setEnvironmentVar", new object[] { EnvironmentVar.ToJSON() }); } }
public async Task <int> Invoke(EnvironmentVar envar) { enVar = envar; dimension = (int)enVar.pmProblem["Dimension"]; await Task.Run(() => MOGA_Start()); await _next(enVar); return(0); }
internal Vector <double> GetRandVect(EnvironmentVar enVar) { int[] selIndexArry = new int[_dimension]; double[] lowbounds = new double[_dimension]; double[] highbounds = new double[_dimension]; for (int o = 0; o < _dimension; o++) { lowbounds[o] = ((List <Tuple <int, int> >)enVar.pmProblem["Bound"])[o].Item1; highbounds[o] = ((List <Tuple <int, int> >)enVar.pmProblem["Bound"])[o].Item2; } // First, Select a block for (int i = 0; i < _dimension; i++) { double lowIndex = 0.0; double sumOfWeight = _numOfParams + 1; double rndNum = enVar.rnd.NextDouble() * sumOfWeight; for (int j = 0; j < _numOfParams; j++) { if (rndNum >= lowIndex && rndNum <= lowIndex + weights[j][i]) { selIndexArry[i] = j; break; } lowIndex = lowIndex + weights[j][i]; } } double[] result = new double[_dimension]; for (int i = 0; i < _dimension; i++) { if (selIndexArry[i] == 0) { result[i] = enVar.rnd.Next((int)lowbounds[i], (int)thetas[0][i]); } else if (selIndexArry[i] == _numOfParams) { result[i] = enVar.rnd.Next((int)thetas[_numOfParams - 1][i], (int)highbounds[i]); } else { result[i] = enVar.rnd.Next((int)thetas[selIndexArry[i] - 1][i], (int)thetas[selIndexArry[i]][i]); } } return(Vector.Build.Dense(result)); }
internal void FixInputs(EnvironmentVar enVar) { Vector <double> low = Vector.Build.Dense( _dimension, (k) => { return(((List <Tuple <int, int> >)enVar.pmProblem["Bound"])[k].Item1); } ); Vector <double> high = Vector.Build.Dense( _dimension, (k) => { return(((List <Tuple <int, int> >)enVar.pmProblem["Bound"])[k].Item2); } ); Vector <double> tmp = (high - low).Add(1).Divide(_numOfParams); for (int i = 0; i < _numOfParams; i++) { thetaDelta.SetRow(i, tmp); } int numOfOne = Convert.ToInt16(Math.Ceiling((_numOfParams + 1) * 0.2)); int numOfNotOne = (_numOfParams - numOfOne) / 2; for (int i = 0; i < _numOfParams + 1; i++) { weights = Matrix.Build.Dense(_numOfParams + 1, _dimension, 1); } int[] cnt = new int[_dimension]; double[] values = new double[2] { 0, 2 }; // Initialize Weights for (int i = 0; i < _dimension; i++) { for (int j = 0; j < _numOfParams + 1; j++) { weights[j, i] = enVar.rnd.NextDouble(); } } }
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (webBrowser.ReadyState == WebBrowserReadyState.Complete && e.Url.ToString() == _pageUrl) { Console.WriteLine(e.Url.ToString()); webBrowser.Document.InvokeScript("setExecModeForSeat"); webBrowser.Document.InvokeScript("setEnvironmentVar", new object[] { EnvironmentVar.ToJSON() }); string strJsonStr = JsonConvert.SerializeObject( new { Emergency = Common.ADDRESS_DICT[Common.ADDRESS_KEY_PROCESS_EMERGENCY], FamilyCalls = Common.ADDRESS_DICT[Common.ADDRESS_KEY_PROCESS_FAMILYCALLS], Life = Common.ADDRESS_DICT[Common.ADDRESS_KEY_PROCESS_LIFE], } ); webBrowser.Document.InvokeScript("setServiceTypeUrl", new object[] { strJsonStr }); webBrowser.Document.InvokeScript("setPageData", new object[] { JsonConvert.SerializeObject(PageData) }); } }
internal void Randomize(EnvironmentVar enVar) { Vector <double> tmp = Vector.Build.Dense( _dimension, (k) => { return(((List <Tuple <int, int> >)enVar.pmProblem["Bound"])[k].Item1); } ); Vector <double> tmpW = Vector.Build.Dense( _dimension, (k) => { return(0); } ); for (int i = 0; i < _numOfParams; i++) { Vector <double> newParam = Vector.Build.Dense(_dimension, (k) => { return(enVar.rnd.Next((int)tmp[k] , (((List <Tuple <int, int> >)enVar.pmProblem["Bound"])[k].Item2))); }); thetas[i] = newParam; tmp = newParam; } for (int i = 0; i < _numOfParams; i++) { Vector <double> newWeight = Vector.Build.Dense(_dimension, (k) => { return(enVar.rnd.Next(0, _numOfParams + 1 - (int)tmpW[k])); }); tmpW = newWeight + tmpW; weights[i] = newWeight; } weights[_numOfParams] = tmpW.SubtractFrom(_numOfParams + 1); }
public async Task <int> Invoke(EnvironmentVar enVar) { Dictionary <string, object> sutParam = SUT.SelectSUT(_selectSUT); _sutPath = (string)sutParam["SUTPath"]; _record.sutInfo = sutParam; _record.InitializeCurrentBest((int)sutParam["NumOfCE"]); //04/21/2017: Since SUT data file is too large, we should use true SUT implementation. // Comment code in below //Task taskInitialSUT = Task.Run(() => { // LocalFileAccess lfa = new LocalFileAccess(); // List<string> list = new List<string>(); // lfa.StoreLinesToList(_sutPath.ToString(), list); // DataTable dt = new DataTable(); // dt.Columns.Add("Input", Type.GetType("System.String")); // for (int i = 0; i < (int)sutParam["NumOfCE"]; i++) // { // dt.Columns.Add((i + 1).ToString(), Type.GetType("System.String")); // } // int numOfInputs = list.First().Count(x=>x == ' '); // foreach (string s in list) // { // int length = dt.Rows.Count; // if (length == 0) // { // int index = 0; // for (int i = 0; i < numOfInputs; i++) // { // index = s.IndexOf(' ',index)+1; // } // int c = Convert.ToInt32(s.Substring(index))+1; // object[] coverSeq = new object[(int)sutParam["NumOfCE"] + 1]; // for (int i = 0; i < coverSeq.Length; i++) // { // coverSeq[i] = 0.0; // } // coverSeq[c] = 1.0; // coverSeq[0] = s.Substring(0, s.LastIndexOf(' ')); // dt.Rows.Add( // coverSeq // ); // } // else if (s.Substring(0, s.IndexOf(' ', s.IndexOf(' ') + 1)) // == dt.Rows[length - 1].ItemArray[0].ToString()) // { // int index = 0; // for (int i = 0; i < numOfInputs; i++) // { // index = s.IndexOf(' ', index) + 1; // } // int c = Convert.ToInt32(s.Substring(index))+1; // dt.Rows[length - 1][c] = 1.0; // } // else // { // int index = 0; // for (int i = 0; i < numOfInputs; i++) // { // index = s.IndexOf(' ', index) + 1; // } // int c = Convert.ToInt32(s.Substring(index))+1; // object[] coverSeq = new object[(int)sutParam["NumOfCE"] + 1]; // for (int i = 0; i < coverSeq.Length; i++) // { // coverSeq[i] = 0.0; // } // coverSeq[c] = 1.0; // coverSeq[0] = s.Substring(0,s.LastIndexOf(' ')); // dt.Rows.Add( // coverSeq // ); // } // } // sutParam["Map"] = dt.AsEnumerable() // .ToDictionary<DataRow, string, double[]>( // row => row.Field<string>(0), // row => // { // double[] coverSeq = new double[(int)sutParam["NumOfCE"]]; // for (int i = 0; i < (int)sutParam["NumOfCE"]; i++) // { // coverSeq[i] = Convert.ToDouble(row.ItemArray[i + 1]); // } // return coverSeq; // }); //}); //await taskInitialSUT; await _next(enVar); Console.WriteLine("All tasks are done"); return(0); }
internal void CalEstFitness(EnvironmentVar enVar) { DenseMatrix m = new DenseMatrix((int)enVar.pmProblem["NumOfCE"], _enVar.testSetSize); DenseMatrix mInputVectors = new DenseMatrix(_enVar.testSetSize, _dimension); DenseMatrix mCoverPairs = new DenseMatrix(((int)_enVar.pmProblem["NumOfCE"] + 1) / 2, 2); //2 outgoing edges per branch Vector <double> vIntersetSet = new DenseVector(((int)_enVar.pmProblem["NumOfCE"] + 1) / 2); duplicateSamples = 0; fitnessPerCE = new DenseVector((int)_enVar.pmProblem["NumOfCE"]); pathRecord.Clear(); for (int i = 0; i < _enVar.testSetSize; i++) { Vector <double> vRandVect = GetRandVect(enVar); if (mInputVectors.EnumerateRows().Contains(vRandVect)) { duplicateSamples = duplicateSamples + 1; } else { mInputVectors.SetRow(i, vRandVect); } double[] coverPoints = null; lock (SUT._locker) { coverPoints = SUT.RunSUT(enVar, vRandVect.ToArray <double>()); } string tmp = null; for (int o = 0; o < coverPoints.Length; o++) { if (o < (int)_enVar.pmProblem["NumOfCE"]) { if (coverPoints[o] == 1) { tmp = tmp + o.ToString() + " "; } } else { break; } } if (pathRecord.Keys.Contains(tmp)) { pathRecord[tmp] = pathRecord[tmp] + 1; } else { pathRecord.Add(tmp, 1); } var tm = Vector.Build.Dense(coverPoints .Where((x, j) => j <= (int)_enVar.pmProblem["NumOfCE"] - 1).ToArray()); fitnessPerCE = fitnessPerCE.Add(tm); List <int> indexList = coverPoints.Select((x, j) => { if (x == 1) { return(j); } return(-1); }).Where(y => y != -1).ToList(); // Construct Cover Element pair count Matrix // |c0, c1| // |c2, c3| // |c4, c5| // ........................................ double[] duplicates = new double[((int)_enVar.pmProblem["NumOfCE"] + 1) / 2]; for (int j = 0; j < indexList.Count; j++) { int index = indexList[j]; int rowIndex = index / 2; int colIndex = index % 2; Vector <double> vTmp = mCoverPairs.Row(rowIndex); vTmp[colIndex] = vTmp[colIndex] + 1; mCoverPairs.SetRow(rowIndex, vTmp.ToArray()); duplicates[rowIndex] = duplicates[rowIndex] + 1; } double[] tmpVect = duplicates.Select(x => { if (x > 0) { return(x - 1); } else { return(0); } }).ToArray(); vIntersetSet = vIntersetSet.Add(Vector.Build.Dense(tmpVect)); } for (int o = 0; o < pathRecord.Count; o++) { pathRecord[pathRecord.ElementAt(o).Key] /= (enVar.testSetSize * 1.0); } strTestSet = mInputVectors.ToString(100, _dimension) .Replace("\r", "").Replace("\n", ";").Replace(" ", "_"); // Calculate P(In) vector Vector <double> vPin = mCoverPairs.RowSums().Subtract(vIntersetSet) / enVar.testSetSize; Matrix <double> mNormalizeRow = mCoverPairs.Subtract( Matrix.Build.DenseOfColumnArrays(vIntersetSet.ToArray(), vIntersetSet.ToArray()) ).NormalizeRows(1); Matrix <double> mTmp = mNormalizeRow.Transpose().Add(0.00001).PointwisePower(-1).PointwiseLog10(); var mEye = DiagonalMatrix.Build.DiagonalOfDiagonalArray(vPin.ToArray()); var fit1Of2 = mNormalizeRow.Multiply(mTmp); var fit2Of2 = fit1Of2.Multiply(mEye).Diagonal(); entropy = fit2Of2.Sum(); fitnessPerCE = fitnessPerCE.Divide(_enVar.testSetSize); duplicateSamples = 1; //For Testing Only... }