private static void ArrayFill(int[] arr) { for (int i = 0; i < arr.Length; i++) { arr[i] = SingleRandom.Next(-100, 101); } }
/// <summary> /// Shuffle parameter values /// </summary> private void Shuffle(object sender) { SingleRandom random = SingleRandom.Instance; List <Tuple <string, double> > requestValues = new List <Tuple <string, double> >(); foreach (var item in _valueParameters) { if (item.Value != 0) { double v = item.Value; double plus = (v + 0.25 * v); // plus minus values - around the current value +-25% double minus = (v - 0.25 * v); double randValue = random.NextDouble() * (plus - minus) + minus; item.SuppressUpdate(); item.Value = randValue; requestValues.Add(new Tuple <string, double>(item.Name, randValue)); } } foreach (var item in _builtInParameters) { if (item.Value != 0) { double v = item.Value; double plus = (v + 0.25 * v); // plus minus values - around the current value +-25% double minus = (v - 0.25 * v); double randValue = random.NextDouble() * (plus - minus) + minus; item.SuppressUpdate(); item.Value = randValue; requestValues.Add(new Tuple <string, double>(item.Name, randValue)); } } if (requestValues.Count > 0) { MakeRequest(RequestId.SlideParam, requestValues); } }
public void JoinUserDataTest() { var peer1 = CreateAndStartLocalPeer(); peer1.SetVisibility(true, null, false); var bytesSent = new byte[123]; SingleRandom.NextBytes(bytesSent); object myLock = new object(); var waitHandel = new AutoResetEvent(false); byte[] bytesReceived = null; peer1.PeerAdded += (int id, Packet userDataPacketReceived) => { lock (myLock) { bytesReceived = userDataPacketReceived.ReadBytes(userDataPacketReceived.BytesRemaining); waitHandel.Set(); } }; var peer2 = CreateAndStartLocalPeer(); var userData = peer1.BorrowPacketFromPool(); userData.WriteBytes(bytesSent); ConnectToLocalPeer(peer2, peer1, null, userData); peer1.ReturnPacketToPool(userData); waitHandel.WaitOne(MAX_REPLY_WAIT_TIME); lock (myLock) { Assert.IsTrue(bytesSent.SequenceEqual(bytesReceived), "user data received in JoinRequest not as sent"); } }
public void RunBatch() { //if there were any xmpp connections specified in the perams not set by the user, connect here if (!AutoConnectExtSim()) { this._error = "Failed to connect to external simulation."; logger.Error(this.error); } batchSuccess = false; _stop = false; bool retVal = true; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); TimeSpan resTime = stopWatch.Elapsed; int actRuns = 0; int curI = 0; StreamWriter pathOutputFile = null; if (!string.IsNullOrEmpty(_keyPathsOutput)) { pathOutputFile = new StreamWriter(_keyPathsOutput, append: false); } //if user defined the seed then reset random so that seed is used. if (ConfigData.seed != null) { SingleRandom.Reset(); } //make a list of result items for each key state. //ResultState[] keyStates = _lists.allStates.Where(i => i.Value.stateType == EnStateType.stKeyState).Select(i => new ResultState(i.Value.name)).ToArray(); //Dictionary<string, ResultState> keyStateResMap = null; //progressCallback(stopWatch.Elapsed, 0); try { _lists.allVariables.ReInitAll(); for (int i = 1; i <= _numRuns; ++i) { SetLog(i); logger.Info("StartRun: " + i); curI = i; if (_stop) { break; } SimulationTracking.StateTracker trackSim; if (_msgServer == null) { trackSim = new SimulationTracking.StateTracker(_lists, _endTime, 0, null); } else { trackSim = new SimulationTracking.StateTracker(_lists, _endTime, _frameRate, _msgServer); } //trackSim.logFunc = logFunc; //logFunc("Run " + i.ToString() + Environment.NewLine); List <int> finalStates = trackSim.StartTracker(); //logFunc("Run " + i.ToString() + " done" + Environment.NewLine); if (trackSim.keyStateCnt > 0) { StatePath[] failedComps = null; if (_logFailedComps) { failedComps = trackSim.GetFailedComponents(); } //if (failedComps.Length == 0) // failedComps = trackSim.GetCurrentStates(); if (pathOutputFile != null) { pathOutputFile.WriteLine("Run - " + i.ToString()); } trackSim.GetKeyPaths(keyPaths, otherPaths); foreach (SimulationEngine.ResultStateBase path in keyPaths.Values) { string keyStateName = path.name; if (logVarVals.Count > 0) { Dictionary <string, List <string> > varDict; if (!_variableVals.TryGetValue(keyStateName, out varDict)) { varDict = new Dictionary <string, List <string> >(); _variableVals.Add(keyStateName, varDict); } List <string> varVals; foreach (string varName in logVarVals) { SimVariable curVar = _lists.allVariables.FindByName(varName); if (curVar == null) { this._error = "No variable found named - " + varName; logger.Error(this.error); } if (!varDict.TryGetValue(varName, out varVals)) { varVals = new List <string>(); varDict.Add(varName, varVals); } varVals.Add(curVar.strValue); } } if (_logFailedComps && (failedComps.Length > 0)) { int[] idArray = failedComps.Select(j => j.state.id).ToArray(); if (!keyFailedItems.ContainsKey(keyStateName)) { keyFailedItems.Add(keyStateName, new FailedItems()); } keyFailedItems[keyStateName].AddCompFailSet(idArray); } } } if (((stopWatch.Elapsed - resTime) > TimeSpan.FromSeconds(1)) && (i > 0)) { stopWatch.Stop(); progressCallback(stopWatch.Elapsed, i, _logFailedComps); stopWatch.Start(); resTime = stopWatch.Elapsed; } actRuns = i; logger.Info("EndOfRun: " + i); } } catch (Exception e) { _error = "Exception - " + e.Message; if (e.InnerException != null) { _error = _error + Environment.NewLine + e.InnerException; } //#if DEBUG // throw e; //#else logger.Info(_error); //#endif retVal = false; } stopWatch.Stop(); if (pathOutputFile != null) { pathOutputFile.Close(); } progressCallback(stopWatch.Elapsed, actRuns, _logFailedComps); if (_jsonResultPaths != "") { OverallResults resultObj = new OverallResults(); resultObj.name = this._lists.name; resultObj.keyStates = keyPaths.Values.ToList(); resultObj.otherStatePaths = otherPaths.Values.ToList(); resultObj.numRuns = curI; resultObj.CalcStats(); Dictionary <string, int> inStateCnts = new Dictionary <string, int>(); //foreach (var keyS in resultObj.keyStates) //{ // Dictionary<string, int> depth = new Dictionary<string, int>(); // StateCounts(keyS, inStateCnts, depth); //} foreach (var keyS in resultObj.keyStates) { // Dictionary<string, int> depth = new Dictionary<string, int>(); //SetResultStatsRec(keyS, inStateCnts, curI);//, depth); if (_variableVals.Count > 0) //if there are any being tracked, they should have a value for each key state. { keyS.watchVariables = _variableVals[keyS.name]; } } string output = JsonConvert.SerializeObject(resultObj, Formatting.Indented); File.WriteAllText(_jsonResultPaths, output); } batchSuccess = retVal; }