def optimizer(model): """ -------------------------------------------------------------------------- template_optimizer: tuning parameters -------------------------------------------------------------------------- """ # Prediction horizon n_horizon = 20 # Robust horizon, set to 0 for standard NMPC n_robust = 0 # open_loop robust NMPC (1) or multi-stage NMPC (0). Only important if n_robust > 0 open_loop = 0 # Sampling time t_step = 0.005 # Simulation time t_end = 0.2 # Choose type of state discretization (collocation or multiple-shooting) state_discretization = 'collocation' # Degree of interpolating polynomials: 1 to 5 poly_degree = 2 # Collocation points: 'legendre' or 'radau' collocation = 'radau' # Number of finite elements per control interval n_fin_elem = 2 # NLP Solver and linear solver nlp_solver = 'ipopt' qp_solver = 'qpoases' # It is highly recommended that you use a more efficient linear solver # such as the hsl linear solver MA27, which can be downloaded as a precompiled # library and can be used by IPOPT on run time linear_solver = 'mumps' # GENERATE C CODE shared libraries NOTE: Not currently supported generate_code = 0 """ -------------------------------------------------------------------------- template_optimizer: uncertain parameters -------------------------------------------------------------------------- """ # Define the different possible values of the uncertain parameters in the scenario tree alpha_values = NP.array([1.0, 1.1, 0.9]) beta_values = NP.array([1.0, 1.1, 0.9]) uncertainty_values = NP.array([alpha_values,beta_values]) # Parameteres of the NLP which may vary along the time (For example a set point that varies at a given time) set_point = SX.sym('set_point') parameters_nlp = NP.array([set_point]) """ -------------------------------------------------------------------------- template_optimizer: time-varying parameters -------------------------------------------------------------------------- """ # Only necessary if time-varying paramters defined in the model # The length of the vector for each parameter should be the prediction horizon # The vectos for each parameter might chance at each sampling time number_steps = int(t_end/t_step) + 1 # Number of time-varying parameters n_tv_p = 2 tv_p_values = NP.resize(NP.array([]),(number_steps,n_tv_p,n_horizon)) for time_step in range (number_steps): if time_step < number_steps/2: tv_param_1_values = 0.6*NP.ones(n_horizon) else: tv_param_1_values = 0.8*NP.ones(n_horizon) tv_param_2_values = 0.9*NP.ones(n_horizon) tv_p_values[time_step] = NP.array([tv_param_1_values,tv_param_2_values]) # Parameteres of the NLP which may vary along the time (For example a set point that varies at a given time) set_point = SX.sym('set_point') parameters_nlp = NP.array([set_point]) """ -------------------------------------------------------------------------- template_optimizer: pass_information (not necessary to edit) -------------------------------------------------------------------------- """ # Check if the user has introduced the data correctly optimizer_dict = {'n_horizon':n_horizon, 'n_robust':n_robust, 't_step': t_step, 't_end':t_end,'poly_degree': poly_degree, 'collocation':collocation, 'n_fin_elem': n_fin_elem,'generate_code':generate_code,'open_loop': open_loop, 'uncertainty_values':uncertainty_values,'parameters_nlp':parameters_nlp, 'state_discretization':state_discretization,'nlp_solver': nlp_solver, 'linear_solver':linear_solver, 'qp_solver':qp_solver, 'tv_p_values':tv_p_values} optimizer_1 = core_do_mpc.optimizer(model,optimizer_dict) return optimizer_1
def optimizer(model): """ -------------------------------------------------------------------------- template_optimizer: tuning parameters -------------------------------------------------------------------------- """ # Prediction horizon #NOTE in setup_nlp Changed from TV_P to TV_P[:,k] n_horizon = 20 # Robust horizon, set to 0 for standard NMPC n_robust = 0 # open_loop robust NMPC (1) or multi-stage NMPC (0). Only important if n_robust > 0 open_loop = 0 t_end = days * hours * minutes + daystart # Number of minutes t_step = minutes / EPTimeStep # 10 min step # Choose type of state discretization (collocation or multiple-shooting) state_discretization = 'discrete-time' # Degree of interpolating polynomials: 1 to 5 poly_degree = 2 # Collocation points: 'legendre' or 'radau' collocation = 'radau' # Number of finite elements per control interval n_fin_elem = 2 # NLP Solver and linear solver nlp_solver = 'ipopt' qp_solver = 'qpoases' # It is highly recommended that you use a more efficient linear solver # such as the hsl linear solver MA27, which can be downloaded as a precompiled # library and can be used by IPOPT on run time linear_solver = 'mumps' # 'MA27' # GENERATE C CODE shared libraries NOTE: Not currently supported generate_code = 0 """ -------------------------------------------------------------------------- template_optimizer: uncertain parameters -------------------------------------------------------------------------- """ # Define the different possible values of the uncertain parameters in the scenario tree alpha_values = NP.array([1, 1]) # beta_values = NP.array([0,0]) uncertainty_values = NP.array([alpha_values]) # Parameteres of the NLP which may vary along the time (For example a set point that varies at a given time) set_point = SX.sym('set_point') parameters_nlp = NP.array([set_point]) """ -------------------------------------------------------------------------- template_optimizer: time-varying parameters -------------------------------------------------------------------------- """ # Only necessary if time-varying paramters defined in the model # The length of the vector for each parameter should be the prediction horizon # The vectos for each parameter might chance at each sampling time number_steps = numSteps / days * 364 # Number of time-varying parameters n_tv_p = 11 tv_p_values = NP.resize(NP.array([]), (number_steps, n_tv_p, n_horizon)) disturbances = NP.load('Neural_Network\disturbances.npy').squeeze() # Create a constraint vector - night constraints - day constraints constraints = createConstraints(19, 17, 22, 20, disturbances.shape[1]) # constraints = createConstraints(23,21,23,21, disturbances.shape[1]) for time_step in range(number_steps): tv_param_1_values = disturbances[0, time_step:time_step + n_horizon] / 63.15 tv_param_2_values = disturbances[1, time_step:time_step + n_horizon] tv_param_3_values = disturbances[2, time_step:time_step + n_horizon] tv_param_4_values = disturbances[3, time_step:time_step + n_horizon] tv_param_5_values = disturbances[4, time_step:time_step + n_horizon] tv_param_6_values = disturbances[5, time_step:time_step + n_horizon] tv_param_7_values = disturbances[6, time_step:time_step + n_horizon] tv_param_8_values = disturbances[7, time_step:time_step + n_horizon] tv_param_9_values = constraints[0, time_step:time_step + n_horizon] tv_param_10_values = constraints[1, time_step:time_step + n_horizon] tv_param_11_values = constraints[2, time_step:time_step + n_horizon] tv_p_values[time_step] = NP.array([ tv_param_1_values, tv_param_2_values, tv_param_3_values, tv_param_4_values, tv_param_5_values, tv_param_6_values, tv_param_7_values, tv_param_8_values, tv_param_9_values, tv_param_10_values, tv_param_11_values ]) # Parameteres of the NLP which may vary along the time (For example a set point that varies at a given time) """ -------------------------------------------------------------------------- template_optimizer: pass_information (not necessary to edit) -------------------------------------------------------------------------- """ # Check if the user has introduced the data correctly optimizer_dict = { 'n_horizon': n_horizon, 'n_robust': n_robust, 't_step': t_step, 't_end': t_end, 'poly_degree': poly_degree, 'collocation': collocation, 'n_fin_elem': n_fin_elem, 'generate_code': generate_code, 'open_loop': open_loop, 'uncertainty_values': uncertainty_values, 'parameters_nlp': parameters_nlp, 'state_discretization': state_discretization, 'nlp_solver': nlp_solver, 'linear_solver': linear_solver, 'qp_solver': qp_solver, 'tv_p_values': tv_p_values } optimizer_1 = core_do_mpc.optimizer(model, optimizer_dict) return optimizer_1
def optimizer(model): """ -------------------------------------------------------------------------- template_optimizer: tuning parameters -------------------------------------------------------------------------- """ # Prediction horizon n_horizon = 20 # Robust horizon, set to 0 for standard NMPC n_robust = 0 # open_loop robust NMPC (1) or multi-stage NMPC (0). Only important if n_robust > 0 open_loop = 0 # Sampling time t_step = 50.0/3600.0 # Simulation time t_end = 2.0 # Choose type of state discretization (collocation or multiple-shooting) state_discretization = 'collocation' # Degree of interpolating polynomials: 1 to 5 poly_degree = 2 # Collocation points: 'legendre' or 'radau' collocation = 'radau' # Number of finite elements per control interval n_fin_elem = 1 # NLP Solver and linear solver nlp_solver = 'ipopt' qp_solver = 'qpoases' # It is highly recommended that you use a more efficient linear solver # such as the hsl linear solver MA27, which can be downloaded as a precompiled # library and can be used by IPOPT on run time linear_solver = 'mumps' # GENERATE C CODE shared libraries NOTE: Not currently supported generate_code = 0 """ -------------------------------------------------------------------------- template_optimizer: uncertain parameters -------------------------------------------------------------------------- """ # Define the different possible values of the uncertain parameters in the scenario tree delH_R_values = NP.array([950.0, 950.0 * 1.30, 950.0 * 0.70]) k_0_values = NP.array([7.0*1.00, 7.0*1.30, 7.0*0.70]) uncertainty_values = NP.array([delH_R_values, k_0_values]) # Parameteres of the NLP which may vary along the time (For example a set point that varies at a given time) set_point = SX.sym('set_point') parameters_nlp = NP.array([set_point]) """ -------------------------------------------------------------------------- template_optimizer: pass_information (not necessary to edit) -------------------------------------------------------------------------- """ # Check if the user has introduced the data correctly optimizer_dict = {'n_horizon':n_horizon, 'n_robust':n_robust, 't_step': t_step, 't_end':t_end,'poly_degree': poly_degree, 'collocation':collocation, 'n_fin_elem': n_fin_elem,'generate_code':generate_code,'open_loop': open_loop, 'uncertainty_values':uncertainty_values,'parameters_nlp':parameters_nlp, 'state_discretization':state_discretization,'nlp_solver': nlp_solver, 'linear_solver':linear_solver, 'qp_solver':qp_solver} optimizer_1 = core_do_mpc.optimizer(model,optimizer_dict) return optimizer_1
def optimizer(model): """ -------------------------------------------------------------------------- template_optimizer: tuning parameters -------------------------------------------------------------------------- """ # Prediction horizon n_horizon = 20 # Robust horizon, set to 0 for standard NMPC n_robust = 0 # open_loop robust NMPC (1) or multi-stage NMPC (0). Only important if n_robust > 0 open_loop = 0 # Sampling time t_step = 0.005 # Simulation time t_end = 0.2 # Choose type of state discretization (collocation or multiple-shooting) state_discretization = 'collocation' # Degree of interpolating polynomials: 1 to 5 poly_degree = 2 # Collocation points: 'legendre' or 'radau' collocation = 'radau' # Number of finite elements per control interval n_fin_elem = 2 # NLP Solver and linear solver nlp_solver = 'ipopt' qp_solver = 'qpoases' # It is highly recommended that you use a more efficient linear solver # such as the hsl linear solver MA27, which can be downloaded as a precompiled # library and can be used by IPOPT on run time linear_solver = 'mumps' # GENERATE C CODE shared libraries NOTE: Not currently supported generate_code = 0 """ -------------------------------------------------------------------------- template_optimizer: uncertain parameters -------------------------------------------------------------------------- """ # Define the different possible values of the uncertain parameters in the scenario tree alpha_values = NP.array([1.0, 1.1, 0.9]) beta_values = NP.array([1.0, 1.1, 0.9]) uncertainty_values = NP.array([alpha_values,beta_values]) # Parameteres of the NLP which may vary along the time (For example a set point that varies at a given time) set_point = SX.sym('set_point') parameters_nlp = NP.array([set_point]) """ -------------------------------------------------------------------------- template_optimizer: time-varying parameters -------------------------------------------------------------------------- """ # Only necessary if time-varying paramters defined in the model # The length of the vector for each parameter should be the prediction horizon # The vectos for each parameter might chance at each sampling time number_steps = int(t_end/t_step) + 1 # Number of time-varying parameters n_tv_p = 2 tv_p_values = NP.resize(NP.array([]),(number_steps,n_tv_p,n_horizon)) for time_step in range (number_steps): if time_step < number_steps/2: tv_param_1_values = 0.6*NP.ones(n_horizon) else: tv_param_1_values = 0.8*NP.ones(n_horizon) tv_param_2_values = 0.9*NP.ones(n_horizon) tv_p_values[time_step] = NP.array([tv_param_1_values,tv_param_2_values]) # Parameteres of the NLP which may vary along the time (For example a set point that varies at a given time) set_point = SX.sym('set_point') parameters_nlp = NP.array([set_point]) """ -------------------------------------------------------------------------- template_optimizer: pass_information (not necessary to edit) -------------------------------------------------------------------------- """ # Check if the user has introduced the data correctly optimizer_dict = {'n_horizon':n_horizon, 'n_robust':n_robust, 't_step': t_step, 't_end':t_end,'poly_degree': poly_degree, 'collocation':collocation, 'n_fin_elem': n_fin_elem,'generate_code':generate_code,'open_loop': open_loop, 'uncertainty_values':uncertainty_values,'parameters_nlp':parameters_nlp, 'state_discretization':state_discretization,'nlp_solver': nlp_solver, 'linear_solver':linear_solver, 'qp_solver':qp_solver, 'tv_p_values':tv_p_values} optimizer_1 = core_do_mpc.optimizer(model,optimizer_dict) return optimizer_1
def optimizer(model, zonenumber): """ -------------------------------------------------------------------------- template_optimizer: tuning parameters -------------------------------------------------------------------------- """ # Prediction horizon n_horizon = 10 # Robust horizon, set to 0 for standard NMPC n_robust = 0 # open_loop robust NMPC (1) or multi-stage NMPC (0). Only important if n_robust > 0 open_loop = 0 t_end = days*hours*minutes + daystart # Number of minutes t_step = minutes/EPTimeStep # 10 min step # Choose type of state discretization (collocation or multiple-shooting) state_discretization = 'discrete-time' # Degree of interpolating polynomials: 1 to 5 poly_degree = 2 # Collocation points: 'legendre' or 'radau' collocation = 'radau' # Number of finite elements per control interval n_fin_elem = 2 # NLP Solver and linear solver nlp_solver = 'ipopt' qp_solver = 'qpoases' # It is highly recommended that you use a more efficient linear solver # such as the hsl linear solver MA27, which can be downloaded as a precompiled # library and can be used by IPOPT on run time linear_solver = 'mumps' # MA27 # GENERATE C CODE shared libraries NOTE: Not currently supported generate_code = 0 """ -------------------------------------------------------------------------- template_optimizer: uncertain parameters -------------------------------------------------------------------------- """ # Define the different possible values of the uncertain parameters in the scenario tree alpha_values = NP.array([1,1]) # beta_values = NP.array([0,0]) uncertainty_values = NP.array([alpha_values]) # Parameteres of the NLP which may vary along the time (For example a set point that varies at a given time) set_point = MX.sym('set_point') parameters_nlp = NP.array([set_point]) """ -------------------------------------------------------------------------- template_optimizer: time-varying parameters -------------------------------------------------------------------------- """ # Only necessary if time-varying paramters defined in the model # The length of the vector for each parameter should be the prediction horizon # The vectos for each parameter might chance at each sampling time number_steps = numSteps/days*364 # Number of time-varying parameters disturbances = NP.load('Neural_Network\disturbances.npy').squeeze() # Create a constraint vector - night constraints - day constraints constraints = createConstraints(model, 19,17,22,20, disturbances.shape[1]) u_values = NP.zeros((4,disturbances.shape[1])) # the disturbances vector contrains internal gains from all zones. Therfore # subtract the number of zones and add 1 for only one internal gain # also add the number of constraints, created above, as well as the number # of blinds which are also tv_p n_tv_p = disturbances.shape[0] - len(zones_Heating) + 1 + constraints.shape[0] + u_values.shape[0] tv_p_values = NP.resize(NP.array([]),(number_steps,n_tv_p,n_horizon)) disturbances = NP.concatenate((disturbances, constraints, u_values)) # pick the correct v_IG (they are all at the beginning) and then the weather information (the rest of the vector) tmp = range(len(zones_Heating),len(zones_Heating)+n_tv_p-1) tmp.insert(0,zonenumber) disturbances = disturbances[tmp,:] for time_step in range (number_steps): for i in range(n_tv_p): tv_p_values[time_step, i] = disturbances[i, time_step: time_step+n_horizon] # Parameteres of the NLP which may vary along the time (For example a set point that varies at a given time) """ -------------------------------------------------------------------------- template_optimizer: pass_information (not necessary to edit) -------------------------------------------------------------------------- """ # Check if the user has introduced the data correctly optimizer_dict = {'n_horizon':n_horizon, 'n_robust':n_robust, 't_step': t_step, 't_end':t_end,'poly_degree': poly_degree, 'collocation':collocation, 'n_fin_elem': n_fin_elem,'generate_code':generate_code,'open_loop': open_loop, 'uncertainty_values':uncertainty_values,'parameters_nlp':parameters_nlp, 'state_discretization':state_discretization,'nlp_solver': nlp_solver, 'linear_solver':linear_solver, 'qp_solver':qp_solver, 'tv_p_values':tv_p_values} optimizer_1 = core_do_mpc.optimizer(model,optimizer_dict) return optimizer_1