# Set parameters. Testing with same file path # filename = 'C:/RH_GH/_verification/170510_ML_Validation_003/02_kevin_prediction_linear_normalized/city_8000_output_normalized.json' # filename = '../../../data/cities/cityIO.json' # filename = '../../../data/cities/city_9000.json' # filename = '../../../data/cities/new_city.json' # filename = '../../../data/cities/latest_city_format.json' filename = '../MachineLearning/runs/65/predicted_cities/SolarSimCity_7000_output_predicted.json' # filename = random.choice(glob.glob('../../../data/train_good/*.json')) # d = '../CityPrediction/input_cities/' # files = glob.glob(d + '*') # filename = max(files, key = os.path.getctime) # Get the latest one log.debug("Getting data from city at {}.".format(os.path.abspath(filename))) # Initialize server, flip ports server = city_udp.City_UDP("CityMatrixSimTestServer", receive_port=SEND_PORT, send_port=RECEIVE_PORT) # Load some test file into a city with open(filename, 'r') as f: json_string = f.read() city = City(json_string) log.debug("Successfully loaded city.") traffic_matrix = city.get_data_matrix(key='traffic') print(traffic_matrix.shape) plt.imshow(traffic_matrix) plt.show() sys.exit(1) # Make some changes for testing purposes
sys.path.extend(['../global/', '../CityPrediction/', '../CityMAItrix/']) import config from utils import * import city_udp import predictor as ML from strategies import random_single_moves as Strategy from objective import objective from cityiograph import * # Check input parameters for AUTO_RESTART value if len(sys.argv) == 2: AUTO_RESTART = False # Create instances of our servers server = city_udp.City_UDP( config.SERVER_NAME, receive_port=config.RECEIVE_PORT, send_port=config.SEND_PORT) unity_server = city_udp.City_UDP( config.UNITY_SERVER_NAME, receive_port=config.UNITY_RECEIVE_PORT, send_port=config.UNITY_SEND_PORT) # Other instance vars log = logging.getLogger('__main__') animBlink = 0 # RZ 170614 PRINT_CITY_RECEIVED = False PRINT_CITY_TO_SEND = True previous_city = None # For comparison purposes AI_move_queue = set() # KL - keep track of previous moves that have been used @atexit.register def register(): """Helper method to close all ports if server is stopped for some reason.
model.load_weights(root_filename + '.h5') # Compile the model with loss, optimizer and metrics model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy']) # Return the final model print("Successfully deserialized our model.") return model # Load model and server model = deserialize_model() server = city_udp.City_UDP("Neural_Network_Model_Server", receive_port=RECEIVE_PORT, send_port=SEND_PORT) # Constantly loop and wait while LISTENING: # Breifly sleep time.sleep(0.1) # Constantly wait for new cities to be received via UDP # Taken directly from Alex's code for regression_server.py # Updated for neural network standards print("Waiting to receive city...") incoming_city = server.receive_city()
import sys import pickle import time import traffic_regression sys.path.insert(0, 'TrafficTreeSim/') import cityiograph sys.path.insert(0, '../') import city_udp model = pickle.load(open('./linear_model.pkl', 'r+b')) server = city_udp.City_UDP("Linear_Model_Server") print("{} listening on ip: {} port: {}".format(server.name, server.receive_ip, server.receive_port)) while (True): time.sleep(0.1) incoming_city = server.receive_city() print("City received") data = traffic_regression.get_features(incoming_city) results = model.predict([data]) outgoing_city = incoming_city traffic_regression.output_to_city(outgoing_city, results[0]) print("Updated city sent") server.send_city(outgoing_city)
# Import local scripts for all key functionality, both ML and AI import sys, logging sys.path.extend(['../global/', '../CityPrediction/', '../CityMAItrix/']) from utils import * import city_udp, simulator, predictor as ML from strategies import random_single_moves as Strategy from objective import objective log = logging.getLogger('__main__') result = None #RZ This is necessary to check if ml_city and ai_city has been calculated onece or not # Check input parameters for AUTO_RESTART value if len(sys.argv) == 2: AUTO_RESTART = False # Create instance of our server server = city_udp.City_UDP(SERVER_NAME, receive_port=RECEIVE_PORT, send_port=SEND_PORT) # Close all ports if server closed @atexit.register def register(): server.close() log.warning("Closing all ports for {}.".format(SERVER_NAME)) # Create instance of our simulator, if needed if DO_SIMULATE: sim = simulator.CitySimulator(SIM_NAME, log) log.info( "{} listening on ip: {}, port: {}. Waiting to receive new city...".format(