Ejemplo n.º 1
0
        public void Estimate(double agentX, double agentY, double agentVx, double agentVy,
                             double puckX, double puckY, double puckVx, double puckVy, double PuckR,
                             double oppX, double oppY, double oppVx, double oppVy
                             )
        {
            AHEntities.SenseEventType newEvent = EstimatorStrategy.Estimate(agentX, agentY, agentVx, agentVy,
                                                                            puckX, puckY, puckVx, puckVy, PuckR, oppX, oppY, oppVx, oppVy, WM);

            if (newEvent == SenseEventType.NoEvent)
            {
                return;
            }

            SenseEventArgs senseEvent;

            mLogger.AddLogMessage("Perception: new event detected: " + newEvent.ToString() +
                                  ", puck velocity: (" + puckVx.ToString() + "," + puckVy.ToString() + ")", true);
            if ((newEvent == SenseEventType.yWall) && (puckVx < 0))
            {
                mLogger.AddLogMessage("Perception: Invoking Refining Of Current Plan");
                senseEvent = new SenseEventArgs(SensePlanArg.Refine, newEvent);
            }
            else
            {
                mLogger.AddLogMessage("Perception: Invoking New Planing");
                senseEvent = new SenseEventArgs(SensePlanArg.Plan, newEvent);
            }
            OnEstimationUpdate(this, senseEvent);
        }
Ejemplo n.º 2
0
        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");
        }
Ejemplo n.º 3
0
        public void UpdateGameStrategy()
        {
            // plan "new" strategy
            Dictionary <string, double> s = StrategyPlannerStrategy.PlanStrategy();

            // check if old strategy has changed
            if (StrategyEquals(s, strategy) == false)
            {
                strategy = s;
                mLogger.AddLogMessage("Strategics: New strategy planned: " + PrintStrategy(s));

                // update World Model
                WM.UpdateStrategy(s["GameStyle"], s["PLeft"], s["PMiddle"], s["PRight"]);
            }
        }
Ejemplo n.º 4
0
        public void StartNewPlan(object sender, EventArgs e)
        {
            ActionDirective             tempA;
            SenseEventArgs              senseE    = (SenseEventArgs)e;
            Dictionary <string, double> gameState = WM.GetPhysicalState();
            Point puckV = new Point(gameState["PuckVx"], gameState["PuckVy"]);

            if ((senseE.EventType == SenseEventType.yWall) && (puckV.X > 0))
            {
                // no replan (prepare action)
                currentEvent = senseE.EventType;
                mLogger.AddLogMessage("Tactics: No New Action Required: " + currentActionDirective.Action.ToString());
            }
            else
            {
                //new plan
                if ((currentEvent == senseE.EventType) &&
                    ((currentEvent == SenseEventType.StuckPlayer) || (currentEvent == SenseEventType.StuckAgent)))
                {
                    // no replan
                    mLogger.AddLogMessage("Tactics: No New Action Required: " + currentActionDirective.Action.ToString());
                }
                else if ((senseE.EventType == SenseEventType.yWall) && (puckV.X < 0))
                {
                    // need to replan current action
                    currentEvent = senseE.EventType;
                    mLogger.AddLogMessage("Tactics: Refining Old Action: " + currentActionDirective.Action.ToString());
                    PlanningScheduler.MakePlanRequest(senseE.EventType);
                }
                else
                {
                    currentEvent = senseE.EventType;
                    tempA        = actionSelectionStrategy.SelectAction(senseE.EventType);
                    //if (tempA.Action != currentActionDirective.Action)
                    //{
                    lock (currentActionDirective.ActionDirectiveLock)
                    {
                        currentActionDirective.Action    = tempA.Action;
                        currentActionDirective.TimeStamp = senseE.TimeStamp;
                        currentActionDirective.Duration  = tempA.Duration - (DateTime.Now - senseE.TimeStamp);
                    }
                    // log action - new action
                    mLogger.AddLogMessage("Tactics: New Action Selected: " + currentActionDirective.Action.ToString());
                    PlanningScheduler.MakePlanRequest(senseE.EventType);

                    /*}
                     * else
                     * {
                     *  mLogger.AddLogMessage("Tactics: Old Action Selected: " + currentActionDirective.Action.ToString());
                     *  PlanningScheduler.MakePlanRequest(senseE.EventType);
                     * }*/
                }
            }


            /*
             * // new plan required
             * if ((senseE.EventType == SenseEventType.AgentCollision) || (senseE.EventType == SenseEventType.OpponentCollision) ||
             *  (senseE.EventType == SenseEventType.xWall) || (senseE.EventType == SenseEventType.yWall) && (puckV.X < 0))
             * {
             *  tempA = actionSelectionStrategy.SelectAction(senseE.EventType);
             *  if (tempA.Action != currentActionDirective.Action)
             *  {
             *      lock (currentActionDirective.ActionDirectiveLock)
             *      {
             *          currentActionDirective.Action = tempA.Action;
             *          currentActionDirective.TimeStamp = senseE.TimeStamp;
             *          currentActionDirective.Duration = tempA.Duration - (DateTime.Now - senseE.TimeStamp);
             *      }
             *      // log action - new action
             *      mLogger.AddLogMessage("Tactics: New Action Selected: " + currentActionDirective.Action.ToString());
             *      PlanningScheduler.MakePlanRequest(senseE.EventType);
             *  }
             *  else
             *  {
             *      mLogger.AddLogMessage("Tactics: Old Action Selected: " + currentActionDirective.Action.ToString());
             *      PlanningScheduler.MakePlanRequest(senseE.EventType);
             *  }
             * }
             * else
             * {
             *  // log action
             *  mLogger.AddLogMessage("Tactics: Refinement Of Active Action: " + currentActionDirective.Action.ToString());
             *  PlanningScheduler.MakePlanRequest(senseE.EventType);
             * }*/



            /*
             * if (senseE.PlanType == SensePlanArg.Plan)
             * {
             *  tempA = actionSelectionStrategy.SelectAction(senseE.EventType);
             *  // construct the action directive object
             *  if (tempA.Action != currentActionDirective.Action)
             *  {
             *      lock (currentActionDirective.ActionDirectiveLock)
             *      {
             *          currentActionDirective.Action = tempA.Action;
             *          currentActionDirective.TimeStamp = senseE.TimeStamp;
             *          currentActionDirective.Duration = tempA.Duration - (DateTime.Now - senseE.TimeStamp);
             *      }
             *      // log action - new action
             *      mLogger.AddLogMessage("Tactics: New Action Selected: " + currentActionDirective.Action.ToString());
             *  }
             *  else
             *  {
             *      if ((currentActionDirective.Action != AHEntities.Action.PREPARE))
             *      {
             *          // log action - refinement of current action.
             *          mLogger.AddLogMessage("Tactics: New (reselection) Action Selected: " + currentActionDirective.Action.ToString());
             *      }
             *  }
             *  PlanningScheduler.MakePlanRequest(senseE.EventType);
             * }
             * else
             * {
             *  // current directive is still valid, need to schedule replanning
             *  // log action
             *  mLogger.AddLogMessage("Tactics: Refinement Of Active Action: " + currentActionDirective.Action.ToString());
             *  PlanningScheduler.MakePlanRequest(senseE.EventType);
             * }*/
        }
        public override PointParams ActionPlanning(AHEntities.ActionDirective action, bool isNewPlaning)
        {
            AHEntities.Action A = action.Action;
            PointParams       finalConditions = null;
            Random            random = new Random();
            Point             puckP, puckV;

            physicalState = worldModel.GetPhysicalState();
            puckP         = new Point(physicalState["PuckX"], physicalState["PuckY"]);
            puckV         = new Point(physicalState["PuckVx"], physicalState["PuckVy"]);
            global        = worldModel.GetSize();
            Point crossing;

            double[] crossParams;

            switch (A)
            {
            case AHEntities.Action.BLOCK:
                #region BLOCK
                crossParams = AHEntities.EstimateLineCrossing.Estimate(puckP, puckV, rp,
                                                                       AHEntities.EstimateLineCrossing.blockLine, global[0], global[1]);
                crossing = new Point(AHEntities.EstimateLineCrossing.blockLine, crossParams[0]);
                Point defendPoint = AHEntities.EstimateLineCrossing.CalculateActionPoint(crossing,
                                                                                         new Point(crossParams[2], crossParams[3]), rp, rm);
                mLogger.AddLogMessage("LowLevel Planning BLOCK: estimated collission: mallet: " + defendPoint.ToString() +
                                      ", puck: " + crossing.ToString());
                finalConditions = new PointParams();
                finalConditions.AddParameters(defendPoint);
                finalConditions.AddParameters(new Point(0, 0));
                finalConditions.T = DateTime.Now + TimeSpan.FromSeconds(crossParams[1]);
                break;

                #endregion BLOCK
            case AHEntities.Action.LEAVE:
                #region LEAVE
                finalConditions = null;
                break;

                #endregion LEAVE
            case AHEntities.Action.ATTACK_RIGHT:
                #region ATTACK_RIGHT
                //finalConditions = CalculateDirectedAttack(AHEntities.Action.ATTACK_RIGHT, puckP, puckV, physicalState["PuckR"],
                //                  AHEntities.EstimateLineCrossing.attackLine, global[0], global[1]);
                //break;
                #endregion ATTACK_RIGHT
            case AHEntities.Action.ATTACK_LEFT:
                #region ATTACK_LEFT
                //finalConditions = CalculateDirectedAttack(AHEntities.Action.ATTACK_LEFT, puckP, puckV, physicalState["PuckR"],
                //                  AHEntities.EstimateLineCrossing.attackLine, global[0], global[1]);
                //break;
                #endregion ATTACK_LEFT
            case AHEntities.Action.ATTACK_MIDDLE:
                #region ATTACK_MIDDLE
                crossParams = AHEntities.EstimateLineCrossing.Estimate(puckP, puckV, rp,
                                                                       AHEntities.EstimateLineCrossing.attackLine, global[0], global[1]);
                mLogger.AddLogMessage("LowLevel: Puck Estimated at: (" + AHEntities.EstimateLineCrossing.attackLine.ToString() + "," + crossParams[0].ToString() + ") in " + crossParams[1].ToString() + " seconds");
                Point  puckPline = new Point(AHEntities.EstimateLineCrossing.attackLine, crossParams[0]);
                double velocity  = 1000;
                finalConditions = CalculateNaiveDirectedAttack(A, puckPline, crossParams[1],
                                                               AHEntities.EstimateLineCrossing.attackLine, rp, rm, global[0], global[1], velocity);
                break;

                #endregion ATTACK_MIDDLE
            case AHEntities.Action.DEFENSE_ATTACK:
                #region DEFENSE_ATTACK
                if (Math.Abs(puckV.X) < 0.01)
                {
                    break;
                }
                crossParams = AHEntities.EstimateLineCrossing.Estimate(puckP, puckV, rp,
                                                                       AHEntities.EstimateLineCrossing.defenseAttackLine, global[0], global[1]);
                if (crossParams == null)
                {
                    finalConditions = null;
                    break;
                }
                crossing = new Point(AHEntities.EstimateLineCrossing.defenseAttackLine, crossParams[0]);
                Point attackPoint = AHEntities.EstimateLineCrossing.CalculateActionPoint(new Point(AHEntities.EstimateLineCrossing.defenseAttackLine, crossParams[0]),
                                                                                         new Point(crossParams[2], crossParams[3]), rp, rm);
                mLogger.AddLogMessage("LowLevel Planning DEFENSE_ATTACK: estimated collission: mallet: " + attackPoint.ToString() +
                                      ", puck estimated at: " + crossing.ToString());

                finalConditions = new PointParams();
                finalConditions.AddParameters(attackPoint);
                double yvel = (attackPoint.Y > 0) ? (yvel = -200) : (yvel = 200);
                finalConditions.AddParameters(new Point(500, yvel));
                finalConditions.T = DateTime.Now + TimeSpan.FromSeconds(crossParams[1]);
                break;

                #endregion DEFENSE_ATTACK
            case AHEntities.Action.PREPARE:
                #region PREPARE
                finalConditions = new PointParams();
                finalConditions.AddParameters(new Point(-1000, 0));
                finalConditions.AddParameters(new Point(0, 0));
                finalConditions.T = action.TimeStamp + TimeSpan.FromSeconds(0.5);
                break;

                #endregion PREPARE
            case AHEntities.Action.STUCK_ATTACK:
                #region STUCK_ATTACK
                double stuckTime;
                stuckTime = (action.Duration - (DateTime.Now - action.TimeStamp)).TotalSeconds;
                Point stuckAttackPoint = AHEntities.EstimateLineCrossing.EstimateStuck(puckP, puckV, rp, global[0], global[1], stuckTime, rm);
                if (stuckAttackPoint == null)
                {
                    finalConditions = null;
                    break;
                }
                mLogger.AddLogMessage("LowLevel Planning STUCK_ATTACK: estimated collission: mallet: " + stuckAttackPoint.ToString());
                finalConditions = new PointParams();
                finalConditions.AddParameters(stuckAttackPoint);
                finalConditions.AddParameters(new Point(0, 0));
                finalConditions.T = action.TimeStamp + TimeSpan.FromSeconds(stuckTime);
                break;

                #endregion STUCK_ATTACK
            default:
                #region default
                finalConditions = null;
                break;
                #endregion default
            }

            //saving current planning (action and parameters)
            lastAction = A;
            if (finalConditions != null)
            {
                if (lastPlan == null)
                {
                    lastPlan = new PointParams();
                }
                lastPlan.Clear();
                lastPlan.AddParameters(finalConditions.GetParamsByIndex(1));
                lastPlan.AddParameters(finalConditions.GetParamsByIndex(2));
                lastPlan.T = finalConditions.T;
            }
            else
            {
                lastPlan = null;
            }

            return(finalConditions);
        }