def get_random_data(self): self.data.clear() self.data['code'] = get_random_number(0, 10000) self.data['citizens'] = get_random_number(500, 500000) self.data['name'] = get_random_string(size=8) self.data['country'] = get_random_string(size=7) return self.data
def update_product(): rand_id = utils.get_random_number(1, utils.count_rows("product")) product_cost = utils.get_random_number(0, 1000) product_amount = utils.get_random_number(0, 10000) utils.execute( """update product set cost = {PRODUCT_COST} where id = {RAND_ID};""". format(PRODUCT_COST=product_cost, RAND_ID=rand_id)) advance_time()
def insert_product(): product_name = utils.get_random_string() product_cost = utils.get_random_number(0, 1000) product_amount = utils.get_random_number(0, 10000) utils.execute( """INSERT INTO product (name, cost, amount) VALUES ('{PRODUCT_NAME}', {PRODUCT_COST}, {PRODUCT_AMOUNT});""" .format(PRODUCT_NAME=product_name, PRODUCT_COST=product_cost, PRODUCT_AMOUNT=product_amount)) advance_time()
def update_purchase(): customer_num_ids = utils.count_rows("customer") product_num_ids = utils.count_rows("product") purchase_customer_id = utils.get_random_number(1, customer_num_ids) purchase_product_id = utils.get_random_number(1, product_num_ids) utils.execute( """update purchase set customer_id = {PURCHASE_CUSTOMER_ID} where product_id = {PURCHASE_PRODUCT_ID};""" .format(PURCHASE_CUSTOMER_ID=purchase_customer_id, PURCHASE_PRODUCT_ID=purchase_product_id)) advance_time()
def _create_class(self, course, dept): _class = Class(id=self.class_number, department=dept, course=course) self.class_number += 1 _class.meeting_time = deepcopy(self.data.meeting_times[int( len(self.data.meeting_times) * get_random_number())]) _class.room = deepcopy(self.data.rooms[int( len(self.data.rooms) * get_random_number())]) _class.instructor = deepcopy(self.data.instructors[int( len(self.data.instructors) * get_random_number())]) self._classes.append(_class)
def load_object(self, urdf, material, state='closed'): loader = self.scene.create_urdf_loader() self.object = loader.load(urdf, {"material": material}) #self.object = loader.load(urdf, material) pose = Pose([self.object_position_offset, 0, 0], [1, 0, 0, 0]) self.object.set_root_pose(pose) # compute link actor information self.all_link_ids = [l.get_id() for l in self.object.get_links()] self.movable_link_ids = [] for j in self.object.get_joints(): if j.get_dof() == 1: self.movable_link_ids.append(j.get_child_link().get_id()) if self.flog is not None: self.flog.write('All Actor Link IDs: %s\n' % str(self.all_link_ids)) self.flog.write('All Movable Actor Link IDs: %s\n' % str(self.movable_link_ids)) # set joint property for joint in self.object.get_joints(): joint.set_drive_property(stiffness=0, damping=10) # set initial qpos joint_angles = [] self.joint_angles_lower = [] self.joint_angles_upper = [] for j in self.object.get_joints(): if j.get_dof() == 1: l = process_angle_limit(j.get_limits()[0, 0]) self.joint_angles_lower.append(float(l)) r = process_angle_limit(j.get_limits()[0, 1]) self.joint_angles_upper.append(float(r)) if state == 'closed': joint_angles.append(float(l)) elif state == 'open': joint_angles.append(float(r)) elif state == 'random-middle': joint_angles.append(float(get_random_number(l, r))) elif state == 'random-closed-middle': if np.random.random() < 0.5: joint_angles.append(float(get_random_number(l, r))) else: joint_angles.append(float(l)) else: raise ValueError('ERROR: object init state %s unknown!' % state) self.object.set_qpos(joint_angles) return joint_angles
def update_category(): rand_id = utils.get_random_number(1, utils.count_rows("category")) rand_name = utils.get_random_string() utils.execute( """update category set name = '{RAND_NAME}' where id = {RAND_ID};""". format(RAND_NAME=rand_name, RAND_ID=rand_id)) advance_time()
def update_customer(): rand_id = utils.get_random_number(1, utils.count_rows("customer")) customer_active = utils.get_random_boolean() utils.execute( """update customer set active = '{CUSTOMER_ACTIVE}' where id = {RAND_ID};""" .format(CUSTOMER_ACTIVE=customer_active, RAND_ID=rand_id)) advance_time()
def __init__(self, src_ip="", dest_ip="", data=""): ''' src_ip : source ip address dest_ip : destination ip address data : data to be sent version : ip version, can be IPv4 or IPv6 ihl : header length type of service : default is 0 total_length : total length of the datagram id : datagram id flag_reserved : default is 0 flag_df : do not fragment flag flag_mf : more fragments flag fragment_offset : used in fragmentation ttl : time to live protocol : transfer layer protocol header_checksum : checksum of the ip header ''' self.src_ip = src_ip self.dest_ip = dest_ip self.data = data self.version = 4 self.ihl = 5 self.type_of_service = 0 self.total_length = calcsize(PACKER_FORMAT) + len(data) self.id = get_random_number(0, 65535) self.flag_reserved = 0 self.flag_df = 1 self.flag_mf = 0 self.fragment_offset = 0 self.ttl = 255 self.protocol = socket.IPPROTO_TCP self.header_checksum = 0
def delete_category_product(): ## Delete some pair found_good_pair = False while not found_good_pair: category_num_ids = utils.count_rows("category") _category_id = utils.get_random_number(1, category_num_ids) product_num_ids = utils.count_rows("product") _product_id = utils.get_random_number(1, product_num_ids) pair = (_category_id, _product_id) if pair in _category_product_pairs: #_category_product_pairs.remove(pair) found_good_pair = True break if not found_good_pair: return category_product_category_id = pair[0] category_product_product_id = pair[1] _category_product_pairs.remove(pair) #utils.execute("""update category_product set category_id = {CATEGORY_PRODUCT_CATEGORY_ID} where product_id = {CATEGORY_PRODUCT_PRODUCT_ID};""".format( # CATEGORY_PRODUCT_CATEGORY_ID=category_product_category_id, CATEGORY_PRODUCT_PRODUCT_ID=category_product_product_id)) utils.execute( """delete from category_product where category_id = {CATEGORY_PRODUCT_CATEGORY_ID} and product_id = {CATEGORY_PRODUCT_PRODUCT_ID};""" .format(CATEGORY_PRODUCT_CATEGORY_ID=category_product_category_id, CATEGORY_PRODUCT_PRODUCT_ID=category_product_product_id)) ## Insert modification of the deleted key found_new_pair = False while not found_new_pair: product_num_ids = utils.count_rows("product") _product_id = utils.get_random_number(1, product_num_ids) new_pair = (category_product_category_id, _product_id) if new_pair not in _category_product_pairs: #_category_product_pairs.remove(pair) found_new_pair = True break if not found_new_pair: return new_category_product_product_id = new_pair[1] _category_product_pairs.append(new_pair) utils.execute( """INSERT INTO category_product (category_id, product_id) VALUES ({CATEGORY_PRODUCT_CATEGORY_ID}, {CATEGORY_PRODUCT_PRODUCT_ID});""" .format(CATEGORY_PRODUCT_CATEGORY_ID=category_product_category_id, CATEGORY_PRODUCT_PRODUCT_ID=new_category_product_product_id)) advance_time()
def select_tournament_population(self, population): tournament_population = Population(size=self.TOURNAMENT_SELECTION_SIZE, data=self.data) for idx in range(self.TOURNAMENT_SELECTION_SIZE): tournament_population.schedules[idx] = population.schedules[int( get_random_number() * len(population.schedules))] return tournament_population
def crossover_schedule(self, schedule1, schedule2): _crossover_schedule = Schedule(data=self.data).initialize() for idx in range(len(_crossover_schedule.classes)): if get_random_number() > 0.5: _crossover_schedule.classes[idx] = deepcopy( schedule1.classes[idx]) else: _crossover_schedule.classes[idx] = deepcopy( schedule2.classes[idx]) return _crossover_schedule
def mutate_schedule(self, _mutate_schedule): _mutate_schedule = deepcopy(_mutate_schedule) _schedule = Schedule(data=self.data).initialize( ) #_schedule est donc ici un schedule different # Remplace les classes du schedule d'entree par celle mutee aleatoirement for idx in range(len(_mutate_schedule.classes)): if self.MUTATION_RATE > get_random_number(): _mutate_schedule.classes[idx] = deepcopy( _schedule.classes[idx]) return _mutate_schedule
def solve_task(task_id, random_no_count, random_min, random_max): print("Solving task : {}, random_no_count : {}".format(task_id, random_no_count)) task_start_time = time.time() sum = 0 for counter in range(random_no_count): random_number = get_random_number(random_min, random_max) sum += random_number task_finish_time = time.time() solution = {"id": task_id, "st_time": task_start_time, "end_time": task_finish_time, "sum": sum} print("Solved task : {}".format(task_id)) return solution
def __init__(self, src_ip="", src_port=0, dest_ip="", dest_port=0, data=""): ''' src_ip : source ip src_port : source port dest_ip : destination ip dest_port : destination port data : data to be sent seq_num : sequence number ack_num : acknowledge number data_offset : size of the TCP header reserverd : default is 0 urg, ack, psh, rst, syn, fin : flags window_size : slide window size checksum : TCP checksum urgent_pointer : default is 0 options : optional, contains some special information ''' self.src_ip = src_ip self.src_port = src_port self.dest_ip = dest_ip self.dest_port = dest_port self.data = data self.seq_num = get_random_number() self.ack_num = 0 self.data_offset = 5 self.reserved = 0 self.urg = 0 self.ack = 0 self.psh = 0 self.rst = 0 self.syn = 0 self.fin = 0 self.window_size = 29200 self.checksum = 0 self.urgent_pointer = 0 self.options = 0 self.data = data
def crossover_population(self, population): _cross_over_popluation = Population(size=len(population.schedules), data=self.data) for idx in range(self.NUMB_OF_ELITE_SCHEDULES): _cross_over_popluation.schedules[idx] = deepcopy( population.schedules[idx]) for idx in range(self.NUMB_OF_ELITE_SCHEDULES, len(population.schedules)): if self.CROSSOVER_RATE > get_random_number(): schedule1 = self.select_tournament_population( population).sort_by_fitness().schedules[0] schedule2 = self.select_tournament_population( population).sort_by_fitness().schedules[1] _cross_over_popluation.schedules[ idx] = self.crossover_schedule(schedule1, schedule2) else: _cross_over_popluation.schedules[idx] = deepcopy( population.schedules[idx]) return _cross_over_popluation
self.executor = ThreadPoolExecutor(thread_pool_size) self.pubsub = get_pubsub() self._task_channel = "task" self._solution_channel = "solution" self._continuous_loop_sleep_interval = 0.01 # in a thread self.executor.submit(self.listen_for_solutions) # def generate_tasks(self, random_no_min=10_000_000, random_no_max=100_000_000, sleep_interval=0.5): def generate_tasks(self, random_no_min=10_000, random_no_max=100_000, sleep_interval=0.5): for task_counter in range(1, self.no_of_tasks_to_produce + 1): if task_counter > 0 and task_counter % 10 == 0: print("Produced task : {}".format(task_counter)) random_number = get_random_number(random_no_min, random_no_max) task_id = str(uuid.uuid4()) # publish problem task = {'id': task_id, 'number': random_number} self.pubsub.publish(self._task_channel, json.dumps(task)) sleep_interval = get_random_number(0, sleep_interval) time.sleep(sleep_interval) def release_resources(self): self.pubsub.close() self.executor.shutdown() def listen_for_solutions(self): solutions = [] subscriber = self.pubsub.pubsub() subscriber.subscribe(self._solution_channel)
def _get_pair_category_product(): category_num_ids = utils.count_rows("category") product_num_ids = utils.count_rows("product") category_product_category_id = utils.get_random_number(1, category_num_ids) category_product_product_id = utils.get_random_number(1, product_num_ids) return (category_product_category_id, category_product_product_id)
def _get_pair_customer_product(): customer_num_ids = utils.count_rows("customer") product_num_ids = utils.count_rows("product") purchase_customer_id = utils.get_random_number(1, customer_num_ids) purchase_product_id = utils.get_random_number(1, product_num_ids) return (purchase_customer_id, purchase_product_id)
def create_testdb_history(): print(sys.argv) IS_HISTORY = False if len(sys.argv) == 2 and sys.argv[1] == "history": IS_HISTORY = True if IS_HISTORY: DBNAME = "testdb_history" else: DBNAME = "testdb" USER = "******" PASSWORD = "******" utils.init(DBNAME, USER, PASSWORD) TIME = {"hour": 12, "minute": 0, "day": 1, "month": 1, "year": 1990} utils.set_db_time(hour=TIME["hour"], day=TIME["day"], month=TIME["month"], year=TIME["year"]) DB_CHANGES = 10000 ## Create model and history tables utils.drop_all_user_tables(USER) utils.execute_from_file("model.sql") tables = utils.get_all_user_tables(USER) for t in tables: table = t[0] if IS_HISTORY: utils.table_attach_history(table) utils.create_history_table(table) ## list of database modification functions functions = [ insert_category, update_category, insert_product, update_product, insert_customer, update_customer, insert_category_product, insert_purchase ] functions_history = [] if IS_HISTORY: functions_history.append(update_purchase) functions_history.append(delete_category_product) ## initial inserts def _init(): insert_category() insert_product() insert_customer() insert_category_product() insert_purchase() for _ in range(20): _init() ## db life simulation for i in range(DB_CHANGES): fcn = functions[utils.get_random_number(0, len(functions) - 1)] print(fcn.__name__) fcn() if IS_HISTORY: if utils.get_random_number(1, 10) == 1: fcn_history = functions_history[utils.get_random_number( 0, len(functions_history) - 1)] print(fcn_history.__name__) fcn_history()
def home(): return render_template('index.html', secret=get_random_number())
def get_word() -> str: return WORD_LIST[get_random_number(0, len(WORD_LIST) - 1)]