Skip to content

Commit

Permalink
Apply format
Browse files Browse the repository at this point in the history
  • Loading branch information
fire committed Dec 19, 2023
1 parent 54b12f4 commit 4a84a4d
Show file tree
Hide file tree
Showing 16 changed files with 226 additions and 460 deletions.
15 changes: 7 additions & 8 deletions addons/task_goal/core/domain.gd
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@ extends Resource

const verbose: int = 0

func _m_verify_g(
state: Dictionary,
method: String,
state_var: String,
arg: String,
desired_val: Variant,
depth: int
) -> Array:

func _m_verify_g(state: Dictionary, method: String, state_var: String, arg: String, desired_val: Variant, depth: int) -> Array:
if state[state_var][arg] != desired_val:
assert(false, "Depth %s: method %s didn't achieve\nGoal %s[%s] = %s" % [depth, method, state_var, arg, desired_val])
if verbose >= 3:
print("Depth %s: method %s achieved\nGoal %s[%s] = %s" % [depth, method, state_var, arg, desired_val])
return []


static func _goals_not_achieved(state: Dictionary, multigoal: Multigoal) -> Dictionary:
var unachieved: Dictionary = {}
for n in multigoal.state:
Expand All @@ -43,6 +38,7 @@ static func _goals_not_achieved(state: Dictionary, multigoal: Multigoal) -> Dict
unachieved[n][arg] = val
return unachieved


func _m_verify_mg(state: Dictionary, method: String, multigoal: Multigoal, depth: int) -> Variant:
var goal_dict = _goals_not_achieved(state, multigoal)
if goal_dict:
Expand All @@ -51,6 +47,7 @@ func _m_verify_mg(state: Dictionary, method: String, multigoal: Multigoal, depth
print("Depth %s: method %s achieved %s" % [depth, method, multigoal])
return []


var _action_dict: Dictionary = {}

var _task_method_dict: Dictionary = {
Expand All @@ -62,8 +59,10 @@ var _unigoal_method_dict: Dictionary = {}

var _multigoal_method_list: Array = []


func _init(domain_name: String) -> void:
set_name(domain_name)


func display() -> void:
print(self)
3 changes: 3 additions & 0 deletions addons/task_goal/core/multigoal.gd
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ var _state: Dictionary = {}
set(value):
_state = value


## multigoal_name is the name to use for the multigoal. The keyword
## args are the names and desired values of state variables.
func _init(multigoal_name, state_variables: Dictionary):
resource_name = multigoal_name
_state = state_variables


## Print the multigoal's state-variables and their values.
## - heading (optional) is a heading to print beforehand.
func display(heading: String = "") -> void:
if heading != "":
print(heading)
print(_state)


## Return a list of all state-variable names in the multigoal.
func state_vars() -> Array:
return _state.keys()
27 changes: 11 additions & 16 deletions addons/task_goal/core/plan.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) 2023-present. This file is part of V-Sekai https://v-sekai.org/.
# K. S. Ernest (Fire) Lee & Contributors (see .all-contributorsrc).
# plan.gd
# plan.gd
# SPDX-License-Identifier: MIT

# SPDX-FileCopyrightText: 2021 University of Maryland
Expand Down Expand Up @@ -296,9 +296,7 @@ func _refine_task_and_continue(state, task1, todo_list, plan, depth, stn) -> Var
for method in relevant:
if verbose >= 3:
print("Depth %s trying %s: " % [depth, method.get_method()])
var subtasks: Variant = method.get_object().callv(
method.get_method(), [state] + task1.slice(1)
)
var subtasks: Variant = method.get_object().callv(method.get_method(), [state] + task1.slice(1))
# Can't just say "if subtasks:", because that's wrong if subtasks == []
if subtasks is Array and stn.is_consistent():
if verbose >= 3:
Expand Down Expand Up @@ -346,9 +344,7 @@ func _refine_unigoal_and_continue(state, goal1, todo_list, plan, depth, stn) ->
var verification = []

if verify_goals:
verification = [
["_verify_g", str(method.get_method()), state_var_name, arg, val, depth]
]
verification = [["_verify_g", str(method.get_method()), state_var_name, arg, val, depth]]
else:
verification = []

Expand All @@ -364,9 +360,7 @@ func _refine_unigoal_and_continue(state, goal1, todo_list, plan, depth, stn) ->
return false


func _refine_multigoal_and_continue(
state: Dictionary, goal1: Multigoal, todo_list: Array, plan: Array, depth: int, stn
) -> Variant:
func _refine_multigoal_and_continue(state: Dictionary, goal1: Multigoal, todo_list: Array, plan: Array, depth: int, stn) -> Variant:
if verbose >= 3:
print("Depth %s multigoal %s: " % [depth, goal1])
var relevant: Array = current_domain._multigoal_method_list
Expand Down Expand Up @@ -400,6 +394,7 @@ func _refine_multigoal_and_continue(
print("Depth %s could not achieve multigoal %s" % [depth, goal1])
return false


func find_plan(state: Dictionary, todo_list: Array, stn: SimpleTemporalNetwork = SimpleTemporalNetwork.new()) -> Variant:
if verbose >= 1:
var todo_array: Array = []
Expand All @@ -408,12 +403,12 @@ func find_plan(state: Dictionary, todo_list: Array, stn: SimpleTemporalNetwork =
var todo_string = "[" + ", ".join(todo_array) + "]"
print("FindPlan> find_plan, verbose=%s:" % verbose)
print(" state = %s\n todo_list = %s" % [state, todo_string])

var result: Variant = seek_plan(state, todo_list, [], 0, stn)

if verbose >= 1:
print("FindPlan> result = ", result, "\n")

return result


Expand All @@ -424,12 +419,12 @@ func seek_plan(state: Dictionary, todo_list: Array, plan: Array, depth: int, stn
todo_array.push_back(_item_to_string(x))
var todo_string = "[" + ", ".join(todo_array) + "]"
print("Depth %s todo_list %s" % [depth, todo_string])

if todo_list.is_empty():
if verbose >= 3:
print("depth %s no more tasks or goals, return plan" % [depth])
return plan

var item1 = todo_list.front()
todo_list.pop_front()

Expand All @@ -444,7 +439,7 @@ func seek_plan(state: Dictionary, todo_list: Array, plan: Array, depth: int, stn
return _refine_unigoal_and_continue(state, item1, todo_list, plan, depth, stn)

assert(false, "Depth %s: %s isn't an action, task, unigoal, or multigoal\n" % [depth, item1])

return false


Expand Down
14 changes: 8 additions & 6 deletions addons/task_goal/core/simple_temporal_network.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ var constraints: Array[TemporalConstraint] = []
var stn_matrix: Array[Array] = []
var num_nodes: int = 0
var node_intervals: Array[Vector2i] = []
var node_indices: Dictionary = {}
var node_indices: Dictionary = {}

func to_dictionary() -> Dictionary:
return { "resource_name": resource_name, "constraints": constraints, "matrix": stn_matrix, "number_of_nodes": num_nodes, "node_intervals": node_intervals }

func to_dictionary() -> Dictionary:
return {"resource_name": resource_name, "constraints": constraints, "matrix": stn_matrix, "number_of_nodes": num_nodes, "node_intervals": node_intervals}


func get_node_index(node_interval: Vector2i) -> int:
Expand All @@ -33,6 +34,7 @@ func _init_matrix() -> void:
else:
stn_matrix[i][j] = INF


func add_temporal_constraint(from_constraint: TemporalConstraint, to_constraint: TemporalConstraint = null, min_gap: float = 0, max_gap: float = 0) -> bool:
if not from_constraint is TemporalConstraint:
return false
Expand All @@ -45,7 +47,7 @@ func add_temporal_constraint(from_constraint: TemporalConstraint, to_constraint:

if not max_gap is float or max_gap < 0:
return false

constraints.append(from_constraint)
constraints.append(to_constraint)

Expand Down Expand Up @@ -122,15 +124,15 @@ func propagate_constraints() -> bool:
return false

return true


func is_consistent() -> bool:
var skip = false
for i in range(constraints.size()):
if skip:
skip = false
continue
for j in range(i+1, constraints.size()):
for j in range(i + 1, constraints.size()):
var c1 := constraints[i]
if c1 == null:
return false
Expand Down
4 changes: 3 additions & 1 deletion addons/task_goal/core/temporal_constraint.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ var time_interval: Vector2i
var duration: int
var temporal_qualifier: TemporalQualifier


func _init(start: int, end: int, duration: int, qualifier: TemporalQualifier, resource: String):
time_interval = Vector2i(start, end)
self.duration = duration
temporal_qualifier = qualifier
resource_name = resource


func to_dictionary() -> Dictionary:
return { "resource_name": resource_name, "time_interval" : time_interval, "duration": duration, "temporal_qualifier": temporal_qualifier }
return {"resource_name": resource_name, "time_interval": time_interval, "duration": duration, "temporal_qualifier": temporal_qualifier}
14 changes: 6 additions & 8 deletions goal_task_tests/test_dream.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

func reserve_practice_room(state, time_interval: TemporalConstraint, stn: SimpleTemporalNetwork) -> Variant:
# Update the state to show that the practice room is reserved
state['shared_resource']['practice_room'] = 'reserved'
state['task_status']['reserve_room'] = 'done'
state["shared_resource"]["practice_room"] = "reserved"
state["task_status"]["reserve_room"] = "done"
# Add the time interval to the STN and propagate the constraints
stn.update_state({"practice": time_interval})
if not stn.is_consistent():
Expand All @@ -16,7 +16,7 @@ func reserve_practice_room(state, time_interval: TemporalConstraint, stn: Simple

func attend_audition(state, time_interval: TemporalConstraint, stn: SimpleTemporalNetwork) -> Variant:
# Update the state to show that the audition has been attended
state['task_status']['audition'] = 'attended'
state["task_status"]["audition"] = "attended"
# Add the time interval to the STN and propagate the constraints
stn.update_state({"audition": time_interval})
if not stn.propagate_constraints():
Expand All @@ -26,7 +26,7 @@ func attend_audition(state, time_interval: TemporalConstraint, stn: SimpleTempor

func practice_craft(state, time_interval: TemporalConstraint, stn: SimpleTemporalNetwork) -> Variant:
# Update the state to show that the practice has been done
state['task_status']['practice'] = 'done'
state["task_status"]["practice"] = "done"
# Add the time interval to the STN and propagate the constraints
stn.update_state({"craft": time_interval})
if not stn.propagate_constraints():
Expand All @@ -36,7 +36,7 @@ func practice_craft(state, time_interval: TemporalConstraint, stn: SimpleTempora

func network(state, time_interval: TemporalConstraint, stn: SimpleTemporalNetwork) -> Variant:
# Update the state to show that networking has been done
state['task_status']['networking'] = 'done'
state["task_status"]["networking"] = "done"
# Add the time interval to the STN and propagate the constraints
stn.update_state({"network": time_interval})
if not stn.propagate_constraints():
Expand All @@ -56,13 +56,11 @@ func get_possible_dream_time_intervals(stn: SimpleTemporalNetwork) -> Array[Temp
max_start = start

# Generate all possible time intervals within the minimum and maximum start times
var time_intervals : Array[TemporalConstraint] = []
var time_intervals: Array[TemporalConstraint] = []
for start_time in range(min_start, max_start + 1):
var end_time = start_time + 24
var time_interval = TemporalConstraint.new(start_time, end_time, end_time - start_time, TemporalConstraint.TemporalQualifier.OVERALL, "")
if stn.is_valid_constraint(time_interval):
time_intervals.append(time_interval)

return time_intervals


37 changes: 8 additions & 29 deletions goal_task_tests/test_game_project.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,21 @@

extends GutTest


func test_propagate_constraints() -> void:
var simple_temporal_network: SimpleTemporalNetwork = SimpleTemporalNetwork.new()

# Add 10 specific 3D game creation tasks
var tasks = [
"Model Character",
"Create Environment",
"Design Level",
"Implement Gameplay Mechanics",
"Animate Characters",
"Add Sound Effects",
"Optimize Performance",
"Test and Debug",
"Polish Game",
"Publish Game"
]
var tasks = ["Model Character", "Create Environment", "Design Level", "Implement Gameplay Mechanics", "Animate Characters", "Add Sound Effects", "Optimize Performance", "Test and Debug", "Polish Game", "Publish Game"]

for i in range(tasks.size()):
var task_name = tasks[i]
var task_duration = 10
var task_start_time = i * task_duration # Set the start time for each task
var task_start_time = i * task_duration # Set the start time for each task

# Create a new temporal constraint using the task name, start time, duration, and AT_END qualifier
var task_constraint = TemporalConstraint.new(task_start_time, task_start_time + task_duration, task_duration, TemporalConstraint.TemporalQualifier.AT_END, task_name)

# Add the constraint to the simple temporal network
simple_temporal_network.add_temporal_constraint(task_constraint)

Expand All @@ -41,20 +31,9 @@ func test_propagate_constraints() -> void:
continue
var dictionary = c.to_dictionary()
constraints_array.append(dictionary)

simple_temporal_network.propagate_constraints()

print_rich(constraints_array)

assert_eq_deep(constraints_array, [
{ "resource_name": "Model Character", "time_interval": Vector2i(0, 10), "duration": 10, "temporal_qualifier": 1 },
{ "resource_name": "Create Environment", "time_interval": Vector2i(10, 20), "duration": 10, "temporal_qualifier": 1 },
{ "resource_name": "Design Level", "time_interval": Vector2i(20, 30), "duration": 10, "temporal_qualifier": 1 },
{ "resource_name": "Implement Gameplay Mechanics", "time_interval": Vector2i(30, 40), "duration": 10, "temporal_qualifier": 1 },
{ "resource_name": "Animate Characters", "time_interval": Vector2i(40, 50), "duration": 10, "temporal_qualifier": 1 },
{ "resource_name": "Add Sound Effects", "time_interval": Vector2i(50, 60), "duration": 10, "temporal_qualifier": 1 },
{ "resource_name": "Optimize Performance", "time_interval": Vector2i(60, 70), "duration": 10, "temporal_qualifier": 1 },
{ "resource_name": "Test and Debug", "time_interval": Vector2i(70, 80), "duration": 10, "temporal_qualifier": 1 },
{ "resource_name": "Polish Game", "time_interval": Vector2i(80, 90), "duration": 10, "temporal_qualifier": 1 },
{ "resource_name": "Publish Game", "time_interval": Vector2i(90, 100), "duration": 10, "temporal_qualifier": 1 }
])
assert_eq_deep(constraints_array, [{"resource_name": "Model Character", "time_interval": Vector2i(0, 10), "duration": 10, "temporal_qualifier": 1}, {"resource_name": "Create Environment", "time_interval": Vector2i(10, 20), "duration": 10, "temporal_qualifier": 1}, {"resource_name": "Design Level", "time_interval": Vector2i(20, 30), "duration": 10, "temporal_qualifier": 1}, {"resource_name": "Implement Gameplay Mechanics", "time_interval": Vector2i(30, 40), "duration": 10, "temporal_qualifier": 1}, {"resource_name": "Animate Characters", "time_interval": Vector2i(40, 50), "duration": 10, "temporal_qualifier": 1}, {"resource_name": "Add Sound Effects", "time_interval": Vector2i(50, 60), "duration": 10, "temporal_qualifier": 1}, {"resource_name": "Optimize Performance", "time_interval": Vector2i(60, 70), "duration": 10, "temporal_qualifier": 1}, {"resource_name": "Test and Debug", "time_interval": Vector2i(70, 80), "duration": 10, "temporal_qualifier": 1}, {"resource_name": "Polish Game", "time_interval": Vector2i(80, 90), "duration": 10, "temporal_qualifier": 1}, {"resource_name": "Publish Game", "time_interval": Vector2i(90, 100), "duration": 10, "temporal_qualifier": 1}])
10 changes: 2 additions & 8 deletions goal_task_tests/test_integration.gd
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,5 @@ func before_each():


func test_together_goal():
var plan = planner.find_plan(
state1.duplicate(true), [["entity_met_entity", "seb", "mia", "coffee_shop"]]
)
assert_eq(
plan,
[["travel_location", "seb", "coffee_shop"], ["travel_location", "mia", "coffee_shop"]],
""
)
var plan = planner.find_plan(state1.duplicate(true), [["entity_met_entity", "seb", "mia", "coffee_shop"]])
assert_eq(plan, [["travel_location", "seb", "coffee_shop"], ["travel_location", "mia", "coffee_shop"]], "")
Loading

0 comments on commit 4a84a4d

Please sign in to comment.