Skip to main content

No project description provided

Project description

Welcome to utilitai!

This package is a minimal(ish) implementation of Utility AI in Python. It is designed to compliment btreeny, my Behaviour Trees implementation!

There actually aren't many options for utility AI in Python, which surprises me! In general for serious systems you should be looking at C++ (or, maybe, Rust 🦀), but hopefully this library will help on the prototyping journey!

import math
import utilitai

@dataclass
class MyContext:
    remaining_battery_mAh: float
    meters_per_mAh: float
    remaining_battery_percentage: int # int from 0 to 100
    robot_position: tuple[float, float]
    goal_position: tuple[float, float]

# Considerations take some computed value and map it to a normalized score usable
# when computing the utility of an action
@consideration(response_curve=lambda v: 1 - v)
def remaining_robot_battery(ctx: MyContext):
    """Return the robot's remaining battery as a float from 0 to 1
    """
    return ctx.remaining_battery_percentage / 100

@consideration(response_curve=partial(utilitai.negexp, scale=0.1))
def distance_to_goal(ctx: MyContext):
    return math.sqrt(
      (ctx.robot_position[0] - ctx.goal_position[0])**2
      + (ctx.robot_position[1] - ctx.goal_position[1])**2
    )

@consideration
def is_at_goal(ctx: MyContext):
    return 1.0 if distance_to_goal.raw(cts) < 1E-2 else 0.0

@consideration
def can_reach_goal_with_battery(ctx: MyContext):
    # Underlying functions from considerations can be used
    # in other considerations, and still be registered in the trace
    distance = distance_to_goal.raw(ctx)
    movable_distance = ctx.remaining_battery_mAh * ctx.meters_per_mAh
    return 1.0 if movable_distance > distance else 0.0

@consideration
def has_valid_path(ctx: MyContext):
    return 1.0

def main():
    actions = {
        # If we have no good choices, do nothing
        "do_nothing": utilitai.eps,
        # As our battery gets lower, our desire to stop and recharge
        # gets higher
        "recharge": remaining_robot_battery,
        # Make progress towards our goal location
        "move_to_goal": has_valid_path,
        "replan_path": lambda ctx: utilitai.curves.inverse_linear(has_valid_path(ctx)),
        # Dancing before we make it to the goal would be silly!
        "do_a_dance": is_at_goal,
    }
    
    planner = utilitai.utility_system(actions)
    
    while True:
        # Perception
        context = update_context_from_sensors(context)
        # Planning (ish)
        best_action = planner(context)
        # Control - maybe using btreeny!
        control_robot_from_action(best_action, context)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

utilitai-0.0.1.tar.gz (44.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

utilitai-0.0.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file utilitai-0.0.1.tar.gz.

File metadata

  • Download URL: utilitai-0.0.1.tar.gz
  • Upload date:
  • Size: 44.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for utilitai-0.0.1.tar.gz
Algorithm Hash digest
SHA256 1fc5094f22cd0511f32af42e48e23d87a4ce1355b24abdd189cdabf99034b8d6
MD5 45411be0704757c07ba5ffb1ab622c30
BLAKE2b-256 b7333c9dcc393f5e5f6bd3baf42164885fa430cafd9914d0876a037e085a8565

See more details on using hashes here.

Provenance

The following attestation bundles were made for utilitai-0.0.1.tar.gz:

Publisher: publish-to-pypi.yml on tingard/utilitai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file utilitai-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: utilitai-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for utilitai-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3ebc18c9535d850fcee0f3ff52509ff85097b4b1795948d42ba3c6fe3ef71135
MD5 3a73f994aee6aa38fe206e745f524b24
BLAKE2b-256 64480e081b5b37830b6ae84b95c98d554e2f70fcf6b88f363b85a4a98e417979

See more details on using hashes here.

Provenance

The following attestation bundles were made for utilitai-0.0.1-py3-none-any.whl:

Publisher: publish-to-pypi.yml on tingard/utilitai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page