/// <summary> /// Initializes a new instance of the <see cref="IndexusMessage"/> class. /// </summary> /// <param name="id">The id.</param> /// <param name="status">The status.</param> /// <param name="action">The action.</param> /// <param name="priority">The priority.</param> /// <param name="host">The host.</param> /// <param name="expires">The expires.</param> /// <param name="key">The key.</param> /// <param name="payload">The payload.</param> public IndexusMessage(long id, StatusValue status, ActionValue action, CacheItemPriority priority, string host, DateTime expires, string key, byte[] payload) { #region Access Log #if TRACE { Handler.LogHandler.Tracking("Access Method: " + this.GetType().ToString() + "->" + ((object)MethodBase.GetCurrentMethod()).ToString() + " ;"); } #endif #endregion Access Log this.id = id; this.status = status; this.action = action; this.itemPriority = priority; if (!string.IsNullOrEmpty(host)) { this.hostname = host; } this.expires = expires; this.key = key; this.payload = payload; this.timestamp = DateTime.Now; }
/// <summary> /// Sets the bytes. /// </summary> /// <param name="stream">The stream.</param> /// <returns></returns> public bool SetBytes(MemoryStream stream) { #region Access Log #if TRACE { Handler.LogHandler.Tracking("Access Method: " + this.GetType().ToString() + "->" + ((object)MethodBase.GetCurrentMethod()).ToString() + " ;"); } #endif #endregion Access Log BinaryReader br = new BinaryReader(stream); this.status = (StatusValue)br.ReadByte(); this.action = (ActionValue)br.ReadByte(); this.id = br.ReadInt64(); this.itemPriority = (CacheItemPriority)br.ReadByte(); // Modified: 11-01-2008 SharedCache.com, rschuetz : pre_release_1.0.2.132 http://www.codeplex.com/SharedCache/WorkItem/View.aspx?WorkItemId=5046 this.expires = Formatters.DateTimeUnix.DateTimeFromUnixTime((long)br.ReadUInt64()); // Formatters.DateTimeUnix.ToDateTime(br.ReadDouble()); this.timestamp = Formatters.DateTimeUnix.DateTimeFromUnixTime((long)br.ReadUInt64()); // Formatters.DateTimeUnix.ToDateTime(br.ReadDouble()); int dataLength1 = br.ReadInt32(); byte[] buf1 = new byte[dataLength1]; int read1 = br.Read(buf1, 0, dataLength1); this.key = System.Text.Encoding.UTF8.GetString(buf1); int dataLength = br.ReadInt32(); if (dataLength > 0) { byte[] buf = new byte[dataLength]; int read = br.Read(buf, 0, dataLength); this.payload = buf; buf = null; } return(true); }
public static object Lerp(System.Type type, ActionValue av1, ActionValue av2, float lerp) { if (av1 == null || av2 == null) { return(null); } //lerp = Mathf.Clamp01(lerp); if (type == typeof(float)) { return(Tools.GetMinMaxValue(av1.GetFloat(), av2.GetFloat(), lerp)); } else if (type == typeof(int)) { return(Mathf.RoundToInt(Tools.GetMinMaxValue(av1.GetInt(), av2.GetInt(), lerp))); } else if (type == typeof(Color)) { return(Color.Lerp(av1.GetColor(), av2.GetColor(), lerp)); } else if (type == typeof(Vector2)) { return(Vector2.Lerp(av1.GetVector2(), av2.GetVector2(), lerp)); } else if (type == typeof(Vector3)) { return(Vector3.Lerp(av1.GetVector3(), av2.GetVector3(), lerp)); } else if (type == typeof(bool)) { return(lerp < 0.5f ? av1.GetBool() : av2.GetBool()); } return(null); }
public Agent(stateType State, Policy <stateType, actionType> policy, ActionValue <stateType, actionType> actionValue, List <actionType> possibleActions) { state = State; _policy = policy; _actionValue = actionValue; _possibleActions = possibleActions; }
public static string Name(this ActionValue action) { if (actionValueString.TryGetValue(action, out var value)) { return(value); } actionValueString.Add(action, action.ToString()); return(actionValueString[action]); }
//TODO: //Under set criteria, will create a new model, either a blank slate, or a copy of a previously existing // previously existing model is useful for models that are similar up to a point but may have a slight difference public ActionValue <stateType, actionType> newModel() { ActionValue <stateType, actionType> returnModel = null; Type AVtype = models[0].GetType(); // AVtype.MakeGenericType(typeof(stateType), typeof(actionType)); returnModel = (ActionValue <stateType, actionType>)Activator.CreateInstance(AVtype, SC, AC, AA, SS, parames); return(returnModel); }
public object addAgent(Type policyType, Type actionValueType, params object[] actionValueParameters) { policyType = policyType.MakeGenericType(typeof(int[]), typeof(int)); Policy <int[], int> newPolicy = (Policy <int[], int>)Activator.CreateInstance(policyType); actionValueType = actionValueType.MakeGenericType(typeof(int[]), typeof(int)); ActionValue <int[], int> newActionValue = (ActionValue <int[], int>)Activator.CreateInstance(actionValueType, new IntArrayComparer(), EqualityComparer <int> .Default, availableActions, discretizeState(_position, _velocity), actionValueParameters); agent = new Agent <int[], int>(discretizeState(_position, _velocity), newPolicy, newActionValue, availableActions); return(agent); }
public object addAgent(Type policyType, Type actionValueType, params object[] actionValueParameters) { policyType = policyType.MakeGenericType(typeof(int[]), typeof(int[])); Policy <int[], int[]> newPolicy = (Policy <int[], int[]>)Activator.CreateInstance(policyType); actionValueType = actionValueType.MakeGenericType(typeof(int[]), typeof(int[])); ActionValue <int[], int[]> newActionValue = (ActionValue <int[], int[]>)Activator.CreateInstance(actionValueType, new IntArrayComparer(), new IntArrayComparer(), availableActions, startState, actionValueParameters); agent = new Agent <int[], int[]>(startState, newPolicy, newActionValue, availableActions); return(agent); }
//Make sure to set the gameObject to inactive before calling this public bool Setup(ObjectPool pool, ActionValue <GameObject> del) { if (!isSetup) { Add = del; objectPool = pool; isSetup = true; return(true); } return(false); }
public override RuntimeValue Execute(ExecutionContext context) { int?duringValue = during?.Execute(context).AsInteger(this); var result = new ActionValue(new ImuseAction(name, duringValue, body)); if (name != null) { context.CurrentScope.AddOrUpdateSymbol(this.Node, name, result); } return(result); }
public LinearEgoAlloValue(IEqualityComparer <int[]> StateComparer, IEqualityComparer <int[]> ActionComparer, List <int[]> AvailableActions, int[] StartState, params object[] parameters) : base(StateComparer, ActionComparer, AvailableActions, StartState, parameters) { if (parameters.Length > 0) { fullPredictionMode = (bool)parameters[0]; } if (parameters.Length > 1) { updateTerminationStepCount = (int)parameters[1]; } if (parameters.Length > 2) { if ((bool)parameters[2]) { alloLearner = new ModelBasedValue <int[], int[]>(StateComparer, ActionComparer, AvailableActions, null, true); } else { alloLearner = new ModelFreeValue <int[], int[]>(StateComparer, ActionComparer, AvailableActions, null, true); } } stateComparer = StateComparer; actionComparer = ActionComparer; availableActions = AvailableActions; //alloModel = new ModelFreeValue<int[], int[]>(StateComparer, ActionComparer, availableActions, null, true); //{ // defaultQ = 10.3 //}; egoLearner = new ModelFreeValue <int[], int[]>(StateComparer, actionComparer, availableActions, StartState) { alpha = 0.9 }; egoPredictionModels = new ModelBasedValue <int[], int[]> [3]; for (int i = 0; i < egoPredictionModels.Length; i++) { egoPredictionModels[i] = new ModelBasedValue <int[], int[]>(StateComparer, ActionComparer, availableActions, StartState) { gamma = 0 }; } visitedStates = new Dictionary <int[], int> [4]; for (int i = 0; i < availableActions.Count; i++) { visitedStates[i] = new Dictionary <int[], int>(StateComparer); } }
/// <summary> /// Initializes a new instance of the <see cref="IndexusMessage"/> class. /// </summary> /// <param name="id">The id.</param> /// <param name="value">The value.</param> public IndexusMessage(long id, ActionValue value) { #region Access Log #if TRACE { Handler.LogHandler.Tracking("Access Method: " + this.GetType().ToString() + "->" + ((object)MethodBase.GetCurrentMethod()).ToString() + " ;"); } #endif #endregion Access Log this.id = id; this.action = value; }
public void ResetAllocentric(bool useModelBased) { if (useModelBased) { alloLearner = new ModelBasedValue <int[], int[]>(stateComparer, actionComparer, availableActions, null, true); } else { alloLearner = new ModelFreeValue <int[], int[]>(stateComparer, actionComparer, availableActions, null, true); } stats.cumulativeReward = 0; }
/// <summary> /// Convert an ActionValue to its string representation /// </summary> /// <param name="value"></param> /// <returns>ActionValue string representation</returns> public static string ToString(ActionValue value) { switch (value) { case ActionValue.AUDIO: return("AUDIO"); case ActionValue.DISPLAY: return("DISPLAY"); case ActionValue.EMAIL: return("EMAIL"); default: return("Not valid argument"); } }
private static double heuristic(stateType state, ActionValue <stateType, actionType> model, List <actionType> availableActions) { return(-model.value(state, availableActions).Max() / 2 + 0.5); //double bestDist = double.PositiveInfinity; //foreach (int[] goal in goals) //{ // int[] diff = arraySubtract(goal, state); // double taxiDist = 0; // foreach (int i in diff) // { // taxiDist += Math.Abs(i); // } // if (taxiDist < bestDist) // bestDist = taxiDist; //} //return bestDist*0.01; }
public void TrackEvent(ActionValue actionValue, string name, int value = 0) { //if (!_init) //{ // _collector.Initialize(_dictionary); // _init = true; //} var analyticsEvent = new AnalyticsEvent() { Category = Category.AppEvents.ToString(), ActionValue = actionValue.ToString(), Name = name, Value = value, ObjectType = GetType().Name }; this.Log(analyticsEvent); //_collector.Log(analyticsEvent); // AppName //ClipContext Clip // ContentId //Uri DocumentUri //Collection<PropertyValue> EventArgs //HitType HitType //InstallState InstallState //bool IsFullScreen // Language //Uri MediaSource //MediaStreamType? MediaStreamType //TimeSpan MediaTimeCode // NavigationState // ObjectName // ObjectType // Referrer //Size Resolution // SilverlightVersion // StateName //TimeSpan TimeCode //DateTimeOffset Timestamp //TrackInfo Track // UserAgent // Version //double ZoomFactor }
public static float ReadValue(this ActionValue action, InputAction.CallbackContext context) { switch (action) { case ActionValue.MoveX: var leftX = context.ReadValue <Vector2>().x; var normalizeX = (leftX + 1f) / 2f; return(normalizeX); case ActionValue.MoveY: var leftY = context.ReadValue <Vector2>().y; var normalizeY = (leftY + 1f) / 2f; return(normalizeY); default: throw new ArgumentOutOfRangeException(nameof(action), action, null); } }
/// <summary> /// Creates a new Action. /// </summary> /// <param name="validator">The validator to use for validation.</param> /// <param name="node">The "action" node.</param> internal Action(NodeValidator validator, Node node) : base(validator, node) { if (node.Attributes.ContainsKey(DEVICE_ATTRIBUTE)) { device = ActionDeviceHelper.TryParse(node.Attributes[DEVICE_ATTRIBUTE]); } if (node.Attributes.ContainsKey(TIME_ATTRIBUTE)) { time = ConversionHelper.ParseHexValue(node.Attributes[TIME_ATTRIBUTE]); } if (node.Attributes.ContainsKey(USAGE_ATTRIBUTE)) { usage = (ActionKey)ConversionHelper.ParseHexValue(node.Attributes[USAGE_ATTRIBUTE]); } if (node.Attributes.ContainsKey(PAGE_ATTRIBUTE)) { page = ConversionHelper.ParseHexValue(node.Attributes[PAGE_ATTRIBUTE]); } if (node.Attributes.ContainsKey(VALUE_ATTRIBUTE)) { value = (ActionValue)ConversionHelper.ParseHexValue(node.Attributes[VALUE_ATTRIBUTE]); } }
/// <summary> /// Copies the specified MSG. /// </summary> /// <param name="msg">The MSG.</param> private void Copy(IndexusMessage msg) { #region Access Log #if TRACE { Handler.LogHandler.Tracking("Access Method: " + this.GetType().ToString() + "->" + ((object)MethodBase.GetCurrentMethod()).ToString() + " ;"); } #endif #endregion Access Log if (msg == null) { this.action = ActionValue.Error; throw new ArgumentNullException("IndexusMessage cannot be NULL"); } this.itemPriority = msg.ItemPriority; this.id = msg.id; this.action = msg.action; this.key = msg.key; this.payload = msg.payload; this.expires = msg.expires; this.status = msg.status; this.timestamp = msg.timestamp; }
public Action(XmlNode node, SkillInfo info, Action parent) { this.SkillInfo = info; //load attribute actionType = (eActionType)Enum.Parse(typeof(eActionType), node.Attributes["type"].Value); //Debug.LogFormat("{0}", node.Attributes["value"].Value); value = int.Parse(node.Attributes["value"].Value); XmlAttribute piercingAttr = node.Attributes["piercing"]; if (piercingAttr != null) { piercing = float.Parse(piercingAttr.Value); } if (parent != null) { switch (actionType) { case eActionType.shield: throw new System.Exception(string.Format("[{0}] shield action should not be subaction", info.ID)); } IsSubAction = true; duration = parent.duration; check_distance = parent.check_distance; skip_dead = parent.skip_dead; loop_target = parent.loop_target; action_value = parent.action_value; } else { if (IsPassive == false) { if (IsDirect == false) { duration = float.Parse(node.Attributes["duration"].Value); } XmlAttribute checkDistanceAttr = node.Attributes["check_distance"]; if (checkDistanceAttr != null) { check_distance = bool.Parse(checkDistanceAttr.Value); } XmlAttribute skipDeadAttr = node.Attributes["skip_dead"]; if (skipDeadAttr != null) { skip_dead = bool.Parse(skipDeadAttr.Value); } XmlAttribute loopTargetAttr = node.Attributes["loop_target"]; if (loopTargetAttr != null) { loop_target = bool.Parse(loopTargetAttr.Value); } } action_value = new ActionValue(node); } switch (actionType) { case eActionType.shield: { attack_types = new List <eAttackType>(); foreach (var attack_type in node.Attributes["attack_type"].Value.Split(',')) { if (string.IsNullOrEmpty(attack_type) == false) { attack_types.Add((eAttackType)Enum.Parse(typeof(eAttackType), attack_type)); } } } break; case eActionType.immune: { immune_types = new List <eImmuneType>(); foreach (var immune_type in node.Attributes["immune_type"].Value.Split(',')) { if (string.IsNullOrEmpty(immune_type) == false) { immune_types.Add((eImmuneType)Enum.Parse(typeof(eImmuneType), immune_type)); } } attack_types = new List <eAttackType>(); foreach (var attack_type in node.Attributes["attack_type"].Value.Split(',')) { if (string.IsNullOrEmpty(attack_type) == false) { attack_types.Add((eAttackType)Enum.Parse(typeof(eAttackType), attack_type)); } } } break; } if (IsBuff == true || IsPassive) { statType = (eStatType)Enum.Parse(typeof(eStatType), node.Attributes["stat"].Value); if (IsBuff) { XmlAttribute show_messageAttr = node.Attributes["show_message"]; if (show_messageAttr != null) { show_message = bool.Parse(show_messageAttr.Value); } } } //load child node if (IsLevelup) { XmlNode increasePerLevelNode = node.SelectSingleNode("IncreasePerLevel"); if (increasePerLevelNode != null) { increasePerLevel = int.Parse(increasePerLevelNode.Attributes["value"].Value); } } var sub_action_nodes = node.SelectNodes("SubAction"); foreach (XmlNode sub_action_node in sub_action_nodes) { SubActions.Add(new Action(sub_action_node, info, this)); } }
/// <summary> /// Initializes a new instance of the <see cref="IndexusMessage"/> class. /// </summary> /// <param name="id">The id.</param> /// <param name="status">The status.</param> /// <param name="action">The action.</param> /// <param name="priority">The priority.</param> /// <param name="host">The host.</param> /// <param name="expires">The expires.</param> /// <param name="key">The key.</param> /// <param name="payload">The payload.</param> public IndexusMessage(long id, StatusValue status, ActionValue action, CacheItemPriority priority, string host, DateTime expires, string key, byte[] payload) { #region Access Log #if TRACE { Handler.LogHandler.Tracking("Access Method: " + this.GetType().ToString() + "->" + ((object)MethodBase.GetCurrentMethod()).ToString() + " ;"); } #endif #endregion Access Log this.id = id; this.status = status; this.action = action; this.itemPriority = priority; if (!string.IsNullOrEmpty(host)) this.hostname = host; this.expires = expires; this.key = key; this.payload = payload; this.timestamp = DateTime.Now; }
/// <summary> /// /// </summary> /// <param name="currentStep"></param> /// <param name="nextStep"></param> /// <param name="inputList"></param> /// <param name="activityId"></param> /// <param name="accountId"></param> /// <param name="ip"></param> /// <returns></returns> public ActionValue SubmitStep(int currentStep, int nextStep, Input inputList, int activityId, int accountId, string ip) { var fullAccountWorkFlowCache = ObjectFactory.GetInstance <FullAccountWorkFlowCache>(); var fullAccountWorkFlow = fullAccountWorkFlowCache.GetFullAccountWorkFlow(activityId, accountId); //用户工作流对应的 if (fullAccountWorkFlow == null) { throw new Exception("请扫描二维码参与活动!"); } //var userWorkFlowList = AccountWorkFlowDictionary[accountWorkFlowKey]; if (nextStep > fullAccountWorkFlow.BaseModuleList.Count) { throw new Exception("没有定义过程节点!"); } var currentModule = currentStep >= 0 ? fullAccountWorkFlow.BaseModuleList[currentStep] : null; var nextModule = fullAccountWorkFlow.BaseModuleList[nextStep]; //TODO:没找到 if (nextModule == null) { throw new Exception("没有定义下一个过程节点!"); } if (inputList.Count > 0) { foreach (var parameterValue in inputList) { parameterValue.Name = GetParaeterKey(currentModule.Name, parameterValue.Name); //根据前端格式 [ > ] var parameter = fullAccountWorkFlow.Input.FirstOrDefault(z => z.Name == parameterValue.Name); if (parameter == null) { fullAccountWorkFlow.Input.Add(parameterValue); } else { parameter.Value = parameterValue.Value; } } fullAccountWorkFlowCache.UpdateFullAccountWorkFlowToCache(fullAccountWorkFlow); } //开始处理 ActionValue actionValue; if (nextModule is RegisterModule) { var strongModule = nextModule as RegisterModule; actionValue = new ActionValue(ActionValueType.OpenRegister.ToString(), strongModule.Output.ToJson(), currentStep + 1); } else if (nextModule is RegisterImageModule) { var strongModule = nextModule as RegisterImageModule; actionValue = new ActionValue(ActionValueType.OpenRegister.ToString(), strongModule.Output.ToJson(), currentStep + 1); } else if (nextModule is PicContainerApiModule) //图片容器 { var strongModule = nextModule as PicContainerApiModule; if (strongModule.Parameters == null) { throw new Exception("输入参数节点有误{0}!".With(nextModule.Step)); } actionValue = new ActionValue(ActionValueType.ShowPic.ToString(), strongModule.Parameters.Pic, currentStep + 1); } else if (nextModule is ContrastPicModule) //比对结果 { var strongModule = nextModule as ContrastPicModule; if (strongModule.Input.Count < 1) { throw new Exception("输入参数节点有误{0}!".With(nextModule.Step)); } //做对比 var inputValue = strongModule.Input[0].Value.ToString(); //图片容器中的输出参数 var picContainerApiModule = fullAccountWorkFlow.BaseModuleList.FirstOrDefault( z => z is PicContainerApiModule && inputValue.Contains(z.Name)); //计算机视觉中的输出参数 var visionInput = fullAccountWorkFlow.Input.FirstOrDefault(z => z.Name == strongModule.Input[1].Value.ToString()); decimal scale = 0M; if (visionInput is VisionApiParameterSetting && picContainerApiModule != null) { var visionParameters = visionInput.Value as VisionParameters; if (visionParameters == null) { throw new Exception("图片识别错误!"); } var parameters = ((PicContainerApiModule)picContainerApiModule).Parameters; var matchNumber = 0; matchNumber += visionParameters.Result.Count(visionTag => parameters.Result.All(z => z.Name == visionTag.Name)); scale = visionParameters.Result.Count > 0 ? (decimal)matchNumber / visionParameters.Result.Count : 0; } //TODO:计算出输出结果 actionValue = SubmitStep(nextStep, nextStep + 1, new Input() { new NumberParameterSetting("比对结果", Math.Round(scale, 2)) }, activityId, accountId, ip); } else if (nextModule is VisionApiModule) //计算机是视觉API { var strongModule = nextModule as VisionApiModule; var inpuValue = strongModule.Input[0].Value.ToString(); var paraeter = fullAccountWorkFlow.Input.FirstOrDefault(z => z.Name == inpuValue); if (paraeter == null) { LogUtility.WorkFlowModule.Error("VisionApiModule 获取输入参数失败"); throw new Exception("输入参数节点有误{0}!".With(nextModule.Step)); } //调用api var picUrl = paraeter.Value.ToString(); var currentInput = GetVisionInput(picUrl, strongModule.Output[0].Name); actionValue = SubmitStep(nextStep, nextStep + 1, currentInput, activityId, accountId, ip); } else if (nextModule is EmotionApiModule) { var strongModule = nextModule as EmotionApiModule; //TODO:查找参数 var picValue = strongModule.Input[0].Value as string; //TODO:没找到 if (picValue == null || fullAccountWorkFlow.Input.All(z => z.Name != picValue)) { throw new Exception("输入参数节点有误{0}!".With(nextModule.Step)); } //调用微软Azure 情绪感知接口 var currentInput = GetMoodsApiModuleInputAsync(fullAccountWorkFlow, picValue, nextModule).Result;//TODO:后期可以改用异步方法 actionValue = SubmitStep(nextStep, nextStep + 1, currentInput, activityId, accountId, ip); } else if (nextModule is RedPackageResultModule) //红包结果 { var strongModule = nextModule as RedPackageResultModule; //最大红包金额 //最小红包金额 var valueKey = strongModule.Input[0].Value as string; var maxRedPackageMoney = Convert.ToDecimal(strongModule.Input[1].Value); var minRedPackageMoney = Convert.ToDecimal(strongModule.Input[2].Value); var parameter = fullAccountWorkFlow.Input.FirstOrDefault(z => z.Name == valueKey); if (!(parameter is NumberParameterSetting)) { throw new Exception("输入参数节点有误{0}!".With(nextModule.Step)); } var value = 0M; Decimal.TryParse(parameter.Value.ToString(), out value); var money = value * maxRedPackageMoney; var totalMoney = money > minRedPackageMoney ? money : minRedPackageMoney; SendActivityReward(activityId, accountId, totalMoney, nextModule, fullAccountWorkFlow.Input, ip); var resultData = new { Money = totalMoney, ActivityId = activityId, Name = parameter.Name, Value = value * 100 }; actionValue = new ActionValue(ActionValueType.OpenResult.ToString(), resultData.ToJson(), strongModule.Step + 1); //TODO:清除用户该活动缓存 } else if (nextModule is GradeResultModule) //评分结果 { var strongModule = nextModule as GradeResultModule; var valueKey = strongModule.Input[0].Value as string; var parameter = fullAccountWorkFlow.Input.FirstOrDefault(z => z.Name == valueKey); if (!(parameter is NumberParameterSetting)) { throw new Exception("输入参数节点有误{0}!".With(nextModule.Step)); } var gradeScore = 0M; decimal.TryParse(parameter.Value.ToString(), out gradeScore); var money = Math.Round(gradeScore, 4) * 100;//评分百分比 //保存记录 var appRedPackageActivityAwardLogService = ObjectFactory.GetInstance <APP_RedPackage_Activity_LogService>(); appRedPackageActivityAwardLogService.CreateAppRedPackageActivityAwardLog(activityId, accountId, money, "评分", fullAccountWorkFlow.Input.SerializeToString()); //返回前端数据 var resultData = new { GradeScore = gradeScore, ActivityId = activityId, Name = parameter.Name, Value = gradeScore * 100 }; actionValue = new ActionValue(ActionValueType.OpenResult.ToString(), resultData.ToJson(), strongModule.Step + 1); } else { throw new Exception("步骤错误!"); } return(actionValue); }
public Dictionary <stateType, double> DijkstraDistances(stateType startState, ActionValue <stateType, actionType> model, List <actionType> availableActions) { Dictionary <stateType, double> dist = new Dictionary <stateType, double>(stateComparer); dist.Add(startState, 0); Dictionary <stateType, stateType> route = new Dictionary <stateType, stateType>(stateComparer); List <stateType> open = new List <stateType>(); open.Add(startState); List <stateType> closed = new List <stateType>(); while (open.Count > 0) { // remove the lowest-cost entry from the Q list stateType n = open.First(); for (int i = 1; i < open.Count; i++) { if (dist[n] < dist[open[i]]) { n = open[i]; } } open.Remove(n); closed.Add(n); // identify all neighbors foreach (actionType act in availableActions) { List <stateType> neighbors = new List <stateType>(); //if (stochasticity) // neighbors.AddRange(lowerLevelModel.PredictNextStates(candidate, act).Keys); //else neighbors.Add(model.PredictNextState(n, act)); // evaluate each neighbor foreach (stateType neighbor in neighbors) { if (neighbor == null) // if there is no knowledge of what this action does { continue; } if (!dist.ContainsKey(neighbor)) { dist.Add(neighbor, double.PositiveInfinity); } if ((dist[n] + 1) < dist[neighbor]) { dist[neighbor] = dist[n] + 1; route[neighbor] = n; } if (!open.Contains(neighbor, stateComparer) && !closed.Contains(neighbor, stateComparer)) { open.Add(neighbor); } } } } return(dist); }
/// <summary> /// Copies the specified MSG. /// </summary> /// <param name="msg">The MSG.</param> private void Copy(IndexusMessage msg) { #region Access Log #if TRACE { Handler.LogHandler.Tracking("Access Method: " + this.GetType().ToString() + "->" + ((object)MethodBase.GetCurrentMethod()).ToString() + " ;"); } #endif #endregion Access Log if (msg == null) { this.action = ActionValue.Error; throw new ArgumentNullException("IndexusMessage cannot be NULL"); } this.itemPriority = msg.ItemPriority; this.id = msg.id; this.action = msg.action; this.key = msg.key; this.payload = msg.payload; this.expires = msg.expires; this.status = msg.status; this.timestamp = msg.timestamp; }
/// <summary> /// Sets the bytes. /// </summary> /// <param name="stream">The stream.</param> /// <returns></returns> public bool SetBytes(MemoryStream stream) { #region Access Log #if TRACE { Handler.LogHandler.Tracking("Access Method: " + this.GetType().ToString() + "->" + ((object)MethodBase.GetCurrentMethod()).ToString() + " ;"); } #endif #endregion Access Log BinaryReader br = new BinaryReader(stream); this.status = (StatusValue)br.ReadByte(); this.action = (ActionValue)br.ReadByte(); this.id = br.ReadInt64(); this.itemPriority = (CacheItemPriority)br.ReadByte(); // Modified: 11-01-2008 SharedCache.com, rschuetz : pre_release_1.0.2.132 http://www.codeplex.com/SharedCache/WorkItem/View.aspx?WorkItemId=5046 this.expires = Formatters.DateTimeUnix.DateTimeFromUnixTime((long)br.ReadUInt64()); // Formatters.DateTimeUnix.ToDateTime(br.ReadDouble()); this.timestamp = Formatters.DateTimeUnix.DateTimeFromUnixTime((long)br.ReadUInt64()); // Formatters.DateTimeUnix.ToDateTime(br.ReadDouble()); int dataLength1 = br.ReadInt32(); byte[] buf1 = new byte[dataLength1]; int read1 = br.Read(buf1, 0, dataLength1); this.key = System.Text.Encoding.UTF8.GetString(buf1); int dataLength = br.ReadInt32(); if (dataLength > 0) { byte[] buf = new byte[dataLength]; int read = br.Read(buf, 0, dataLength); this.payload = buf; buf = null; } return true; }
// Assumes deterministic transitions public List <Tuple <stateType, actionType, double> > AStar(stateType lowerLevelState, List <stateType> goals, ActionValue <stateType, actionType> lowerLevelModel, List <actionType> availableActions, bool stochasticity) { // from the higher level goal state, generate the list of goal states at this level //List<stateType> goals = getLowerLevelStatesMethod(higherLevelGoal); //goals.Add(new int[2] { higherLevelGoal[0] << 1, higherLevelGoal[1] << 1 }); //goals.Add(new int[2] { (higherLevelGoal[0] << 1) + 1, higherLevelGoal[1] << 1 }); //goals.Add(new int[2] { higherLevelGoal[0] << 1, (higherLevelGoal[1] << 1) + 1 }); //goals.Add(new int[2] { (higherLevelGoal[0] << 1) + 1, (higherLevelGoal[1] << 1) + 1 }); //// generate the list of allowed states the path can pass through //int[] higherLevelState = new int[2] {lowerLevelState[0] >> 1, lowerLevelState[1] >> 1}; //List<int[]> allowedStates = new List<int[]>(); //allowedStates.Add(new int[2] { higherLevelState[0] << 1, higherLevelState[1] << 1 }); //allowedStates.Add(new int[2] { (higherLevelState[0] << 1) + 1, higherLevelState[1] << 1 }); //allowedStates.Add(new int[2] { higherLevelState[0] << 1, (higherLevelState[1] << 1) + 1 }); //allowedStates.Add(new int[2] { (higherLevelState[0] << 1) + 1, (higherLevelState[1] << 1) + 1 }); //allowedStates.AddRange(goals); // search for a route to a goal state Dictionary <stateType, Tuple <stateType, actionType, double> > cameFrom = new Dictionary <stateType, Tuple <stateType, actionType, double> >(stateComparer); List <stateType> closed = new List <stateType>(); PriorityQueue <double, stateType> open = new PriorityQueue <double, stateType>(); Dictionary <stateType, double> gScore = new Dictionary <stateType, double>(stateComparer); gScore.Add(lowerLevelState, 0); Dictionary <stateType, double> fScore = new Dictionary <stateType, double>(stateComparer); fScore.Add(lowerLevelState, gScore[lowerLevelState] + heuristic(lowerLevelState, lowerLevelModel, availableActions)); open.Enqueue(0, lowerLevelState); while (!open.IsEmpty) { stateType candidate = open.Dequeue(); if (goals.Contains(candidate, stateComparer)) { List <Tuple <stateType, actionType, double> > path = reconstructPath(candidate, cameFrom); //expectedReward = -(getGorF(candidate, gScore) * 2 - 1 * path.Count); return(path); } closed.Add(candidate); // identify all possible neighbors foreach (actionType act in availableActions) { List <stateType> neighbors = new List <stateType>(); if (stochasticity) { neighbors.AddRange(lowerLevelModel.PredictNextStates(candidate, act).Keys); } else { neighbors.Add(lowerLevelModel.PredictNextState(candidate, act)); } // evaluate each neighbor foreach (stateType neighbor in neighbors) { //stateType neighbor = lowerLevelModel.PredictNextState(candidate, act); if (neighbor == null) // if there is no knowledge of what this action does { continue; } //if (!allowedStates.Contains(neighbor, comparer)) //{ // continue; //} if (closed.Contains(neighbor, stateComparer) || open.Contains(neighbor, stateComparer)) { continue; } double reward = lowerLevelModel.PredictReward(candidate, act, neighbor); double cost = -reward / 2 + 0.5; // ****** assumes the reward is between -1 and 1 double tentativeG = getGorF(candidate, gScore) + cost; if (!open.Contains(neighbor, stateComparer) || (tentativeG < getGorF(neighbor, gScore))) { cameFrom[neighbor] = new Tuple <stateType, actionType, double>(candidate, act, reward); gScore[neighbor] = tentativeG; fScore[neighbor] = gScore[neighbor] + heuristic(neighbor, lowerLevelModel, availableActions); if (!open.Contains(neighbor, stateComparer)) { open.Enqueue(getGorF(neighbor, fScore), neighbor); } } } } } return(new List <Tuple <stateType, actionType, double> >()); }
/// <summary> /// Initializes a new instance of the <see cref="IndexusMessage"/> class. /// </summary> /// <param name="id">The id.</param> /// <param name="value">The value.</param> public IndexusMessage(long id, ActionValue value) { #region Access Log #if TRACE { Handler.LogHandler.Tracking("Access Method: " + this.GetType().ToString() + "->" + ((object)MethodBase.GetCurrentMethod()).ToString() + " ;"); } #endif #endregion Access Log this.id = id; this.action = value; }
private List <Goal <int[], actionType> > path2subgoals(List <Tuple <int[], actionType, double> > path, int level, ActionValue <int[], actionType> model) { List <Goal <int[], actionType> > subgoals = new List <Goal <int[], actionType> >(); for (int i = 0; i < path.Count - 1; i++) { subgoals.Add(new Goal <int[], actionType>(level, path[i].Item1, path[i].Item2, path[i + 1].Item1, path[i].Item3, stateComparer, actionComparer)); } return(subgoals); }