How to use the_task method in locust

Best Python code snippet using locust

test_rddl_writer.py

Source:test_rddl_writer.py Github

copy

Full Screen

1import os2import tempfile3import tarski4from tarski.theories import Theory5from tarski.syntax import *6from tarski.io import rddl7from tarski.syntax.arithmetic import *8from tarski.syntax.arithmetic.special import *9from tarski.syntax.arithmetic.random import *10from tarski.rddl import Task11def test_simple_rddl_model():12 lang = tarski.language('lqr_nav_1d', [Theory.EQUALITY, Theory.ARITHMETIC, Theory.SPECIAL])13 the_task = Task(lang, 'lqr_nav_1d', 'instance_001')14 the_task.requirements = [rddl.Requirements.CONTINUOUS, rddl.Requirements.REWARD_DET]15 the_task.parameters.discount = 1.016 the_task.parameters.horizon = 1017 the_task.parameters.max_nondef_actions = 118 # variables19 x = lang.function('x', lang.Real)20 v = lang.function('v', lang.Real)21 u = lang.function('u', lang.Real)22 t = lang.function('t', lang.Real)23 # non fluents24 dt = lang.function('dt', lang.Real)25 gx = lang.function('gx', lang.Real)26 # cpfs27 the_task.add_cpfs(t(), t() + dt())28 the_task.add_cpfs(v(), v() + dt() * u())29 the_task.add_cpfs(x(), x() + dt() * v())30 # constraints31 the_task.add_constraint((u() >= -1.0) & (u() <= 1.0), rddl.ConstraintType.ACTION)32 the_task.add_constraint((v() >= -5.0) & (v() <= 5.0), rddl.ConstraintType.STATE)33 the_task.add_constraint((x() >= -100.0) & (x() <= 100.0), rddl.ConstraintType.STATE)34 # cost function35 Q = (x() - gx()) * (x() - gx())36 # MRJ: RDDL does not support the abs() algebraic construct37 R = ite(abs(x() - gx()) > 0.0, u() * u() * 0.01, lang.constant(0.0, lang.Real))38 # R = u() * u() * 0.0139 the_task.reward = Q + R40 # definitions41 the_task.x0.set(x(), 0.0)42 the_task.x0.set(v(), 0.0)43 the_task.x0.set(u(), 0.0)44 the_task.x0.set(t(), 0.0)45 the_task.x0.set(dt(), 0.5)46 the_task.x0.set(gx(), 20.0)47 # fluent metadata48 the_task.declare_state_fluent(x(), 0.0)49 the_task.declare_state_fluent(t(), 0.0)50 the_task.declare_interm_fluent(v(), 1)51 the_task.declare_action_fluent(u(), 0.0)52 the_task.declare_non_fluent(dt(), 0.0)53 the_task.declare_non_fluent(gx(), 0.0)54 the_writer = rddl.Writer(the_task)55 rddl_filename = os.path.join(tempfile.gettempdir(), 'lqr_nav_1d_001.rddl')56 the_writer.write_model(rddl_filename)57 mr_reader = rddl.Reader(rddl_filename)58 assert mr_reader.rddl_model is not None59 assert mr_reader.rddl_model.domain.name == 'lqr_nav_1d'60 mr_reader.translate_rddl_model()61 assert mr_reader.language is not None62def test_rddl_model_with_random_vars():63 lang = tarski.language('lqg_nav_1d', [Theory.EQUALITY, Theory.ARITHMETIC, Theory.SPECIAL, Theory.RANDOM])64 the_task = Task(lang, 'lqg_nav_1d', 'instance_001')65 the_task.requirements = [rddl.Requirements.CONTINUOUS, rddl.Requirements.REWARD_DET]66 the_task.parameters.discount = 1.067 the_task.parameters.horizon = 1068 the_task.parameters.max_nondef_actions = 169 # variables70 x = lang.function('x', lang.Real)71 v = lang.function('v', lang.Real)72 u = lang.function('u', lang.Real)73 t = lang.function('t', lang.Real)74 # non fluents75 dt = lang.function('dt', lang.Real)76 gx = lang.function('gx', lang.Real)77 # Perturbation distribution parameters78 mu_w = lang.function('mu_w', lang.Real)79 sigma_w = lang.function('sigma_w', lang.Real)80 # cpfs81 the_task.add_cpfs(t(), t() + dt())82 the_task.add_cpfs(v(), v() + dt() * u() + normal(mu_w(), sigma_w()))83 the_task.add_cpfs(x(), x() + dt() * v())84 # constraints85 the_task.add_constraint((u() >= -1.0) & (u() <= 1.0), rddl.ConstraintType.ACTION)86 the_task.add_constraint((v() >= -5.0) & (v() <= 5.0), rddl.ConstraintType.STATE)87 the_task.add_constraint((x() >= -100.0) & (x() <= 100.0), rddl.ConstraintType.STATE)88 # cost function89 Q = (x() - gx()) * (x() - gx())90 # MRJ: RDDL does not support the abs() algebraic construct91 R = ite(abs(x() - gx()) > 0.0, u() * u() * 0.01, lang.constant(0.0, lang.Real))92 # R = u() * u() * 0.0193 the_task.reward = Q + R94 # definitions95 the_task.x0.set(x(), 0.0)96 the_task.x0.set(v(), 0.0)97 the_task.x0.set(u(), 0.0)98 the_task.x0.set(t(), 0.0)99 the_task.x0.set(dt(), 0.5)100 the_task.x0.set(gx(), 20.0)101 the_task.x0.set(mu_w(), 0.0)102 the_task.x0.set(sigma_w(), 0.05)103 # fluent metadata104 the_task.declare_state_fluent(x(), 0.0)105 the_task.declare_state_fluent(t(), 0.0)106 the_task.declare_interm_fluent(v(), 1)107 the_task.declare_action_fluent(u(), 0.0)108 the_task.declare_non_fluent(dt(), 0.0)109 the_task.declare_non_fluent(gx(), 0.0)110 the_task.declare_non_fluent(mu_w(), 0.0)111 the_task.declare_non_fluent(sigma_w(), 0.0)112 the_writer = rddl.Writer(the_task)113 rddl_filename = os.path.join(tempfile.gettempdir(), 'lqg_nav_1d_001.rddl')114 the_writer.write_model(rddl_filename)115 mr_reader = rddl.Reader(rddl_filename)116 assert mr_reader.rddl_model is not None117 assert mr_reader.rddl_model.domain.name == 'lqg_nav_1d'118 mr_reader.translate_rddl_model()119 assert mr_reader.language is not None120def test_parametrized_model_with_random_vars():121 lang = tarski.language('lqg_nav_2d_multi_unit', [Theory.EQUALITY, Theory.ARITHMETIC, Theory.SPECIAL, Theory.RANDOM])122 the_task = Task(lang, 'lqg_nav_2d_multi_unit', 'instance_001')123 the_task.requirements = [rddl.Requirements.CONTINUOUS, rddl.Requirements.REWARD_DET]124 the_task.parameters.discount = 1.0125 the_task.parameters.horizon = 10126 the_task.parameters.max_nondef_actions = 1127 vehicle = lang.sort('vehicle', lang.Object)128 # variables129 x = lang.function('x', vehicle, lang.Real)130 y = lang.function('y', vehicle, lang.Real)131 vx = lang.function('vx', vehicle, lang.Real)132 vy = lang.function('vy', vehicle, lang.Real)133 ux = lang.function('ux', vehicle, lang.Real)134 uy = lang.function('uy', vehicle, lang.Real)135 t = lang.function('t', lang.Real)136 # objects137 v001 = lang.constant('v001', vehicle)138 # non fluents139 dt = lang.function('dt', lang.Real)140 gx = lang.function('gx', lang.Real)141 gy = lang.function('gy', lang.Real)142 # Perturbation distribution parameters143 mu_w = lang.function('mu_w', lang.Real)144 sigma_w = lang.function('sigma_w', lang.Real)145 # logical variable146 v = lang.variable('v', vehicle)147 # cpfs148 the_task.add_cpfs(t(), t() + dt())149 the_task.add_cpfs(vx(v), vx(v) + dt() * ux(v) + normal(mu_w(), sigma_w()))150 the_task.add_cpfs(vy(v), vy(v) + dt() * uy(v) + normal(mu_w(), sigma_w()))151 the_task.add_cpfs(x(v), x(v) + dt() * vx(v))152 the_task.add_cpfs(y(v), y(v) + dt() * vy(v))153 # constraints154 the_task.add_constraint(forall(v, (ux(v) >= -1.0) & (ux(v) <= 1.0) & (uy(v) >= -1.0) & (uy(v) <= 1.0)),155 rddl.ConstraintType.ACTION)156 the_task.add_constraint(forall(v, (sqrt(vx(v) * vx(v) + vy(v) * vy(v)) <= 5.0)), rddl.ConstraintType.STATE)157 the_task.add_constraint(forall(v, (x(v) >= -100.0) & (x(v) <= 100.0)), rddl.ConstraintType.STATE)158 the_task.add_constraint(forall(v, (y(v) >= -100.0) & (y(v) <= 100.0)), rddl.ConstraintType.STATE)159 # cost function160 Q = sumterm(v, ((x(v) - gx()) * (x(v) - gx())) + ((y(v) - gy()) * (y(v) * gy())))161 # MRJ: RDDL does not support the abs() algebraic construct162 R = sumterm(v, ite(sqrt(vx(v) * vx(v) + vy(v) * vy(v)) > 0.0, (ux(v) * ux(v) * 0.01) + (uy(v) * uy(v) * 0.01),163 lang.constant(0.0, lang.Real)))164 # R = u() * u() * 0.01165 the_task.reward = Q + R166 # definitions167 the_task.x0.set(x(v001), 0.0)168 the_task.x0.set(y(v001), 0.0)169 the_task.x0.set(vx(v001), 0.0)170 the_task.x0.set(vy(v001), 0.0)171 the_task.x0.set(ux(v001), 0.0)172 the_task.x0.set(uy(v001), 0.0)173 the_task.x0.set(t(), 0.0)174 the_task.x0.set(dt(), 0.5)175 the_task.x0.set(gx(), 20.0)176 the_task.x0.set(gy(), 20.0)177 the_task.x0.set(mu_w(), 0.0)178 the_task.x0.set(sigma_w(), 0.05)179 # fluent metadata180 the_task.declare_state_fluent(x(v), 0.0)181 the_task.declare_state_fluent(y(v), 0.0)182 the_task.declare_state_fluent(t(), 0.0)183 the_task.declare_interm_fluent(vx(v), 1)184 the_task.declare_interm_fluent(vy(v), 1)185 the_task.declare_action_fluent(ux(v), 0.0)186 the_task.declare_action_fluent(uy(v), 0.0)187 the_task.declare_non_fluent(dt(), 0.0)188 the_task.declare_non_fluent(gx(), 0.0)189 the_task.declare_non_fluent(gy(), 0.0)190 the_task.declare_non_fluent(mu_w(), 0.0)191 the_task.declare_non_fluent(sigma_w(), 0.0)192 the_writer = rddl.Writer(the_task)193 rddl_filename = os.path.join(tempfile.gettempdir(), 'lqg_nav_2d_multi_unit_001.rddl')194 the_writer.write_model(rddl_filename)195 mr_reader = rddl.Reader(rddl_filename)196 assert mr_reader.rddl_model is not None197 assert mr_reader.rddl_model.domain.name == 'lqg_nav_2d_multi_unit'198 mr_reader.translate_rddl_model()199 assert mr_reader.language is not None200def test_parametrized_model_with_random_vars_and_waypoints():201 lang = tarski.language('lqg_nav_2d_multi_unit_waypoints',202 [Theory.EQUALITY, Theory.ARITHMETIC, Theory.SPECIAL, Theory.RANDOM])203 the_task = Task(lang, 'lqg_nav_2d_multi_unit_waypoints', 'instance_001')204 the_task.requirements = [rddl.Requirements.CONTINUOUS, rddl.Requirements.REWARD_DET]205 the_task.parameters.discount = 1.0206 the_task.parameters.horizon = 40207 the_task.parameters.max_nondef_actions = 2208 vehicle = lang.sort('vehicle', lang.Object)209 waypoint = lang.sort('waypoint', lang.Object)210 # variables211 x = lang.function('x', vehicle, lang.Real)212 y = lang.function('y', vehicle, lang.Real)213 wx = lang.function('wx', waypoint, lang.Real)214 wy = lang.function('wy', waypoint, lang.Real)215 wv = lang.function('visited', waypoint, lang.Real)216 vx = lang.function('vx', vehicle, lang.Real)217 vy = lang.function('vy', vehicle, lang.Real)218 ux = lang.function('ux', vehicle, lang.Real)219 uy = lang.function('uy', vehicle, lang.Real)220 t = lang.function('t', lang.Real)221 # objects222 v001 = lang.constant('v001', vehicle)223 p1 = lang.constant('p1', waypoint)224 p2 = lang.constant('p2', waypoint)225 p3 = lang.constant('p3', waypoint)226 # non fluents227 dt = lang.function('dt', lang.Real)228 wr = lang.function('waypoint_radius', lang.Real)229 # Perturbation distribution parameters230 mu_w = lang.function('mu_w', lang.Real)231 sigma_w = lang.function('sigma_w', lang.Real)232 # logical variable233 v = lang.variable('v', vehicle)234 wpt = lang.variable('wpt', waypoint)235 # cpfs236 the_task.add_cpfs(t(), t() + dt())237 dist_vec_norm = ((x(v) - wx(wpt)) * (x(v) - wx(wpt))) + ((y(v) - wy(wpt)) * (y(v) - wy(wpt)))238 the_task.add_cpfs(wv(wpt), ite(sqrt(dist_vec_norm) <= wr(), lang.constant(1.0, lang.Real),239 max(lang.constant(0.0, lang.Real), wv(wpt))))240 the_task.add_cpfs(vx(v), vx(v) + dt() * ux(v) + normal(mu_w(), sigma_w()))241 the_task.add_cpfs(vy(v), vy(v) + dt() * uy(v) + normal(mu_w(), sigma_w()))242 the_task.add_cpfs(x(v), x(v) + dt() * vx(v))243 the_task.add_cpfs(y(v), y(v) + dt() * vy(v))244 # constraints245 the_task.add_constraint(forall(v, (ux(v) >= -1.0) & (ux(v) <= 1.0) & (uy(v) >= -1.0) & (uy(v) <= 1.0)),246 rddl.ConstraintType.ACTION)247 the_task.add_constraint(forall(v, (sqrt(vx(v) * vx(v) + vy(v) * vy(v)) <= 5.0)), rddl.ConstraintType.STATE)248 the_task.add_constraint(forall(v, (x(v) >= -100.0) & (x(v) <= 100.0)), rddl.ConstraintType.STATE)249 the_task.add_constraint(forall(v, (y(v) >= -100.0) & (y(v) <= 100.0)), rddl.ConstraintType.STATE)250 # cost function251 WV = sumterm(wpt, wv(wpt))252 # MRJ: RDDL does not support the abs() algebraic construct253 R = sumterm(v, (ux(v) * ux(v) * 0.01) + (uy(v) * uy(v) * 0.01))254 # R = u() * u() * 0.01255 the_task.reward = WV - R256 # definitions257 the_task.x0.set(x(v001), 0.0)258 the_task.x0.set(y(v001), 0.0)259 the_task.x0.set(vx(v001), 0.0)260 the_task.x0.set(vy(v001), 0.0)261 the_task.x0.set(ux(v001), 0.0)262 the_task.x0.set(uy(v001), 0.0)263 the_task.x0.set(wx(p1), 1.0)264 the_task.x0.set(wy(p1), -1.0)265 the_task.x0.set(wv(p1), 0.0)266 the_task.x0.set(wx(p2), 1.0)267 the_task.x0.set(wy(p2), 1.0)268 the_task.x0.set(wv(p2), 0.0)269 the_task.x0.set(wx(p3), 2.0)270 the_task.x0.set(wy(p3), 1.0)271 the_task.x0.set(wv(p3), 0.0)272 the_task.x0.set(t(), 0.0)273 the_task.x0.set(dt(), 0.5)274 the_task.x0.set(mu_w(), 0.0)275 the_task.x0.set(sigma_w(), 0.05)276 the_task.x0.set(wr(), 2.0)277 # fluent metadata278 the_task.declare_state_fluent(x(v), 0.0)279 the_task.declare_state_fluent(y(v), 0.0)280 the_task.declare_state_fluent(t(), 0.0)281 the_task.declare_state_fluent(wv(wpt), 0.0)282 the_task.declare_interm_fluent(vx(v), 1)283 the_task.declare_interm_fluent(vy(v), 1)284 the_task.declare_action_fluent(ux(v), 0.0)285 the_task.declare_action_fluent(uy(v), 0.0)286 the_task.declare_non_fluent(dt(), 0.0)287 the_task.declare_non_fluent(mu_w(), 0.0)288 the_task.declare_non_fluent(sigma_w(), 0.0)289 the_task.declare_non_fluent(wx(wpt), 0.0)290 the_task.declare_non_fluent(wy(wpt), 0.0)291 the_task.declare_non_fluent(wr(), 0.0)292 the_writer = rddl.Writer(the_task)293 rddl_filename = os.path.join(tempfile.gettempdir(), 'lqg_nav_2d_multi_unit_waypoints.rddl')294 the_writer.write_model(rddl_filename)295 mr_reader = rddl.Reader(rddl_filename)296 assert mr_reader.rddl_model is not None297 assert mr_reader.rddl_model.domain.name == 'lqg_nav_2d_multi_unit_waypoints'298 mr_reader.translate_rddl_model()299 assert mr_reader.language is not None300def test_parametrized_model_with_random_vars_and_waypoints_boolean():301 lang = tarski.language('lqg_nav_2d_multi_unit_bool_waypoints',302 [Theory.EQUALITY, Theory.ARITHMETIC, Theory.SPECIAL, Theory.RANDOM])303 the_task = Task(lang, 'lqg_nav_2d_multi_unit_bool_waypoints', 'instance_001')304 the_task.requirements = [rddl.Requirements.CONTINUOUS, rddl.Requirements.REWARD_DET]305 the_task.parameters.discount = 1.0306 the_task.parameters.horizon = 40307 the_task.parameters.max_nondef_actions = 2308 vehicle = lang.sort('vehicle', lang.Object)309 waypoint = lang.sort('waypoint', lang.Object)310 # variables311 x = lang.function('x', vehicle, lang.Real)312 y = lang.function('y', vehicle, lang.Real)313 wx = lang.function('wx', waypoint, lang.Real)314 wy = lang.function('wy', waypoint, lang.Real)315 vx = lang.function('vx', vehicle, lang.Real)316 vy = lang.function('vy', vehicle, lang.Real)317 ux = lang.function('ux', vehicle, lang.Real)318 uy = lang.function('uy', vehicle, lang.Real)319 t = lang.function('t', lang.Real)320 wv = lang.predicate('visited', waypoint)321 # objects322 v001 = lang.constant('v001', vehicle)323 p1 = lang.constant('p1', waypoint)324 p2 = lang.constant('p2', waypoint)325 p3 = lang.constant('p3', waypoint)326 # non fluents327 dt = lang.function('dt', lang.Real)328 wr = lang.function('waypoint_radius', lang.Real)329 # Perturbation distribution parameters330 mu_w = lang.function('mu_w', lang.Real)331 sigma_w = lang.function('sigma_w', lang.Real)332 # logical variable333 v = lang.variable('v', vehicle)334 wpt = lang.variable('wpt', waypoint)335 # cpfs336 the_task.add_cpfs(t(), t() + dt())337 dist_vec_norm = ((x(v) - wx(wpt)) * (x(v) - wx(wpt))) + ((y(v) - wy(wpt)) * (y(v) - wy(wpt)))338 the_task.add_cpfs(wv(wpt), (wv(wpt) | (sqrt(dist_vec_norm) <= wr())))339 the_task.add_cpfs(vx(v), vx(v) + dt() * ux(v) + normal(mu_w(), sigma_w()))340 the_task.add_cpfs(vy(v), vy(v) + dt() * uy(v) + normal(mu_w(), sigma_w()))341 the_task.add_cpfs(x(v), x(v) + dt() * vx(v))342 the_task.add_cpfs(y(v), y(v) + dt() * vy(v))343 # constraints344 the_task.add_constraint(forall(v, (ux(v) >= -1.0) & (ux(v) <= 1.0) & (uy(v) >= -1.0) & (uy(v) <= 1.0)),345 rddl.ConstraintType.ACTION)346 the_task.add_constraint(forall(v, (sqrt(vx(v) * vx(v) + vy(v) * vy(v)) <= 5.0)), rddl.ConstraintType.STATE)347 the_task.add_constraint(forall(v, (x(v) >= -100.0) & (x(v) <= 100.0)), rddl.ConstraintType.STATE)348 the_task.add_constraint(forall(v, (y(v) >= -100.0) & (y(v) <= 100.0)), rddl.ConstraintType.STATE)349 # cost function350 WV = sumterm(wpt, ite(wv(wpt), lang.constant(1.0, lang.Real), lang.constant(0.0, lang.Real)))351 # MRJ: RDDL does not support the abs() algebraic construct352 R = sumterm(v, (ux(v) * ux(v) * 0.01) + (uy(v) * uy(v) * 0.01))353 # R = u() * u() * 0.01354 the_task.reward = WV - R355 # definitions356 the_task.x0.set(x(v001), 0.0)357 the_task.x0.set(y(v001), 0.0)358 the_task.x0.set(vx(v001), 0.0)359 the_task.x0.set(vy(v001), 0.0)360 the_task.x0.set(ux(v001), 0.0)361 the_task.x0.set(uy(v001), 0.0)362 the_task.x0.set(wx(p1), 1.0)363 the_task.x0.set(wy(p1), -1.0)364 the_task.x0.set(wx(p2), 1.0)365 the_task.x0.set(wy(p2), 1.0)366 the_task.x0.set(wx(p3), 2.0)367 the_task.x0.set(wy(p3), 1.0)368 the_task.x0.set(t(), 0.0)369 the_task.x0.set(dt(), 0.5)370 the_task.x0.set(mu_w(), 0.0)371 the_task.x0.set(sigma_w(), 0.05)372 the_task.x0.set(wr(), 2.0)373 # fluent metadata374 the_task.declare_state_fluent(x(v), 0.0)375 the_task.declare_state_fluent(y(v), 0.0)376 the_task.declare_state_fluent(t(), 0.0)377 the_task.declare_state_fluent(wv(wpt), False)378 the_task.declare_interm_fluent(vx(v), 1)379 the_task.declare_interm_fluent(vy(v), 1)380 the_task.declare_action_fluent(ux(v), 0.0)381 the_task.declare_action_fluent(uy(v), 0.0)382 the_task.declare_non_fluent(dt(), 0.0)383 the_task.declare_non_fluent(mu_w(), 0.0)384 the_task.declare_non_fluent(sigma_w(), 0.0)385 the_task.declare_non_fluent(wx(wpt), 0.0)386 the_task.declare_non_fluent(wy(wpt), 0.0)387 the_task.declare_non_fluent(wr(), 0.0)388 the_writer = rddl.Writer(the_task)389 rddl_filename = os.path.join(tempfile.gettempdir(), 'lqg_nav_2d_multi_unit_bool_waypoints.rddl')390 the_writer.write_model(rddl_filename)391 mr_reader = rddl.Reader(rddl_filename)392 assert mr_reader.rddl_model is not None393 assert mr_reader.rddl_model.domain.name == 'lqg_nav_2d_multi_unit_bool_waypoints'394 mr_reader.translate_rddl_model()...

Full Screen

Full Screen

lqr2d.py

Source:lqr2d.py Github

copy

Full Screen

1"""2 2D LQR Navigation problems3"""4import argparse5import os6import numpy as np7from tqdm import tqdm8import tarski9from tarski.theories import Theory10from tarski.syntax import *11from tarski.io import rddl12from tarski.model import Model13from tarski.evaluators.simple import evaluate14from tarski.syntax.arithmetic import *15from tarski.syntax.arithmetic.special import *16from tarski.syntax.arithmetic.random import *17from tarski.rddl import Task18def parse_arguments():19 parser = argparse.ArgumentParser(description='2D LQR problem generators')20 parser.add_argument('--num-instances', default=10, type=int, help='Number of random instances to generate')21 parser.add_argument('--seed', required=True, type=int, help='Seed for the random number generator')22 parser.add_argument('--output-prefix', required=False, default='.', help='Prefix path for output files')23 parser.add_argument('--horizon', required=False, default=20, type=int, help='Horizon of the MDP')24 args = parser.parse_args()25 if not os.path.exists(args.output_prefix):26 print("Creating output directory '{}'...".format(args.output_prefix))27 os.makedirs(args.output_prefix)28 return args29def make_task(k, args):30 lang = tarski.language('lqr_nav_2d', [Theory.EQUALITY, Theory.ARITHMETIC, Theory.SPECIAL])31 the_task = Task( lang, 'lqr_nav_2d', 'instance_{0:06d}'.format(k))32 the_task.requirements = [rddl.Requirements.CONTINUOUS, rddl.Requirements.REWARD_DET]33 the_task.parameters.discount = 1.034 the_task.parameters.horizon = args.horizon35 the_task.parameters.max_nondef_actions = 'pos-inf'36 # variables37 x = lang.function('x', lang.Real)38 y = lang.function('y', lang.Real)39 vx = lang.function('vx', lang.Real)40 vy = lang.function('vy', lang.Real)41 ux = lang.function('ux', lang.Real)42 uy = lang.function('uy', lang.Real)43 t = lang.function('t', lang.Real)44 # non fluents45 dt = lang.function('dt', lang.Real)46 gx = lang.function('gx', lang.Real)47 gy = lang.function('gy', lang.Real)48 H = lang.function('H', lang.Real)49 # cpfs50 the_task.add_cpfs(t(), t() + 1.0)51 the_task.add_cpfs(vx(), vx() + dt() * ux())52 the_task.add_cpfs(vy(), vy() + dt() * uy())53 the_task.add_cpfs(x(), x() + dt() * vx())54 the_task.add_cpfs(y(), y() + dt() * vy())55 # constraints56 the_task.add_constraint( ux() >= -1.0, rddl.ConstraintType.ACTION)57 the_task.add_constraint( ux() <= 1.0, rddl.ConstraintType.ACTION)58 the_task.add_constraint( uy() >= -1.0, rddl.ConstraintType.ACTION)59 the_task.add_constraint( uy() <= 1.0, rddl.ConstraintType.ACTION) 60 the_task.add_constraint( vx() >= -5.0, rddl.ConstraintType.STATE)61 the_task.add_constraint( vx() <= 5.0, rddl.ConstraintType.STATE)62 the_task.add_constraint( vy() >= -5.0, rddl.ConstraintType.STATE)63 the_task.add_constraint( vy() <= 5.0, rddl.ConstraintType.STATE) 64 the_task.add_constraint( x() >= -100.0, rddl.ConstraintType.STATE)65 the_task.add_constraint( x() >= -100.0, rddl.ConstraintType.STATE)66 the_task.add_constraint( y() >= -100.0, rddl.ConstraintType.STATE)67 the_task.add_constraint( y() <= 100.0, rddl.ConstraintType.STATE)68 # cost function69 Q = ((x() - gx()) * (x() - gx())) + ((y() - gy()) * (y() - gy()))70 # MRJ: RDDL does not support the abs() algebraic construct71 zero = lang.constant(0.0, lang.Real)72 R = ite(t() < H(), (ux() * ux() * 0.01) + (uy() * uy() * 0.01), zero)73 Vx = ite(abs(vx()) <= 5.0, 5.0 - abs(vx()), lang.constant(0.0, lang.Real))74 Vy = ite(abs(vy()) <= 5.0, 5.0 - abs(vy()), lang.constant(0.0, lang.Real))75 X = ite(abs(x()) <= 100.0, 100.0 - abs(x()), lang.constant(0.0, lang.Real))76 Y = ite(abs(y()) <= 100.0, 100.0 - abs(y()), lang.constant(0.0, lang.Real))77 78 #MRJ: flip sign to turn minimisation into maximisation problem79 the_task.reward = -1.0 * (Q + R + Vx + Vy + X + Y)80 # fluent metadata81 the_task.declare_state_fluent(x(), 0.0)82 the_task.declare_state_fluent(y(), 0.0)83 the_task.declare_state_fluent(t(), 0.0)84 the_task.declare_state_fluent(vx(), 0.0)85 the_task.declare_state_fluent(vy(), 0.0)86 the_task.declare_action_fluent(ux(), 0.0)87 the_task.declare_action_fluent(uy(), 0.0)88 the_task.declare_non_fluent(dt(), 0.0)89 the_task.declare_non_fluent(gx(), 0.0)90 the_task.declare_non_fluent(gy(), 0.0)91 the_task.declare_non_fluent(H(), 0.0)92 # definitions93 the_task.x0.setx(x(), 0.0)94 the_task.x0.setx(y(), 0.0)95 the_task.x0.setx(vx(), 0.0)96 the_task.x0.setx(vy(), 0.0)97 the_task.x0.setx(ux(), 0.0)98 the_task.x0.setx(uy(), 0.0)99 the_task.x0.setx(t(), 0.0)100 the_task.x0.setx(dt(), 0.5)101 the_task.x0.setx(H(), float(args.horizon))102 return the_task103def main(args):104 np.random.seed(args.seed)105 for k in tqdm(range(args.num_instances)):106 task_k = make_task(k, args)107 if not os.path.exists(os.path.join(args.output_prefix, task_k.domain_name)):108 os.makedirs(os.path.join(args.output_prefix, task_k.domain_name))109 x = task_k.L.get('x')110 y = task_k.L.get('y')111 112 vx = task_k.L.get('vx')113 vy = task_k.L.get('vy')114 115 gx = task_k.L.get('gx')116 gy = task_k.L.get('gy')117 task_k.x0.setx(x(), np.random.normal(0, 100.0))118 task_k.x0.setx(y(), np.random.normal(0, 100.0))119 120 task_k.x0.setx(vx(), np.random.normal(0, 1.0))121 task_k.x0.setx(vy(), np.random.normal(0, 1.0))122 123 task_k.x0.setx(gx(), np.random.normal(0, 100.0))124 task_k.x0.setx(gy(), np.random.normal(0, 100.0))125 126 the_writer = rddl.Writer(task_k)127 rddl_filename = os.path.join(args.output_prefix,\128 task_k.domain_name,\129 '{}.rddl'.format(task_k.instance_name))130 the_writer.write_model(rddl_filename)131if __name__ == '__main__':132 print('lqr2d.py')133 args = parse_arguments()...

Full Screen

Full Screen

lqg1d.py

Source:lqg1d.py Github

copy

Full Screen

1"""2 1D LQR Navigation problems3"""4import argparse5import os6import numpy as np7from tqdm import tqdm8import tarski9from tarski.theories import Theory10from tarski.syntax import *11from tarski.io import rddl12from tarski.model import Model13from tarski.evaluators.simple import evaluate14from tarski.syntax.arithmetic import *15from tarski.syntax.arithmetic.special import *16from tarski.syntax.arithmetic.random import *17from tarski.rddl import Task18def parse_arguments():19 parser = argparse.ArgumentParser(description='1D LQG problem generators')20 parser.add_argument('--num-instances', default=10, type=int, help='Number of random instances to generate')21 parser.add_argument('--seed', required=True, type=int, help='Seed for the random number generator')22 parser.add_argument('--output-prefix', required=False, default='.', help='Prefix path for output files')23 parser.add_argument('--horizon', required=False, default=20, type=int, help='Horizon of the MDP')24 args = parser.parse_args()25 if not os.path.exists(args.output_prefix):26 print("Creating output directory '{}'...".format(args.output_prefix))27 os.makedirs(args.output_prefix)28 return args29def make_task(k, args):30 lang = tarski.language('lqg_nav_1d', [Theory.EQUALITY, Theory.ARITHMETIC, Theory.SPECIAL, Theory.RANDOM])31 the_task = Task( lang, 'lqg_nav_1d', 'instance_{0:06d}'.format(k))32 the_task.requirements = [rddl.Requirements.CONTINUOUS, rddl.Requirements.REWARD_DET]33 the_task.parameters.discount = 1.034 the_task.parameters.horizon = args.horizon35 the_task.parameters.max_nondef_actions = 'pos-inf'36 # variables37 x = lang.function('x', lang.Real)38 v = lang.function('v', lang.Real)39 u = lang.function('u', lang.Real)40 t = lang.function('t', lang.Real)41 # non fluents42 dt = lang.function('dt', lang.Real)43 gx = lang.function('gx', lang.Real)44 H = lang.function('H', lang.Real)45 # Perturbation distribution parameters46 mu_w = lang.function('mu_w', lang.Real)47 sigma_w = lang.function('sigma_w', lang.Real)48 # cpfs49 the_task.add_cpfs(t(), t() + 1.0)50 the_task.add_cpfs(v(), v() + dt() * u() + normal(mu_w(), sigma_w()))51 the_task.add_cpfs(x(), x() + dt() * v())52 # constraints53 # constraints54 the_task.add_constraint( u() >= -1.0, rddl.ConstraintType.ACTION)55 the_task.add_constraint( u() <= 1.0, rddl.ConstraintType.ACTION)56 the_task.add_constraint( v() >= -5.0, rddl.ConstraintType.STATE)57 the_task.add_constraint( v() <= 5.0, rddl.ConstraintType.STATE)58 the_task.add_constraint( x() >= -100.0, rddl.ConstraintType.STATE)59 the_task.add_constraint( x() <= 100.0, rddl.ConstraintType.STATE)60 # cost function61 Q = (x()-gx()) * (x()-gx())62 # MRJ: RDDL does not support the abs() algebraic construct63 zero = lang.constant(0.0, lang.Real)64 R = ite(t() < H(), u() * u() * 0.01, zero)65 66 V = ite(abs(v()) <= 5.0, 5.0 - abs(v()), lang.constant(0.0, lang.Real))67 X = ite(abs(x()) <= 100.0, 100.0 - abs(x()), lang.constant(0.0, lang.Real))68 69 #R = u() * u() * 0.0170 #MRJ: flip sign to turn minimisation into maximisation problem71 the_task.reward = -1.0 * (Q + R + V + X)72 # fluent metadata73 the_task.declare_state_fluent(x(), 0.0)74 the_task.declare_state_fluent(t(), 0.0)75 the_task.declare_state_fluent(v(), 0.0)76 the_task.declare_action_fluent(u(), 0.0)77 the_task.declare_non_fluent(dt(), 0.0)78 the_task.declare_non_fluent(gx(), 0.0)79 the_task.declare_non_fluent(mu_w(), 0.0)80 the_task.declare_non_fluent(sigma_w(), 0.0) 81 the_task.declare_non_fluent(H(), 0.0)82 # definitions83 the_task.x0.setx(x(), 0.0)84 the_task.x0.setx(v(), 0.0)85 the_task.x0.setx(u(), 0.0)86 the_task.x0.setx(t(), 0.0)87 the_task.x0.setx(dt(), 0.5)88 the_task.x0.setx(mu_w(), 0.0)89 the_task.x0.setx(sigma_w(), 0.05) 90 the_task.x0.setx(H(), float(args.horizon))91 return the_task92def main(args):93 np.random.seed(args.seed)94 for k in tqdm(range(args.num_instances)):95 task_k = make_task(k, args)96 if not os.path.exists(os.path.join(args.output_prefix, task_k.domain_name)):97 os.makedirs(os.path.join(args.output_prefix, task_k.domain_name))98 x = task_k.L.get('x')99 v = task_k.L.get('v')100 gx = task_k.L.get('gx')101 task_k.x0.setx(x(), np.clip(np.random.normal(0, 100.0), -100.0, 100.0))102 task_k.x0.setx(v(), np.clip(np.random.normal(0, 1.0), -5.0, 5.0))103 task_k.x0.setx(gx(), np.clip(np.random.normal(0, 100.0), -100.0, 100.0))104 the_writer = rddl.Writer(task_k)105 rddl_filename = os.path.join(args.output_prefix,\106 task_k.domain_name,\107 '{}.rddl'.format(task_k.instance_name))108 the_writer.write_model(rddl_filename)109if __name__ == '__main__':110 print('lqg1d.py')111 args = parse_arguments()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run locust automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful