/// <summary> /// Gets the current wireless carriers network name from the SIM. /// </summary> /// <returns>The carrier description.</returns> public static string GetServiceProvider() { IntPtr hSim, res; res = SimInitialize(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out hSim); if (res != IntPtr.Zero) throw new Exception("Could not initialize SIM"); SimRecord rec = new SimRecord(); rec.cbSize = (IntPtr)Marshal.SizeOf(rec); res = SimGetRecordInfo(hSim, (IntPtr)SERVICE_PROVIDER, ref rec); if (res != IntPtr.Zero) throw new Exception("Could not read the service provider information from the SIM"); byte[] bData = new byte[(int)rec.dwSize + 1]; IntPtr dwBytesRead = IntPtr.Zero; res = SimReadRecord(hSim, (IntPtr)SERVICE_PROVIDER, rec.dwRecordType, IntPtr.Zero, bData, (IntPtr)bData.Length, ref dwBytesRead); if (res != IntPtr.Zero) throw new Exception("Could not read the service provider from the SIM"); byte[] bScrubbed = new byte[(int)dwBytesRead]; int nPos = 0; // Scrub the non-ascii characters for (int i = 0; i < (int)dwBytesRead; i++) { if (((int)bData[i] > 19) && ((int)bData[i] < 125)) { bScrubbed[nPos] = bData[i]; nPos++; } } SimDeinitialize(hSim); return Encoding.ASCII.GetString(bScrubbed, 0, bScrubbed.Length); }
private static extern IntPtr SimGetRecordInfo(IntPtr hSim, IntPtr dwAddress, ref SimRecord lpSimRecordInfo);
// return: シミュレーションが成功したか public bool Simulate() { // distanceFtoG = -1; // while(distanceFtoG == -1){ // InitNodes(); // } // connectedが保証されてないので絶対に使ってはいけません. // MakeRandomGraph(); InitNodes(); var tryCount = 0; const int maxTryCount = 1000; while (!IsAllGoalKnown()) { tryCount++; if (maxTryCount < tryCount) { return(false); } // 選択可能な全ノード var selectableNodes = _currentNode.GetAllLinkedNodes(MaxSelectHop); // ユーザーの視界を再現した, 見える範囲のノード var visibleNodes = new List <SimNode>(); for (int i = 0; i < MaxVisibleCount; i++) { var selectHop = SelectHopByExpDist(); // try{ // // みたいノードが0個になったら, 手前のノードで代替する // while(0 < selectHop && selectableNodes[selectHop].Count == 0){ // selectHop--; // continue; // } // } catch(IndexOutOfRangeException){ // Debug.Log("out of range"); // Debug.Log("SelectHop:" + selectHop); // Debug.Log("length:" + selectableNodes.Length); // } if (selectableNodes[selectHop].Count == 0) { continue; } if (selectHop == 0) { break; } var r = rand.Next(0, selectableNodes[selectHop].Count); visibleNodes.Add(selectableNodes[selectHop][r]); selectableNodes[selectHop].RemoveAt(r); var selectableNodesCount = selectableNodes.Sum(ns => ns.Count); if (selectableNodesCount == 1) { break; } } // 目的ノードが視界に入っていたら高確率で選択 var moved = false; foreach (var goalNode in _goalNodes) { if (visibleNodes.Contains(goalNode)) { var r = rand.NextDouble(); if (r < pSelectVisibleGoal && !goalNode.Known) { _currentNode = goalNode; _currentNode.Known = true; _opCount++; moved = true; } } } if (moved) { continue; } // 一定確率で始点ノードに戻る if ((float)rand.NextDouble() < pReturnFirst && _currentNode != _firstNode) { _currentNode = _firstNode; _opCount++; continue; } if (IsAllGoalKnown()) { continue; } var r1 = rand.NextDouble(); // 未知ノードに移動 if (r1 < pSelectUnknown) { // var selectHop = SelectHopByProbArray(pSelectHopUnknown); var selectHop = SelectHopByExpDist(); // var unknownNodes = _currentNode.GetLinkedNodes(selectHop, false).ToList(); var unknownNodes = visibleNodes.Where(n => n.Known == false).ToArray(); if (unknownNodes.Length == 0) { continue; } var r2 = rand.Next(0, unknownNodes.Length); _currentNode = unknownNodes[r2]; _currentNode.Known = true; _opCount++; } // 既知ノードに移動 else { // var selectHop = SelectHopByProbArray(pSelectHopKnown); var selectHop = SelectHopByExpDist(); // Debug.Log("Select Hop " + selectHop); // var knownNodes = _currentNode.GetLinkedNodes(selectHop, true); var knownNodes = visibleNodes.Where(n => n.Known == true).ToArray(); if (knownNodes.Length == 0) { continue; } var r2 = rand.Next(0, knownNodes.Length); _currentNode = knownNodes[r2]; _currentNode.Known = true; _opCount++; } } // Debug.Log("操作数" + _opCount); var record = new SimRecord { dataSet = dataSet, nodeCount = allNodeCount, goalCount = goalCount, graphDensity = _graphDensity, lambda = Lambda, distance = distanceFtoG, opCount = _opCount, pSelectUnknown = pSelectUnknown, pReturnFirst = pReturnFirst, pSelectVisibleGoal = pSelectVisibleGoal }; records.Add(record); return(true); }