Skip to content

uocsclub/AI_Code_And_Coffee

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pre-Requisite


What is AI

  • Breaking misconceptions
    • This isn't a new field
      • Decision trees
      • Rule based Agents
      • Neural Networks
      • LLMs
    • Not hard or fast rule in-terms of definition
      • Marketing
      • Changing goal post
      • Generally an algorithm used for decision making

REST

  • representational state transfer
  • Decoupled interface
    • Server nor the client care about the implementation of the other implementation
    • Doesn't matter what language you use
  • Ideally stateless
    • Kinda a lie - nothing is totally stateless with servers & implementations

Over-view

import uvicorn
import argparse
from fastapi import FastAPI, Request
import json
import math
from math import pi

from typing import Tuple

app = FastAPI()

# In-memory storage for game data (for simplicity)
games = {}

@app.post('/start_game')
async def start_game(request: Request):
  # Setting Up state
  return {'message': f'Game {game_id} started successfully', 'game_id': game_id}

@app.post('/brain')
async def brain(request: Request):
  #AI logic
  return {"action": f"rotate_{games[game_id]['turning']}"}

@app.post('/win')
async def win(request: Request):
  #cleaning up state
  return {'message': f'Game {game_id} ended successfully', 'game_id': game_id}

@app.post('/loss')
async def loss(request: Request):
  #cleaning up state
  return json.dumps({'message': f'Game {game_id} ended successfully', 'game_id': game_id})

if __name__ == '__main__':
  # Argument parser for handling the port input
  parser = argparse.ArgumentParser(description="Flask Tank Game AI")
  parser.add_argument('--port', type=int, default=5000, help='Port to run the server on')
  
  # Parse the arguments
  args = parser.parse_args()
  
  # Run the app on the specified port
  uvicorn.run(app, host="0.0.0.0", port=args.port)


Behaviors


  • There are many ways conceptualize how to structure an AI & Agents

Agents

  • Precises and acts upon it's environment
  • Simple as thermostat


image info


  • LangChain
    • Utilizes LLMs to choose between tools, other LLMs to complete complex tasks
    • RAGS (Retrieval Augmented Generation)

BDI

  • Software architecture
  • Bridge Human thinking to an algorithm

Belief

  • State of an agent
  • Example
    • Current temperature
    • Visual data
    • Memory
  • BeliefSet is a dataset of beliefs

@app.post('/start_game')
async def start_game(request: Request):
    data = await request.json()
    game_id = data.get('game_id')

    ...
    # Initializing beliefSet for a game
    games[game_id] = {
        'status': 'active',
        'counting': 0, # internal counter
        'turning': False, # Am I turning
        'old_pos': (0, 0), # Where I was
        'old_rot': 0 # Previous baring
    }
  ...
  return {...}

@app.post('/brain')
async def brain(request: Request):
  data = await request.json()
  ...
  
  counting = games[game_id]["counting"]
  turning = games[game_id]["turning"]
  old_pos = games[game_id]["old_pos"]
  old_rot = games[game_id]["old_rot"]

  current_heading = data["rot"] # Where i'm looking right now
  current_pos = data["pos"] # Where i am right now
  
  # What i see right now
  sensors = {
      "n": data["hull_vision"][0],
      "ne": data["hull_vision"][1],
      "e": data["hull_vision"][2],
      "se": data["hull_vision"][3],
      "s": data["hull_vision"][4],
      "sw": data["hull_vision"][5],
      "w": data["hull_vision"][6],
      "nw": data["hull_vision"][7]
  }

Desire

  • The goals of an agent
  • Example
    • prevent a machine from over heating
    • Tank Game
      • Not die
      • Kill the other tank

Intention

  • A plan to achieve one or more goals
  • Depends on metric
    • Ideally maximize goals achieved
    • Maximize preferred goals
  • Example
    • Reduce the temperature of a machine
    • Tank Game
      • Move around and shoot at the enemy tank

AI - 0 State Machine

image info


@app.post('/brain')
async def brain(request: Request):
  ...
    if turning is None:
      forward_blocked = is_blocked(sensors['n'], min_dist=45.)[0] or\
          is_blocked(sensors['ne'], min_dist=40.)[0] or\
          is_blocked(sensors['nw'], min_dist=40.)[0]
      

      if forward_blocked:
          print("time to turn")
          left_dist = max(
              is_blocked(sensors['w'])[1],
              is_blocked(sensors['nw'])[1],
          )
          right_dist = max(
              is_blocked(sensors['e'])[1],
              is_blocked(sensors['ne'])[1],
          )
          if left_dist < right_dist:
              print("rotate right")
              games[game_id]["turning"] = 'right'
          else:
              print("rotate left")
              games[game_id]["turning"] = 'left'
          games[game_id]["old_pos"] = current_pos
          return {"action": "move_backward"}
      else:
          print("Forward")

          if dist(old_pos, current_pos) <= 0.000001:
              counting=1+(counting % 30)
              counting = games[game_id]["counting"]

              if counting == 29:
                  print("Move back")
                  games[game_id]["old_pos"] = current_pos
                  
                  left_dist = min(
                      is_blocked(sensors['w'])[1],
                      is_blocked(sensors['nw'])[1],
                  )
                  right_dist = min(
                      is_blocked(sensors['e'])[1],
                      is_blocked(sensors['ne'])[1],
                  )
                  if left_dist < right_dist:
                      print("rotate right")
                      games[game_id]["turning"] = 'right'
                  else:
                      print("rotate left")
                      games[game_id]["turning"] = 'left'
                  
                  return {"action": "move_backward"}

          counting=0
          counting = games[game_id]["counting"]

          games[game_id]["old_pos"] = current_pos
          return {"action": "move_forward"}
  else:
      print("Turning")
      forward_blocked = is_blocked(sensors['n'], min_dist=70.)[0] or\
          is_blocked(sensors['ne'], min_dist=40.)[0] or\
          is_blocked(sensors['nw'], min_dist=40.)[0]
      
      print(is_blocked(sensors['n'], min_dist=70.))
      print(is_blocked(sensors['ne'], min_dist=40.))
      print(is_blocked(sensors['nw'], min_dist=40.))
      good_rot = [float(x) * pi/4 for x in range(8)]

      facing_good_dir = any(
          map(
              lambda x: angle_distance(x, current_heading) < 0.1,
              good_rot
          )
      )

      if angle_distance(current_heading, old_rot) < 0.00001:
          print("We might be stuck")
          counting=1+(counting % 30)
          counting = games[game_id]["counting"]

          if counting == 29:
              if games[game_id]['turning'] == "left":
                  games[game_id]['turning'] = "right"
              else:
                  games[game_id]['turning'] = "left"
          
          if counting == 10:
              print("Move back")
              games[game_id]["old_pos"] = current_pos
              return {"action": "move_backward"}
          
      
      if facing_good_dir and not forward_blocked:
          print("No longer blocked")
          games[game_id]["turning"] = None
          games[game_id]["old_pos"] = current_pos
          return {"action": "move_forward"}
      
      print(f"rotate_{games[game_id]['turning']}")
      games[game_id]["old_rot"] = current_heading
      return {"action": f"rotate_{games[game_id]['turning']}"}

Loops

  • Rinse and repeat

OODA loops

image info


OODA loops

  • Not an AI framework rather, a decision framework
  • Created by the US AirForce after Vietnam
    • Colonel John Boyd image info

  • Pre-Vietnam
    • High on the Victory of WWII and status quo of Korea
    • Enough fire-power and industry and wipe any force
      • Especially against industrially and military inferior foe
  • Decision making framework

image info


image info


Observe

  • Look at the current environment.

image info


Orient

  • Analysis the data
  • Look at past outcomes

image info


Decide

  • Create a Plans

image info


Act

  • Implement a plan
  • Acting on the environment changes the environment

image info


Applications

  • Adversarial thinking
  • Ukraine
    • Early counter-attack success

  • WWII image info

  • Allies tried to jump the NAZI OODA loop
  • Encirclement in Belgium
  • Slow to Fast
  • Pace
    • Speed vs. Intelligence

MAPPING

SLAM

  • Simultaneous Localization and Mapping
  • Uses relative inputs
    • Laser distance
    • Visual identification
    • Velocity
    • Orientation
    • GPS coordinates
      • Most use cases don't have access to GPS coordinates

  • Used in robotics, drones
  • Analogy of LSLAM
  • Add to belief set

  • Use cases
    • Path finding
    • Run & Hide
    • Scouting
    • Ambushes

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published