public OnDemandActionPlanningSchedulingStrategy(double period, ActionDirective action, LowLevelLayer lowLevel) { SceduelingPeriod = TimeSpan.FromSeconds(period); A = action; lowLevelLayer = lowLevel; newPlanRequest = false; }
public override void PlanningScheduler() { while (internalState == ModuleState.Active) { if (newPlanRequest) { newPlanRequest = false; lock (A.ActionDirectiveLock) { lastA = new ActionDirective(A); } // new planning of action lowLevelLayer.PlanNewAction(A, true); } else { if (DateTime.Compare(DateTime.Now, A.TimeStamp + A.Duration) < 0) { // replanning current action only if it's an attack if (IsDirectedAttack(A)) { lowLevelLayer.PlanNewAction(A, false); } } } Thread.Sleep(SceduelingPeriod); } }
public TacticLayer(WorldModel worldModel, LowLevelLayer lowLevel) { actionSelectionStrategy = new FuzzyActionSelectionStrategy(worldModel); WM = worldModel; lowLevelLayer = lowLevel; mLogger = Logger.Logger.Instance; internalState = ModuleState.Inactive; Hashtable consts = WM.GetConstants(); currentActionDirective = new ActionDirective(AHEntities.Action.LEAVE, DateTime.Now); //PlanningScheduler = new OnDemandActionPlanningSchedulingStrategy(0.02, currentActionDirective, lowLevel); PlanningScheduler = new PeriodicActionPlanningSchedulingStrategy((double)consts["PlanPeriod"], currentActionDirective, lowLevel); }
public void PlanNewAction(ActionDirective A, bool isNewPlanning) { planTime.Restart(); // target point parameters (for the whole motion) //if (isNewPlanning) //bounderyConditions = actionPlanningStrategy.ActionPlanning(A, isNewPlanning); bounderyConditions = actionPlanningStrategy.ActionPlanning(A, true); if (bounderyConditions == null) { return; } state = WM.GetPhysicalState(); Point agentP = new Point(state["AgentX"], state["AgentY"]); Point agentV = new Point(state["AgentVx"], state["AgentVy"]); mLogger.AddLogMessage("LowLevel: action planned: agent currently at: " + agentP.ToString() + " and: " + agentV.ToString() + ", target: " + bounderyConditions.ToString()); // initial movement parameters PointParams initialConditions = new PointParams(); initialConditions.AddParameters(agentP); initialConditions.AddParameters(agentV); initialConditions.T = DateTime.Now; // new trajectory generation double time = (bounderyConditions.T - initialConditions.T).TotalSeconds; double[][,] newTrajectory = null; if ((isNewPlanning) || ((time < maxTime) && (time > 0))) { newTrajectory = trajectoryPlanningStrategy.TrajectoryPlanning(initialConditions, bounderyConditions); } if (newTrajectory != null) { mLogger.AddLogMessage("LowLevel: New Trajectory Designed, Length: " + newTrajectory[0].LongLength.ToString() + " time: " + time.ToString()); commandsQueue.Replace(QueueType.Position, newTrajectory[0]); // output the whole trajectory to log //mLogger.AddLogMessage("LowLevel: Trajectory: " + commandsQueue.PositionToString()); commandsQueue.Replace(QueueType.Velocity, newTrajectory[1]); } planTime.Stop(); mLogger.AddLogMessage("LowLevel: Planning time was: " + planTime.Elapsed.TotalSeconds.ToString() + " Seconds"); }
private bool IsDirectedAttack(ActionDirective ad) { switch (ad.Action) { case Action.ATTACK_LEFT: return(true); case Action.ATTACK_MIDDLE: return(true); case Action.ATTACK_RIGHT: return(true); case Action.DEFENSE_ATTACK: return(true); default: return(false); } }
public override AHEntities.ActionDirective SelectAction(SenseEventType planReason) { Dictionary <string, double> physicalState = WM.GetPhysicalState(); double[] crossParamsGoal, crossParamsAttack, crossParamsDefense; bool isThreat = false; double time2goal = 0; double time2attack = 0; double time2defense = 0; AHEntities.ActionDirective action = new ActionDirective(); action.TimeStamp = DateTime.Now; #region stuck events handling if (planReason == SenseEventType.StuckPlayer) { action.Action = AHEntities.Action.PREPARE; action.Duration = TimeSpan.FromSeconds(1); return(action); } if (planReason == SenseEventType.StuckAgent) { action.Action = AHEntities.Action.STUCK_ATTACK; action.Duration = TimeSpan.FromSeconds(0.3); return(action); } #endregion stuck events handling #region Crossing calculations Point puckP = new Point(physicalState["PuckX"], physicalState["PuckY"]); Point puckV = new Point(physicalState["PuckVx"], physicalState["PuckVy"]); try { // (y, T) crossParamsGoal = AHEntities.EstimateLineCrossing.Estimate(puckP, puckV, 32, AHEntities.EstimateLineCrossing.goalLine, (int)global["Tablewidth"], (int)global["Tableheight"]); crossParamsDefense = AHEntities.EstimateLineCrossing.Estimate(puckP, puckV, 32, AHEntities.EstimateLineCrossing.defenseAttackLine, (int)global["Tablewidth"], (int)global["Tableheight"]); crossParamsAttack = AHEntities.EstimateLineCrossing.Estimate(puckP, puckV, 32, AHEntities.EstimateLineCrossing.attackLine, (int)global["Tablewidth"], (int)global["Tableheight"]); } catch (Exception) { action.Action = AHEntities.Action.LEAVE; action.Duration = TimeSpan.FromSeconds(0); return(action); } #endregion Crossing calculations // not directed to the goal if (crossParamsGoal == null) { action.Action = AHEntities.Action.PREPARE; action.Duration = TimeSpan.FromSeconds(1); return(action); } // is directed to the goal if ((crossParamsGoal[0] < 220) && (crossParamsGoal[0] > -220)) { isThreat = true; } // time to reach the goal and attack lines time2goal = crossParamsGoal[1]; time2defense = crossParamsDefense[1]; time2attack = crossParamsAttack[1]; #region emergency Block/Leave // puck is too fast, immidiate BLOCK or LEAVE if (time2goal < 0.2) { if (isThreat) { action.Action = AHEntities.Action.BLOCK; action.Duration = TimeSpan.FromSeconds(time2goal); return(action); } else { action.Action = AHEntities.Action.LEAVE; action.Duration = TimeSpan.FromSeconds(0); return(action); } } #endregion emergency Block/Leave if (time2attack < 0.5) // defense attack (no time to calcualte elaborated attack { action.Action = AHEntities.Action.DEFENSE_ATTACK; action.Duration = TimeSpan.FromSeconds(time2defense); return(action); } else // Directed attack { int height = (int)global["Tableheight"] / 2; double rim = height * 0.95; action.Duration = TimeSpan.FromSeconds(time2attack); // puck is crossing on the rim of the table if (Math.Abs(crossParamsAttack[0]) > rim) { action.Action = AHEntities.Action.DEFENSE_ATTACK; action.Duration = TimeSpan.FromSeconds(time2defense); return(action); } else // crossing on the inside of the table { Random random = new Random(); AHEntities.Action[] attacks; double outerTable = height * 0.65; if (Math.Abs(crossParamsAttack[0]) > outerTable) // outer part { if (crossParamsAttack[2] > height - outerTable) // Agent's left part { attacks = new AHEntities.Action[] { AHEntities.Action.ATTACK_RIGHT, AHEntities.Action.ATTACK_MIDDLE }; } else // Agent's right part { attacks = new AHEntities.Action[] { AHEntities.Action.ATTACK_LEFT, AHEntities.Action.ATTACK_MIDDLE }; } } else // inner part { attacks = new AHEntities.Action[] { AHEntities.Action.ATTACK_LEFT, AHEntities.Action.ATTACK_MIDDLE, AHEntities.Action.ATTACK_RIGHT }; } action.Action = attacks[random.Next(attacks.Length)]; return(action); } } }