/// <summary> /// Attempts to populate <see cref="m_countersMatrixSnapshot"/> from <see cref="m_countersMatrix"/> in /// a consistent fashion (linearizable with all of the transitions). /// </summary> /// <remarks> /// <see cref="m_snapshotLock"/> must be held to protect <see cref="m_countersMatrixSnapshot"/>. /// </remarks> private bool TrySnapshotMatrix() { Contract.Requires(Monitor.IsEntered(m_snapshotLock)); // TODO: This deserves perf instrumentation. If this simple implementation becomes a problem, // we can move to an implementation that co-operates with concurrent writers to complete. // For example see: // Maurice Herlihy and Nir Shavit. 2008. The Art of Multiprocessor Programming. Morgan Kaufmann Publishers Inc., San Francisco, CA, USA. // (section 4.3 - 'Atomic Snapshots') // This implementation (like the example in 4.3.1) is merely 'obstruction-free' since it is only guaranteed to complete in bounded steps // when running in isolation. Array.Copy(m_countersMatrix, m_countersMatrixSnapshot, m_countersMatrix.Length); for (int i = 0; i < m_countersMatrix.GetLength(0); i++) { for (int j = 0; j < m_countersMatrix.GetLength(1); j++) { for (int pipTypeIdx = 0; pipTypeIdx < (int)PipType.Max; pipTypeIdx++) { if (m_countersMatrix[i, j, pipTypeIdx] != m_countersMatrixSnapshot[i, j, pipTypeIdx]) { // Snapshot violated. return(false); } } } } return(true); }
private static bool isInMatrix(long[,,] matrix, long dimension) { bool result = dimension >= 0 && dimension <= matrix.GetLength(0) && dimension <= matrix.GetLength(1) && dimension <= matrix.GetLength(2) && dimension >= 0; return(result); }
public static void Print(long[,,] k){ for(int i=0;i<k.GetLength(0);i++){ for(int j=0;j<k.GetLength(1);j++){ for(int l=0;l<k.GetLength(2);l++){ Console.Write(k[i,j,l]+" "); } Console.WriteLine(); } Console.WriteLine(); } }
/// <summary> /// Collects a snapshot of current state counts into the provided instance. /// </summary> /// <remarks> /// Calls to this method are serialized. /// </remarks> public void CollectSnapshot(PipType[] pipTypes, PipStateCountersSnapshot target) { lock (m_snapshotLock) { // Populate m_countersMatrixSnapshot as a snapshot of m_countersMatrix. while (!TrySnapshotMatrix()) { } target.Clear(); // Note that from now on we should be using m_countersMatrixSnapshot. No access to m_countersMatrix // would be correct, since m_snapshotLock does not block writers to m_countersMatrix. // For each state, derive the number of pips in that state by accumulating the transitions into and out of it. for (int fromIdx = 0; fromIdx < m_countersMatrixSnapshot.GetLength(0); fromIdx++) { for (int toIdx = 0; toIdx < m_countersMatrixSnapshot.GetLength(1); toIdx++) { for (int pipTypeIdx = 0; pipTypeIdx < pipTypes.Length; pipTypeIdx++) { var pipType = pipTypes[pipTypeIdx]; long transitionCount = m_countersMatrixSnapshot[fromIdx, toIdx, (int)pipType]; m_snapshotSums[toIdx] += transitionCount; // The reflexive 'transitions' are for counting initial states. if (toIdx != fromIdx) { m_snapshotSums[fromIdx] -= transitionCount; } } } } for (int i = 0; i < m_snapshotSums.Length; i++) { if (m_snapshotSums[i] < 0) { Contract.Assume( false, "PipStateCounters dropped below 0 for a state, suggesting PipStateCounters was not notified of a transition: " + (PipState)(i + PipStateRange.MinValue)); } } m_snapshotSums = target.Swap(m_snapshotSums); } }
public static long[,,] ReplaceAllFromRange(long[,,] arr, long minValue, long maxValue, long replaceTo) { for (int i = 0; i < arr.GetLength(0); i++) { for (int j = 0; j < arr.GetLength(1); j++) { for (int k = 0; k < arr.GetLength(2); k++) { arr[i, j, k] = ReplaceIfFromRange(arr[i, j, k], minValue, maxValue, replaceTo); } } } return(arr); }
private void solve_Click(object sender, EventArgs e) { solveMatrix = null; try//get the text off the textField and process it { solveMatrix = textToArray(); } catch (Exception) { this.label1.Text = "Matrix was not valid."; } if (solveMatrix == null) { Console.WriteLine("Error: Matrix has not been recieved"); } else { MinPathSum solve = new MinPathSum(); long[,,] minPathArray = solve.minPath(solveMatrix); //long minPathSum = MinPathSum.pathSum(minPath, solveMatrix); long minSumRow = long.MaxValue; int minRowPlacement = int.MaxValue; for (int i = 0; i < minPathArray.GetLength(1); i++) { //find the lowest pathSum if (minPathArray[minPathArray.GetLength(0) - 1, i, 1] < minSumRow) { minSumRow = minPathArray[minPathArray.GetLength(0) - 1, i, 1]; minRowPlacement = i; } } String path = recursiveReportPath(new Point(minPathArray.GetLength(0) - 1, minRowPlacement), minPathArray); path += " -> SUM = " + minPathArray[minPathArray.GetLength(0) - 1, minRowPlacement, 1]; MessageBox.Show(path); //unecessary, but can be included for (int i = 0; i < minPathArray.GetLength(1); i++) { for (int j = 0; j < minPathArray.GetLength(0); j++) { Console.Write(minPathArray[j, i, 1] + " "); } Console.WriteLine(); } } }
private RespuestaGeneral AcumularValores(long[] infoOperacion, long[,,] matriz) { long sumatoria = 0; int length = matriz.GetLength(0); for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { for (int k = 0; k < length; k++) { if (infoOperacion[0] - 1 <= i && infoOperacion[1] - 1 <= j && infoOperacion[2] - 1 <= k && i <= infoOperacion[3] - 1 && j <= infoOperacion[4] - 1 && k <= infoOperacion[5] - 1) { sumatoria += matriz[i, j, k]; } } } } respuestaGeneral.Mensaje += sumatoria + "\n"; return(respuestaGeneral); }
private static void ValidateResults(long[,,] times, World[] worlds) { var normizedTimes = new long[times.GetLength(0), times.GetLength(2)]; for (var worldIndex = 0; worldIndex < times.GetLength(0); worldIndex++) { for (var testIndex = 0; testIndex < times.GetLength(2); testIndex++) { long testTime = 0; for (var executionIndex = 0; executionIndex < times.GetLength(1); executionIndex++) { testTime += times[worldIndex, executionIndex, testIndex]; } testTime /= times.GetLength(1); normizedTimes[worldIndex, testIndex] = testTime; Console.WriteLine(worlds[worldIndex].GetType().Name + "; Test " + testIndex + " took " + testTime + "ms on average"); } } }
public void WriteExtended(string key, long[,,] array3D) { if (writer == null) { throw new FileEE("無効なストリームです"); } if (array3D == null) { throw new FileEE("無効な配列が渡されました"); } var countX = 0; var length0 = array3D.GetLength(0); var length1 = array3D.GetLength(1); var length2 = array3D.GetLength(2); var countY = new int[length0]; var countZ = new int[length0, length1]; for (var x = 0; x < length0; x++) { for (var y = 0; y < length1; y++) { for (var z = 0; z < length2; z++) { if (array3D[x, y, z] != 0) { countX = x + 1; countY[x] = y + 1; countZ[x, y] = z + 1; } } } } if (countX == 0) { return; } writer.WriteLine(key); for (var x = 0; x < countX; x++) { writer.WriteLine(x + "{"); if (countY[x] == 0) { writer.WriteLine("}"); continue; } for (var y = 0; y < countY[x]; y++) { var builder = new StringBuilder(""); if (countZ[x, y] == 0) { writer.WriteLine(""); continue; } for (var z = 0; z < countZ[x, y]; z++) { builder.Append(array3D[x, y, z].ToString()); if (z != countZ[x, y] - 1) { builder.Append(","); } } writer.WriteLine(builder.ToString()); } writer.WriteLine("}"); } writer.WriteLine(FINISHER); }
public static bool RunTests(int pid, NktRemoteBridge remoteBridge) { //long<->long test Console.Write(" Long single value test... "); try { long nBaseLong = 10; object res; res = remoteBridge.InvokeJavaStaticMethod(pid, "JavaLongTest", "Test", nBaseLong); if (res.GetType() != typeof(long)) { Console.WriteLine("Error: Invalid returned type"); return(false); } if ((long)res != 255 - nBaseLong) { Console.WriteLine("Error: Returned data mismatch"); return(false); } } catch (System.Exception ex) { Console.WriteLine("failed."); Console.WriteLine(ex.ToString()); return(false); } Console.WriteLine(" OK"); //long<->long array test Console.Write(" Long array test... "); try { long[, ,] arrayLong = new long[4, 6, 5]; object res; int i, j, k; for (i = 0; i < 4; i++) { for (j = 0; j < 6; j++) { for (k = 0; k < 5; k++) { arrayLong[i, j, k] = (long)(i + j + k); } } } res = remoteBridge.InvokeJavaStaticMethod(pid, "JavaLongTest", "Test3D", new object[] { arrayLong }); if (res.GetType() != typeof(long[, , ])) { Console.WriteLine("Error: Invalid returned type"); return(false); } long[, ,] arrayLongRes = (long[, , ])res; if (arrayLongRes.GetLength(0) != 4 || arrayLongRes.GetLength(1) != 6 || arrayLongRes.GetLength(2) != 5) { Console.WriteLine("Error: Returned array size mismatch"); return(false); } for (i = 0; i < 4; i++) { for (j = 0; j < 6; j++) { for (k = 0; k < 5; k++) { if (arrayLongRes[i, j, k] != 255 - arrayLong[i, j, k]) { Console.WriteLine("Error: Returned data mismatch (" + i.ToString() + "," + j.ToString() + "," + k.ToString() + ")"); return(false); } } } } } catch (System.Exception ex) { Console.WriteLine("failed."); Console.WriteLine(ex.ToString()); return(false); } Console.WriteLine(" OK"); //byte<->long test Console.Write(" Long single value test using bytes... "); try { byte nBaseByte = 10; object res; res = remoteBridge.InvokeJavaStaticMethod(pid, "JavaLongTest", "Test", nBaseByte); if (res.GetType() != typeof(long)) { Console.WriteLine("Error: Invalid returned type"); return(false); } if ((byte)(long)res != 255 - nBaseByte) { Console.WriteLine("Error: Returned data mismatch"); return(false); } } catch (System.Exception ex) { Console.WriteLine("failed."); Console.WriteLine(ex.ToString()); return(false); } Console.WriteLine(" OK"); //byte<->long array test Console.Write(" Long array test using bytes... "); try { byte[, ,] arrayByte = new byte[4, 6, 5]; object res; int i, j, k; for (i = 0; i < 4; i++) { for (j = 0; j < 6; j++) { for (k = 0; k < 5; k++) { arrayByte[i, j, k] = (byte)(i + j + k); } } } res = remoteBridge.InvokeJavaStaticMethod(pid, "JavaLongTest", "Test3D", new object[] { arrayByte }); if (res.GetType() != typeof(long[, , ])) { Console.WriteLine("Error: Invalid returned type"); return(false); } long[, ,] arrayLongRes = (long[, , ])res; if (arrayLongRes.GetLength(0) != 4 || arrayLongRes.GetLength(1) != 6 || arrayLongRes.GetLength(2) != 5) { Console.WriteLine("Error: Returned array size mismatch"); return(false); } for (i = 0; i < 4; i++) { for (j = 0; j < 6; j++) { for (k = 0; k < 5; k++) { if (arrayLongRes[i, j, k] != 255 - (long)arrayByte[i, j, k]) { Console.WriteLine("Error: Returned data mismatch (" + i.ToString() + "," + j.ToString() + "," + k.ToString() + ")"); return(false); } } } } } catch (System.Exception ex) { Console.WriteLine("failed."); Console.WriteLine(ex.ToString()); return(false); } Console.WriteLine(" OK"); //short<->long test Console.Write(" Long single value test using shorts... "); try { short nBaseShort = 10; object res; res = remoteBridge.InvokeJavaStaticMethod(pid, "JavaLongTest", "Test", nBaseShort); if (res.GetType() != typeof(long)) { Console.WriteLine("Error: Invalid returned type"); return(false); } if ((short)(long)res != 255 - nBaseShort) { Console.WriteLine("Error: Returned data mismatch"); return(false); } } catch (System.Exception ex) { Console.WriteLine("failed."); Console.WriteLine(ex.ToString()); return(false); } Console.WriteLine(" OK"); //short<->long array test Console.Write(" Long array test using shorts... "); try { short[, ,] arrayShort = new short[4, 6, 5]; object res; int i, j, k; for (i = 0; i < 4; i++) { for (j = 0; j < 6; j++) { for (k = 0; k < 5; k++) { arrayShort[i, j, k] = (short)(i + j + k); } } } res = remoteBridge.InvokeJavaStaticMethod(pid, "JavaLongTest", "Test3D", new object[] { arrayShort }); if (res.GetType() != typeof(long[, , ])) { Console.WriteLine("Error: Invalid returned type"); return(false); } long[, ,] arrayLongRes = (long[, , ])res; if (arrayLongRes.GetLength(0) != 4 || arrayLongRes.GetLength(1) != 6 || arrayLongRes.GetLength(2) != 5) { Console.WriteLine("Error: Returned array size mismatch"); return(false); } for (i = 0; i < 4; i++) { for (j = 0; j < 6; j++) { for (k = 0; k < 5; k++) { if (arrayLongRes[i, j, k] != 255 - (long)arrayShort[i, j, k]) { Console.WriteLine("Error: Returned data mismatch (" + i.ToString() + "," + j.ToString() + "," + k.ToString() + ")"); return(false); } } } } } catch (System.Exception ex) { Console.WriteLine("failed."); Console.WriteLine(ex.ToString()); return(false); } Console.WriteLine(" OK"); //int<->long test Console.Write(" Long single value test using ints... "); try { int nBaseInt = 10; object res; res = remoteBridge.InvokeJavaStaticMethod(pid, "JavaLongTest", "Test", nBaseInt); if (res.GetType() != typeof(long)) { Console.WriteLine("Error: Invalid returned type"); return(false); } if ((int)(long)res != 255 - nBaseInt) { Console.WriteLine("Error: Returned data mismatch"); return(false); } } catch (System.Exception ex) { Console.WriteLine("failed."); Console.WriteLine(ex.ToString()); return(false); } Console.WriteLine(" OK"); //int<->long array test Console.Write(" Long array test using ints... "); try { int[, ,] arrayInt = new int[4, 6, 5]; object res; int i, j, k; for (i = 0; i < 4; i++) { for (j = 0; j < 6; j++) { for (k = 0; k < 5; k++) { arrayInt[i, j, k] = (int)(i + j + k); } } } res = remoteBridge.InvokeJavaStaticMethod(pid, "JavaLongTest", "Test3D", new object[] { arrayInt }); if (res.GetType() != typeof(long[, , ])) { Console.WriteLine("Error: Invalid returned type"); return(false); } long[, ,] arrayLongRes = (long[, , ])res; if (arrayLongRes.GetLength(0) != 4 || arrayLongRes.GetLength(1) != 6 || arrayLongRes.GetLength(2) != 5) { Console.WriteLine("Error: Returned array size mismatch"); return(false); } for (i = 0; i < 4; i++) { for (j = 0; j < 6; j++) { for (k = 0; k < 5; k++) { if (arrayLongRes[i, j, k] != 255 - (long)arrayInt[i, j, k]) { Console.WriteLine("Error: Returned data mismatch (" + i.ToString() + "," + j.ToString() + "," + k.ToString() + ")"); return(false); } } } } } catch (System.Exception ex) { Console.WriteLine("failed."); Console.WriteLine(ex.ToString()); return(false); } Console.WriteLine(" OK"); return(true); }
private void writeData(long[,,] array) { var countZero = 0; //0については0が連続する数を記憶する。その他はそのまま記憶する。 var countAllZero = 0; //列の要素が全て0である列の連続する数を記憶する。列の要素に一つでも非0があるなら通常の記憶方式。 var countAllZero2D = 0; //行列の要素が全て0である行列の・・・ var length0 = array.GetLength(0); var length1 = array.GetLength(1); var length2 = array.GetLength(2); writer.Write(length0); writer.Write(length1); writer.Write(length2); for (var x = 0; x < length0; x++) { for (var y = 0; y < length1; y++) { for (var z = 0; z < length2; z++) { if (array[x, y, z] == 0) { countZero++; } else { if (countAllZero2D > 0) { writer.Write(Ebdb.ZeroA2); m_WriteInt(countAllZero2D); countAllZero2D = 0; } if (countAllZero > 0) { writer.Write(Ebdb.ZeroA1); m_WriteInt(countAllZero); countAllZero = 0; } if (countZero > 0) { writer.Write(Ebdb.Zero); m_WriteInt(countZero); countZero = 0; } m_WriteInt(array[x, y, z]); } } if (countZero == length2) { countAllZero++; } else { writer.Write(Ebdb.EoA1); } countZero = 0; } if (countAllZero == length1) { countAllZero2D++; } else { writer.Write(Ebdb.EoA2); } countAllZero = 0; } writer.Write(Ebdb.EoD); }
public void GenerateDataToTxt(string symbol, string lastWeek) { int n = TestParameters2.nTpsl; string ccScoreFileName = TestParameters.GetBaseFilePath(string.Format("{0}_ccScores_w{2}_{1}.txt", TestParameters2.CandidateParameter.MainSymbol, TestParameters2.CandidateParameter.MainPeriod, TestParameters2.lastWeek)); DateTime date = TestParameters2.TrainStartTime; DateTime maxDate = TestParameters2.TrainEndTime; if (!TestParameters2.RealTimeMode && System.IO.File.Exists(ccScoreFileName)) { return; } SortedDictionary <DateTime, string> dictAlready = new SortedDictionary <DateTime, string>(); if (System.IO.File.Exists(ccScoreFileName)) { using (StreamReader sr = new StreamReader(ccScoreFileName)) { while (!sr.EndOfStream) { string s = sr.ReadLine(); int idx = s.IndexOf(','); DateTime d = Convert.ToDateTime(s.Substring(0, idx)); string s2 = s.Substring(idx + 1); dictAlready[d] = s2.Trim(); } } } string sql; System.Data.DataTable allDt = null; DateTime nextBufferDate = DateTime.MinValue; while (true) { if (!TestParameters2.RealTimeMode) { Console.WriteLine(date.ToString(Parameters.DateTimeFormat)); } if (dictAlready.ContainsKey(date)) { GenerateDateToTxt(ccScoreFileName, date, dictAlready[date]); date = date.AddHours(TestParameters2.MainPeriodOfHour); if (date >= maxDate) { break; } } else { if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday) { date = date.AddHours(TestParameters2.MainPeriodOfHour); continue; } if (nextBufferDate <= date) { nextBufferDate = date.AddHours(TestParameters2.MainPeriodOfHour); string tableName = string.Format("{0}_CCSCORE_{1}", TestParameters2.CandidateParameter.MainSymbol, TestParameters2.lastWeek); sql = string.Format("SELECT * FROM {0} WHERE TIME >= '{1}' AND TIME < '{2}' AND {3}", tableName, WekaUtils.GetTimeFromDate(date), WekaUtils.GetTimeFromDate(nextBufferDate), string.IsNullOrEmpty(TestParameters.DbSelectWhere) ? "1 = 1" : TestParameters.DbSelectWhere); allDt = Feng.Data.DbHelper.Instance.ExecuteDataTable(sql); } DateTime nextDate = date.AddHours(TestParameters2.MainPeriodOfHour); sql = string.Format("TIME >= '{1}' AND TIME < '{2}'", symbol, WekaUtils.GetTimeFromDate(date), WekaUtils.GetTimeFromDate(nextDate)); var dt = allDt.Select(sql); if (dt.Length > 0) { double[, ,] scores = new double[2, TestParameters.TpMaxCount / n, TestParameters.SlMaxCount / n]; long[, ,] ndas = new long[2, TestParameters.TpMaxCount / n, TestParameters.SlMaxCount / n]; long[, ,] ndss = new long[2, TestParameters.TpMaxCount / n, TestParameters.SlMaxCount / n]; for (int j = 0; j < scores.GetLength(0); ++j) { for (int k = 0; k < scores.GetLength(1); ++k) { for (int l = 0; l < scores.GetLength(2); ++l) { scores[j, k, l] = -1; ndas[j, k, l] = -1; ndss[j, k, l] = -1; } } } System.Data.DataRow row = dt[0]; { double[, ,] score = DeserializeScores((byte[])row["scores"]); long[, ,] nda = DeserializeScoreTimes((byte[])row["nda"]); long[, ,] nds = DeserializeScoreTimes((byte[])row["nds"]); WekaUtils.DebugAssert(score.GetLength(0) == 2, ""); WekaUtils.DebugAssert(nda.GetLength(0) == 2, ""); for (int j = 0; j < score.GetLength(0); ++j) { for (int k = 0; k < score.GetLength(1); ++k) { for (int l = 0; l < score.GetLength(2); ++l) { if (k % n != n - 1 || l % n != n - 1) { continue; } //if (k / n >= TestParameters2.tpCount) // continue; //if (l / n >= TestParameters2.slCount) // continue; scores[j, k / n, l / n] = score[j, k, l]; ndas[j, k / n, l / n] = nda[j, k, l]; ndss[j, k / n, l / n] = nds[j, k, l]; } } } GenerateDateToTxt(ccScoreFileName, date, scores, ndas, ndss); } } date = nextDate; if (date >= maxDate) { break; } } } }