Skip to content

Commit

Permalink
support config of util function via cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Schneider committed Oct 5, 2021
1 parent 4217685 commit 3a8f36f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
17 changes: 13 additions & 4 deletions deepcomp/env/entities/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
from matplotlib import cm

from deepcomp.env.util.utility import log_utility, step_utility
from deepcomp.util.constants import MIN_UTILITY, MAX_UTILITY
from deepcomp.util.constants import MIN_UTILITY, MAX_UTILITY, SUPPORTED_UTILITIES


class User:
"""
A user/UE moving around in the world and requesting mobile services
Connection to BS are checked before connecting and after every move to check if connection is lost or still stable
"""
def __init__(self, id, map, pos_x, pos_y, movement, dr_req=1):
def __init__(self, id, map, pos_x, pos_y, movement, util_func='log', dr_req=1):
"""
Create new UE object
:param id: Unique ID of UE (string)
Expand All @@ -27,6 +27,9 @@ def __init__(self, id, map, pos_x, pos_y, movement, dr_req=1):
self.id = id
self.map = map
self.movement = movement
assert util_func in SUPPORTED_UTILITIES, \
f"Utility function {util_func} not supported. Supported: {SUPPORTED_UTILITIES}"
self.util_func = util_func
self.dr_req = dr_req
# dict of connected BS: BS (only connected BS are keys!) --> data rate of connection
self.bs_dr = {}
Expand Down Expand Up @@ -77,8 +80,14 @@ def utility(self):

def dr_to_utility(self, dr):
"""Utility function to map given data rate to utility for the UE"""
# return step_utility(dr, self.dr_req)
return log_utility(dr)
assert self.util_func in SUPPORTED_UTILITIES, \
f"Utility function {self.util_func} not supported. Supported: {SUPPORTED_UTILITIES}"
if self.util_func == 'log':
return log_utility(dr)
if self.util_func == 'step':
return step_utility(dr, self.dr_req)
# unknown utility not implemented
raise NotImplementedError(f"Utility function {self.util_func} not implemented!")

def seed(self, seed=None):
self.rng.seed(seed)
Expand Down
3 changes: 2 additions & 1 deletion deepcomp/util/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import structlog

from deepcomp.util.constants import SUPPORTED_ALGS, SUPPORTED_ENVS, SUPPORTED_AGENTS, SUPPORTED_RENDER, \
SUPPORTED_SHARING, SUPPORTED_REWARDS, CENTRAL_ALGS, MULTI_ALGS, SUPPORTED_UE_ARRIVAL
SUPPORTED_SHARING, SUPPORTED_REWARDS, CENTRAL_ALGS, MULTI_ALGS, SUPPORTED_UE_ARRIVAL, SUPPORTED_UTILITIES


log = structlog.get_logger()
Expand Down Expand Up @@ -53,6 +53,7 @@ def setup_cli():
parser.add_argument('--ue-arrival', type=str, choices=SUPPORTED_UE_ARRIVAL, help="UE arrival sequence")
parser.add_argument('--sharing', type=str, choices=SUPPORTED_SHARING.union({'mixed'}), default='mixed',
help="Sharing model used by BS to split resources and/or rate among connected UEs.")
parser.add_argument('--util', type=str, choices=SUPPORTED_UTILITIES, default='log', help="UEs' utility function")
# evaluation
parser.add_argument('--rand-train', action='store_true', help="Randomize training episodes.")
parser.add_argument('--cont-train', action='store_true', help="Continuous training without resetting.")
Expand Down
1 change: 1 addition & 0 deletions deepcomp/util/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
SUPPORTED_RENDER = {'html', 'gif', 'both', None}
SUPPORTED_SHARING = {'max-cap', 'resource-fair', 'rate-fair', 'proportional-fair'}
SUPPORTED_REWARDS = {'min', 'sum'}
SUPPORTED_UTILITIES = {'log', 'step'}

# small epsilon used in denominator to avoid division by zero
EPSILON = 1e-16
Expand Down
28 changes: 14 additions & 14 deletions deepcomp/util/env_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from ray.rllib.agents.ppo import DEFAULT_CONFIG
from ray.rllib.env.multi_agent_env import MultiAgentEnv

from deepcomp.util.constants import SUPPORTED_ENVS, SUPPORTED_AGENTS, SUPPORTED_SHARING, SUPPORTED_UE_ARRIVAL
from deepcomp.util.constants import SUPPORTED_ENVS, SUPPORTED_AGENTS, SUPPORTED_SHARING, SUPPORTED_UE_ARRIVAL, \
SUPPORTED_UTILITIES
from deepcomp.env.single_ue.variants import RelNormEnv
from deepcomp.env.multi_ue.central import CentralRelNormEnv
from deepcomp.env.multi_ue.multi_agent import MultiAgentMobileEnv
Expand Down Expand Up @@ -141,18 +142,21 @@ def create_dyn_large_map(sharing_model, num_bs, dist_to_border=10):
return map, bs_list


def create_ues(map, num_static_ues, num_slow_ues, num_fast_ues):
def create_ues(map, num_static_ues, num_slow_ues, num_fast_ues, util_func):
"""Create custom number of slow/fast UEs on the given map. Return UE list"""
ue_list = []
id = 1
for i in range(num_static_ues):
ue_list.append(User(str(id), map, pos_x='random', pos_y='random', movement=RandomWaypoint(map, velocity=0)))
ue_list.append(User(str(id), map, pos_x='random', pos_y='random', movement=RandomWaypoint(map, velocity=0),
util_func=util_func))
id += 1
for i in range(num_slow_ues):
ue_list.append(User(str(id), map, pos_x='random', pos_y='random', movement=RandomWaypoint(map, velocity='slow')))
ue_list.append(User(str(id), map, pos_x='random', pos_y='random', movement=RandomWaypoint(map, velocity='slow'),
util_func=util_func))
id += 1
for i in range(num_fast_ues):
ue_list.append(User(str(id), map, pos_x='random', pos_y='random', movement=RandomWaypoint(map, velocity='fast')))
ue_list.append(User(str(id), map, pos_x='random', pos_y='random', movement=RandomWaypoint(map, velocity='fast'),
util_func=util_func))
id += 1
return ue_list

Expand All @@ -169,18 +173,14 @@ def create_custom_env(sharing_model):
Basestation('C', Point(184, 60), get_sharing_for_bs(sharing_model, 2)),
Basestation('D', Point(97, 110), get_sharing_for_bs(sharing_model, 3)),
]

# UEs are now created dynamically according to CLI
# ue_list = [
# User(str(1), map, pos_x=70, pos_y=40, movement=UniformMovement(map)),
# User(str(2), map, pos_x=80, pos_y=60, movement=UniformMovement(map))
# ]
return map, bs_list


def get_env(map_size, bs_dist, num_static_ues, num_slow_ues, num_fast_ues, sharing_model, num_bs=None):
def get_env(map_size, bs_dist, num_static_ues, num_slow_ues, num_fast_ues, sharing_model, util_func, num_bs=None):
"""Create and return the environment corresponding to the given map_size"""
assert map_size in SUPPORTED_ENVS, f"Environment {map_size} is not one of {SUPPORTED_ENVS}."
assert util_func in SUPPORTED_UTILITIES, \
f"Utility function {util_func} not supported. Supported: {SUPPORTED_UTILITIES}"

# create map and BS list
map, bs_list = None, None
Expand All @@ -197,7 +197,7 @@ def get_env(map_size, bs_dist, num_static_ues, num_slow_ues, num_fast_ues, shari
map, bs_list = create_custom_env(sharing_model)

# create UEs
ue_list = create_ues(map, num_static_ues, num_slow_ues, num_fast_ues)
ue_list = create_ues(map, num_static_ues, num_slow_ues, num_fast_ues, util_func)

return map, ue_list, bs_list

Expand Down Expand Up @@ -235,7 +235,7 @@ def create_env_config(cli_args):
"""
env_class = get_env_class(cli_args.agent)
map, ue_list, bs_list = get_env(cli_args.env, cli_args.bs_dist, cli_args.static_ues, cli_args.slow_ues,
cli_args.fast_ues, cli_args.sharing, cli_args.num_bs)
cli_args.fast_ues, cli_args.sharing, cli_args.util, num_bs=cli_args.num_bs)

# this is for DrEnv and step utility
# env_config = {
Expand Down
6 changes: 3 additions & 3 deletions deepcomp/util/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def load_agent(self, rllib_dir=None, rand_seed=None, fixed_action=1, explore=Fal
self.agent_train_steps = self.get_training_steps()

def set_result_filename(self):
"""Return a suitable filename (without file ending) in the format 'agent_env-class_env-size_num-ues_time'"""
"""Return a suitable filename (without file ending) depending on current configuration"""
assert self.agent is not None, "Set the filename after loading the agent"
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
agent_name = type(self.agent).__name__
Expand All @@ -293,8 +293,8 @@ def set_result_filename(self):
test = 'rand' if self.cli_args.rand_test else 'fixed'
seed = self.cli_args.seed
self.result_filename = \
f'{agent_name}_{self.env_name}_{env_size}_{self.cli_args.sharing}_{num_ues}UEs-{self.cli_args.reward}' \
f'_{train}-{test}_{seed}_{timestamp}'
f'{agent_name}_{self.env_name}_{env_size}_{self.cli_args.sharing}_{num_ues}UEs-{self.cli_args.util}-' \
f'{self.cli_args.reward}_{train}-{test}_{seed}_{timestamp}'

def save_animation(self, fig, patches, mode):
"""
Expand Down

0 comments on commit 3a8f36f

Please sign in to comment.