public Model UpdateModel(State currentState, AIAction action, State resultState, double reward) { if (currentState.IsEqual(resultState)) { return(this); } List <KeyValuePair <StateAction, StateReward> > result = new List <KeyValuePair <StateAction, StateReward> >(ModelWorld); StateAction stateAction; StateReward stateReward; stateAction.state = currentState; stateAction.action = action; stateReward = result.Find(x => x.Key.action.IsEqual(action) && x.Key.state.IsEqual(currentState) && x.Value.state.IsEqual(resultState)).Value; if (stateReward == null) { stateReward = new StateReward(); stateReward.reward = reward; stateReward.state = resultState; result.Add(new KeyValuePair <StateAction, StateReward>(stateAction, stateReward)); return(new Model(result)); } if (Math.Abs(stateReward.reward) <= reward) { stateReward.reward = reward; } return(new Model(result)); }
private void BackwardUpdatePriorityQueue(Model model, StateAction currentStateAction) { foreach (var item in model.FindPreviousStateActions(currentStateAction.state)) { StateReward stateReward = model.GetStateReward(currentStateAction); if (stateReward == null) { return; } double priority = getPriority(currentStateAction.state, item.state, item.action, stateReward.reward); if (Math.Abs(priority) > 0.0001) { priorityQueue.Push(priority, item); } } }
public void Plan(PlanningDetails details) { double priority = getPriority(details.currentState, details.nextState, details.action, details.reward); if (Math.Abs(priority) > 0.0001) { priorityQueue.Push(priority, details.CurrentStateAction); } for (int i = 0; i < details.cycleRate && !priorityQueue.IsEmpty(); i++) { StateAction currentStateAction = priorityQueue.Pop(); StateReward nextStateReward = details.model.GetStateReward(currentStateAction); if (nextStateReward != null) { StepDetails stepDetails; stepDetails.currentState = currentStateAction.state; stepDetails.action = currentStateAction.action; stepDetails.nextState = nextStateReward.state; stepDetails.reward = nextStateReward.reward; updateValues(stepDetails); } BackwardUpdatePriorityQueue(details.model, currentStateAction); } }