How to use num_requests method in locust

Best Python code snippet using locust

reoptimization_model.py

Source:reoptimization_model.py Github

copy

Full Screen

1import gurobipy as gp2from gurobipy import GRB3from gurobipy import GurobiError4from gurobipy import quicksum5import graphviz6from models.reoptimization_config import *7from models.updater_for_reopt import *8from models.updater_for_reopt import Updater9class ReoptModel:10 def __init__(self, current_route_plan, event, num_requests, first, rejected):11 self.model = "MIP 1"12 self.route_plan = current_route_plan13 self.event = event14 self.num_requests = num_requests15 self.updater = Updater(16 self.route_plan, self.event, self.num_requests, first, rejected17 )18 def vizualize_route(self, results, num_nodes_and_depots):19 dot = graphviz.Digraph(engine="neato")20 colors = [21 "fuchsia",22 "blue",23 "green",24 "aquamarine",25 "bisque",26 "black",27 "blueviolet",28 "brown",29 "chartreuse",30 "cornflowerblue",31 "purple",32 "darkmagenta",33 "dodgerblue",34 "greenyellow",35 "mediumseagreen",36 "navy",37 ]38 nodes = [i for i in range(num_nodes_and_depots)]39 for node in nodes:40 # nodes41 state = "Pickup" if node < self.num_requests else "Dropoff"42 state = "Depot" if node >= 2 * self.num_requests else state43 number = node if node < self.num_requests else node - self.num_requests44 printable_label = (45 f"State: {state}"46 f"\nPos: {Position[node][0],Position[node][1]}"47 f"\nRequest No: {number}"48 )49 dot.node(50 name=str(node),51 label=printable_label,52 pos=f"{Position[node][0]},{Position[node][1]}!",53 )54 for v in results:55 # edges56 if v.varName.startswith("x") and v.x > 0:57 var = (58 str(v.varName)59 .replace("x", "")60 .replace("[", "")61 .replace("]", "")62 .split(",")63 )64 try:65 edgelabel = datetime.fromtimestamp(66 next(67 a.x for a in results if a.varName == f"t[{var[0]},{var[2]}]"68 )69 ).strftime("%Y-%m-%d %H:%M:%S")70 dot.edge(71 str(var[0]),72 str(var[1]),73 label=edgelabel,74 color=colors[int(var[2])],75 )76 except StopIteration as e:77 dot.edge(78 str(var[0]),79 str(var[1]),80 color=colors[int(var[2])],81 )82 continue83 dot.render(filename="route.gv", cleanup=True, view=True)84 def run_model(self):85 # update sets with new request86 (87 pickups_remaining,88 pickups_new,89 pickups,90 pickups_previous_not_rejected,91 nodes_previous_not_rejected,92 nodes_depots,93 nodes_remaining,94 nodes_new,95 nodes,96 fixate_x,97 fixate_t,98 T_O,99 D_ij,100 T_ij,101 T_S_L,102 T_S_U,103 T_H_L,104 T_H_U,105 M_ij,106 L_S,107 L_W,108 rejected,109 ) = self.updater.update()110 try:111 m = gp.Model("mip1")112 m.setParam("NumericFocus", 3)113 dropoffs = [i for i in range(self.num_requests, 2 * self.num_requests)]114 vehicles = [i for i in range(num_vehicles)]115 # Create variables116 x = m.addVars(117 nodes_depots, nodes_depots, vehicles, vtype=GRB.BINARY, name="x"118 )119 q_S = m.addVars(nodes_depots, vehicles, vtype=GRB.INTEGER, name="q_S")120 q_W = m.addVars(nodes_depots, vehicles, vtype=GRB.INTEGER, name="q_W")121 t = m.addVars(nodes, name="t")122 l = m.addVars(nodes, name="l")123 u = m.addVars(nodes, name="u")124 d = m.addVars(pickups, name="d")125 s = m.addVars(pickups, vtype=GRB.BINARY, name="s")126 z_plus = m.addVars(nodes_previous_not_rejected, name="z+")127 z_minus = m.addVars(nodes_previous_not_rejected, name="z-")128 y = m.addVars(vehicles, vtype=GRB.BINARY, name="y")129 # OBJECTIVE FUNCTION130 m.setObjectiveN(131 beta132 * (133 quicksum(134 C_D * D_ij[i][j] * x[i, j, k]135 for i in nodes_depots136 for j in nodes_depots137 for k in vehicles138 if j != (2 * self.num_requests + k + num_vehicles)139 )140 + quicksum(C_K * y[k] for k in vehicles)141 ),142 index=0,143 )144 m.setObjectiveN(145 (1 - beta)146 * (147 quicksum(C_T * (l[i] + u[i]) for i in nodes)148 + quicksum(C_F * d[i] for i in pickups)149 + quicksum(C_R * s[i] for i in pickups)150 + quicksum(151 C_O * (z_plus[i] + z_minus[i])152 for i in nodes_previous_not_rejected153 )154 ),155 index=1,156 )157 m.ModelSense = GRB.MINIMIZE158 # ARC ELIMINATION159 # cannot drive from pick-up nodes to destinations160 for v in vehicles:161 for k in vehicles:162 for i in pickups:163 x[i, 2 * self.num_requests + v + num_vehicles, k].lb = 0164 x[i, 2 * self.num_requests + v + num_vehicles, k].ub = 0165 # cannot drive from origins to drop-offs166 for v in vehicles:167 for k in vehicles:168 for j in dropoffs:169 x[2 * self.num_requests + v, j, k].lb = 0170 x[2 * self.num_requests + v, j, k].ub = 0171 # cannot drive from own drop-off to own pick-up172 for k in vehicles:173 for i in pickups:174 x[self.num_requests + i, i, k].lb = 0175 x[self.num_requests + i, i, k].ub = 0176 # cannot drive from itself to itself177 for k in vehicles:178 for i in pickups:179 x[i, i, k].lb = 0180 x[i, i, k].ub = 0181 # cannot drive into an origin182 for v in vehicles:183 for k in vehicles:184 for i in nodes_depots:185 x[i, 2 * self.num_requests + v, k].lb = 0186 x[i, 2 * self.num_requests + v, k].ub = 0187 # cannot drive from a destination188 for v in vehicles:189 for k in vehicles:190 for j in nodes_depots:191 x[2 * self.num_requests + v + num_vehicles, j, k].lb = 0192 x[2 * self.num_requests + v + num_vehicles, j, k].ub = 0193 # cannot drive from origins that are not their own194 for v in vehicles:195 for k in vehicles:196 if k != v:197 for j in nodes_depots:198 x[2 * self.num_requests + v, j, k].lb = 0199 x[2 * self.num_requests + v, j, k].ub = 0200 # cannot drive into destinations that are not their own201 for v in vehicles:202 for k in vehicles:203 if k != v:204 for i in nodes_depots:205 x[i, 2 * self.num_requests + v + num_vehicles, k].lb = 0206 x[i, 2 * self.num_requests + v + num_vehicles, k].ub = 0207 # not add arc if vehicle cannot reach node j from node i within the time window of j208 for k in vehicles:209 for i in nodes:210 for j in nodes:211 if T_H_L[i] + S + T_ij[i][j] > T_H_U[j]:212 x[i, j, k].lb = 0213 x[i, j, k].ub = 0214 # not add swip-tour to node i from node j if that means node (j+n) cannot be reached in time215 for k in vehicles:216 for i in pickups:217 for j in pickups:218 if (219 T_H_L[j]220 + S221 + T_ij[j][i]222 + S223 + T_ij[i][j + self.num_requests]224 > T_H_U[j + self.num_requests]225 ):226 x[i, j + self.num_requests, k].lb = 0227 x[i, j + self.num_requests, k].ub = 0228 # not add arc if route from node (i+n) to node j means that node (j+n) cannot be reached in time229 for k in vehicles:230 for i in pickups:231 for j in pickups:232 if (233 T_H_L[i]234 + S235 + T_ij[i][i + self.num_requests]236 + S237 + T_ij[i + self.num_requests][j]238 + S239 + T_ij[j][j + self.num_requests]240 > T_H_U[j + self.num_requests]241 ):242 x[i + self.num_requests, j, k].lb = 0243 x[i + self.num_requests, j, k].ub = 0244 # FIXATING VALUES245 # x values - outside time window246 for f_x in fixate_x:247 x[f_x[0], f_x[1], f_x[2]].lb = 1248 x[f_x[0], f_x[1], f_x[2]].ub = 1249 # t values - historic values250 for f_t in fixate_t:251 t[f_t].lb = fixate_t[f_t]252 t[f_t].ub = fixate_t[f_t]253 # rejected requests in previous plans254 for i in rejected:255 s[i].lb = 1256 s[i].ub = 1257 # FLOW CONSTRAINTS258 m.addConstrs(259 (260 quicksum(x[i, j, k] for j in nodes_depots for k in vehicles) == 1261 for i in pickups_remaining262 ),263 name="Flow1",264 )265 m.addConstrs(266 (267 quicksum(x[2 * self.num_requests + k, j, k] for j in nodes_depots)268 == 1269 for k in vehicles270 ),271 name="Flow3.1",272 )273 m.addConstrs(274 (275 quicksum(276 x[i, 2 * self.num_requests + k + num_vehicles, k]277 for i in nodes_depots278 )279 == 1280 for k in vehicles281 ),282 name="Flow3.2",283 )284 m.addConstrs(285 (286 quicksum(x[i, j, k] for j in nodes_depots)287 - quicksum(x[self.num_requests + i, j, k] for j in nodes_depots)288 == 0289 for i in pickups290 for k in vehicles291 ),292 name="Flow6",293 )294 m.addConstrs(295 (296 quicksum(x[j, i, k] for j in nodes_depots)297 - quicksum(x[i, j, k] for j in nodes_depots)298 == 0299 for i in nodes300 for k in vehicles301 ),302 name="Flow7",303 )304 # STANDARD SEATS CAPACITY CONSTRAINTS305 m.addConstrs(306 (307 q_S[i, k] + L_S[j] - q_S[j, k] <= (Q_S + L_S[j]) * (1 - x[i, j, k])308 for j in pickups309 for i in nodes_depots310 for k in vehicles311 ),312 name="SCapacity1",313 )314 m.addConstrs(315 (316 q_S[i, k] - L_S[j] - q_S[self.num_requests + j, k]317 <= Q_S * (1 - x[i, self.num_requests + j, k])318 for j in pickups319 for i in nodes_depots320 for k in vehicles321 ),322 name="SCapacity2",323 )324 m.addConstrs(325 (326 quicksum(L_S[i] * x[i, j, k] for j in nodes_depots) <= q_S[i, k]327 for i in pickups328 for k in vehicles329 ),330 name="SCapacity3.1",331 )332 m.addConstrs(333 (334 q_S[i, k] <= quicksum(Q_S * x[i, j, k] for j in nodes_depots)335 for i in pickups336 for k in vehicles337 ),338 name="SCapacity3.2",339 )340 m.addConstrs(341 (342 quicksum(343 (Q_S - L_S[i]) * x[self.num_requests + i, j, k]344 for j in nodes_depots345 )346 >= q_S[self.num_requests + i, k]347 for i in pickups348 for k in vehicles349 ),350 name="SCapacity5",351 )352 m.addConstrs(353 (354 q_S[i, k]355 <= Q_S * (1 - x[i, 2 * self.num_requests + k + num_vehicles, k])356 for i in dropoffs357 for k in vehicles358 ),359 name="SCapacity6",360 )361 # WHEELCHAIR SEATS CAPACITY CONSTRAINTS362 m.addConstrs(363 (364 q_W[i, k] + L_W[j] - q_W[j, k] <= (Q_W + L_W[j]) * (1 - x[i, j, k])365 for j in pickups366 for i in nodes_depots367 for k in vehicles368 ),369 name="WCapacity1",370 )371 m.addConstrs(372 (373 q_W[i, k] - L_W[j] - q_W[self.num_requests + j, k]374 <= Q_W * (1 - x[i, self.num_requests + j, k])375 for j in pickups376 for i in nodes_depots377 for k in vehicles378 ),379 name="WCapacity2",380 )381 m.addConstrs(382 (383 quicksum(L_W[i] * x[i, j, k] for j in nodes_depots) <= q_W[i, k]384 for i in pickups385 for k in vehicles386 ),387 name="WCapacity3.1",388 )389 m.addConstrs(390 (391 q_W[i, k] <= quicksum(Q_W * x[i, j, k] for j in nodes_depots)392 for i in pickups393 for k in vehicles394 ),395 name="WCapacity3.2",396 )397 m.addConstrs(398 (399 quicksum(400 (Q_W - L_W[i]) * x[self.num_requests + i, j, k]401 for j in nodes_depots402 )403 >= q_W[self.num_requests + i, k]404 for i in pickups405 for k in vehicles406 ),407 name="WCapacity4",408 )409 m.addConstrs(410 (411 q_W[i, k]412 <= Q_W * (1 - x[i, 2 * self.num_requests + k + num_vehicles, k])413 for i in dropoffs414 for k in vehicles415 ),416 name="WCapacity6",417 )418 # TIME WINDOW CONSTRAINTS419 m.addConstrs(420 (T_S_L[i] - l[i] <= t[i] for i in nodes),421 name="TimeWindow1.1",422 )423 m.addConstrs(424 (t[i] <= T_S_U[i] + u[i] for i in nodes),425 name="TimeWindow1.2",426 )427 m.addConstrs(428 (T_H_L[i] <= t[i] for i in nodes),429 name="TimeWindow2.1",430 )431 m.addConstrs(432 (t[i] <= T_H_U[i] for i in nodes),433 name="TimeWindow2.2",434 )435 m.addConstrs(436 (437 t[i] + S + T_ij[i][j] - t[j] <= M_ij[i][j] * (1 - x[i, j, k])438 for i in nodes439 for j in nodes440 for k in vehicles441 ),442 name="TimeWindow3",443 )444 m.addConstrs(445 (446 t[i] + S + T_ij[i][self.num_requests + i]447 <= t[self.num_requests + i]448 for i in pickups449 ),450 name="TimeWindow4",451 )452 m.addConstrs(453 (454 T_O[i] - t[i] == z_plus[i] - z_minus[i]455 for i in nodes_previous_not_rejected456 ),457 name="TimeWindow5",458 )459 # RIDE TIME CONSTRAINTS460 m.addConstrs(461 (462 d[i]463 >= t[self.num_requests + i]464 - (t[i] + (1 + F + M * s[i]) * T_ij[i][self.num_requests + i])465 for i in pickups466 ),467 name="RideTime1",468 )469 # REJECTION CONSTRAINTS470 m.addConstrs(471 (472 s[i]473 == 1 - quicksum(x[i, j, k] for j in nodes_depots for k in vehicles)474 for i in pickups_new475 ),476 name="Rejection1",477 )478 m.addConstr(479 (quicksum(s[i] for i in pickups_previous_not_rejected) == 0),480 name="Rejection2",481 )482 # RUN MODEL483 m.optimize()484 # m.computeIIS()485 # m.write("model.ilp")486 print("New Event: ", self.num_requests - 1)487 for i in pickups_new:488 print(s[i].varName, s[i].x)489 if s[i].x > 0.1:490 print("Your request has been rejected:/")491 rejected.append(i)492 """493 for v in m.getVars():494 if v.varName.startswith("t"):495 continue496 if v.x > 0:497 print("%s %g" % (v.varName, v.x))498 for i in nodes:499 print(500 t[i].varName,501 datetime.utcfromtimestamp(t[i].x * 3600).strftime(502 "%Y-%m-%d %H:%M:%S"503 ),504 )505 """506 # print("Obj: %g" % m.objVal)507 print(beta)508 obj1 = m.getObjective(index=0)509 print("Operational costs: ", obj1.getValue())510 obj2 = m.getObjective(index=1)511 print("Quality of service: ", obj2.getValue())512 obj3 = obj1.getValue() + obj2.getValue()513 print("Total: ", obj3)514 operational = obj1.getValue()515 quality = obj2.getValue()516 single_z = quicksum(517 C_O * (z_plus[i] + z_minus[i]) for i in nodes_previous_not_rejected518 )519 for i in nodes:520 print(t[i].varName, t[i].x)521 # print("Obj: %g" % m.objVal)522 """523 NOTE524 self.vizualize_route(525 results=m.getVars(), num_nodes_and_depots=len(nodes_depots)526 )527 """528 route_plan = dict()529 route_plan["x"] = {k: v.X for k, v in x.items()}530 route_plan["t"] = {k: v.X for k, v in t.items()}531 route_plan["q_S"] = {k: v.X for k, v in q_S.items()}532 route_plan["q_W"] = {k: v.X for k, v in q_W.items()}533 num_not_used_vehicles = len(534 [535 k536 for k in vehicles537 if route_plan["x"][538 (539 2 * (self.num_requests - 1) + k,540 2 * (self.num_requests - 1) + k + num_vehicles,541 k,542 )543 ]544 == 1545 ]546 )547 single_z = single_z.getValue()548 return (549 route_plan,550 rejected,551 num_not_used_vehicles,552 operational,553 quality,554 single_z,555 )556 except GurobiError as e:557 print("Error reported")...

Full Screen

Full Screen

api.py

Source:api.py Github

copy

Full Screen

...30 querystr=querystr,31 )32 return url33@pytest.fixture(name="num_requests")34def fixture_num_requests():35 """Return a defaultdict to count requests to HTTP handlers."""36 return collections.defaultdict(int)37@pytest.fixture(name="try_get")38def fixture_try_get(num_requests):39 """Return a function to try GET requests to the server."""40 def try_get(httpd, querystr):41 """Try GET requests to the server."""42 num_requests["get_handler"] = 043 f = urlopen(httpd_url(httpd, "/api/resource/1", querystr))44 assert f.getcode() == 20045 assert json.loads(f.read()) == {"called": 1, "id": "1", "query": querystr}46 assert num_requests["get_handler"] == 147 return try_get48@pytest.fixture(name="try_post")...

Full Screen

Full Screen

rrclient.py

Source:rrclient.py Github

copy

Full Screen

1#!/usr/bin/env python2"""3synopsis:4 Request-reply client in Python5 Connects REQ socket to tcp://localhost:55596 Sends "Hello" to server, expects "World" back7 Modified for async/ioloop: Dave Kuhlman <dkuhlman(at)davekuhlman(dot)org>8usage:9 python rrclient.py [num_requests]10 where:11 num_requests is the number of requests to be sent. Default12 if omitted is 10.13notes:14 To run this, start rrbroker.py, any number of instances of rrworker.py,15 and rrclient.py.16"""17import sys18import zmq19from zmq.asyncio import Context, ZMQEventLoop20import asyncio21@asyncio.coroutine22def run_client(context, num_requests):23 # Prepare our context and sockets24 socket = context.socket(zmq.REQ)25 socket.connect("tcp://localhost:5559")26 # Do num_requests requests, waiting each time for a response27 for requestno in range(1, num_requests + 1):28 message = 'Hello {}'.format(requestno)29 message = message.encode('utf-8')30 yield from socket.send(message)31 message = yield from socket.recv()32 print("Received reply %s [%s]" % (requestno, message))33@asyncio.coroutine34def run(loop, num_requests):35 context = Context()36 yield from run_client(context, num_requests)37def main():38 args = sys.argv[1:]39 if len(args) == 1:40 num_requests = int(args[0])41 elif len(args) == 0:42 num_requests = 1043 else:44 sys.exit(__doc__)45 try:46 loop = ZMQEventLoop()47 asyncio.set_event_loop(loop)48 loop.run_until_complete(run(loop, num_requests))49 except KeyboardInterrupt:50 print('\nFinished (interrupted)')51if __name__ == '__main__':...

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