我尝试使用模型的敏感度分析来改进我的代码。该模型是基于代理的模型,用于模拟一个国家的经济学。
我需要变量8个参数,每个参数至少超过10个不同的值。所以,我一直这样做“手工”有点复杂(因为我做了一些错误)并且花了很多时间。
我的模型由一个序列构成,我运行“my_control.py”来控制重复次数,并且(我希望将)控制参数选择以进行测试。这个“my_control.py”调用“my_main.py”,其中描述了“orders”,而“main.py”调用“generator.py”来创建我的填充。等等...
因此,我使用“my_control.py”的三个循环中的“sys.argvs”开发了“R style”的过程(因为我在R中有一点经验)。第一个循环是控制区域的数量,第二个循环是在我的8个参数中选择参数,第三个循环用于选择使用选择作为第二个循环的值创建的序列内的值。
理论上它会起作用,但它不起作用。
所以,提前谢谢。
# my_control.py module
__author__ = 'Isaque'
import os
for reg in range(3):
for prm_test in range(10):
for prm_to_run in range(10):
cmd = "python mainmodel.py "+ str(reg) +" "+ str(prm_test) +" "+ str(prm_to_run)
print cmd
os.system(cmd)
# my_parameters.py module
from __future__ import division
import sys
import pandas as pd
import numpy as np
# System constrain: number of households should be larger than number of families
# System constrain: number of members in a family is determined by the average ratio of inputs #agents #families
#CREATING THE PARAMETERS IN DEFAULT VALUES
# RUN PARAMETERS
TOTAL_DAYS = 5040
TOTAL_AGENTS = 1000
TOTAL_FAMILIES = 400
TOTAL_HOUSEHOLDS = 450
TOTAL_FIRMS = 110
ALPHA = .2
BETA = .9
QUANTITY_TO_CHANGE_PRICES = 10
PERCENTAGE_CHANGE_PRICES = 0.05
LABOUR_MARKET = 0.3
SIZE_MARKET = 10
CONSUMPTION_SATISFACTION = .1
PERCENTAGE_CHECK_NEW_LOCATION = 0.1
TAX_CONSUMPTION = 0.01
# AUTOMATED SELECTION OF PARAMETERS
# CREATING A REGIONS NUMBER OBJECT
def prm():
regions = pd.DataFrame({"NUMBER_REGIONS" : [1, 4, 7]})
# CREATING A OBJECT WITH ALL PARAMETERS VARIATION POSSIBLE
prm_parameters = pd.DataFrame({"Parameters":('SIZE_MARKET','ALPHA','BETA','QUANTITY_TO_CHANGE_PRICES', 'PERCENTAGE_CHANGE_PRICES','LABOUR_MARKET','CONSUMPTION_SATISFACTION','PERCENTAGE_CHECK_NEW_LOCATION','TAX_CONSUMPTION')})
# Parameters values
#"prm_start" is the lowest value to test that parameter in the model
#"prm_end" is the highest value to test that parameter in the model
#"prm_num_classes" is the number of classes for the intervall of each o test that parameter in the model
# Market Alpha Beta Qt. %Price Labour Sats. Loc. Tax
prm_start = pd.DataFrame({"Start" : (1, 0.1, 0.1, 10, 0.01, 0.1, 0.01, 0.01, 0.1)})
prm_end = pd.DataFrame({"End" : (50, 1, 1, 1000, 1, 1, 1, 1, 1)})
prm_num_classes = pd.DataFrame({"N_classes": (10, 10, 10, 10, 10, 10, 10, 10, 10)})
parameters_to_test = pd.concat([prm_parameters, prm_start,prm_end,prm_num_classes], axis=1)
# CALLING THE ARGV FROM LOOPS TO SELECT THE VALUES OF PARAMETERS IN MY OBJECT "parameters_to_test"
# "reg_index" will be used to test the different number of regions in the model
reg_index = sys.argv[1]
# "prm_test" and "prm_to_run" will be used to select the parameters values inside of "parameters_to_test" object
prm_test = sys.argv[2]
prm_to_run = sys.argv[3]
#renaming the variable for "NUMBER_REGIONS" selected to test
globals()["NUMBER_REGIONS"] = int(regions.iloc[reg_index])
# Selecting the row (each row represent one parameter)
prm_run = parameters_to_test.iloc[prm_test]
# Creating a variable length to generate the sequence of parameters
interval_out = prm_run['End']/prm_run['N_classes']
# Creating the sequence with all possible values to test
seq_prm_run = pd.DataFrame(np.arange(prm_run['Start'],(prm_run['End']+interval_out),interval_out))
# Selecting the name of variable from the selected row that create the sequence
name_prm = str(prm_run['Parameters'])
#Selecting the value inside the sequence and renaming the variable as the name
globals()[name_prm] = (seq_prm_run.iloc[prm_to_run])[0]
print name_prm
print (seq_prm_run.iloc[prm_to_run])[0]
if __name__ == '__main__':
prm()