def setUp(self): choices = ['SOV', 'HOV'] coefficients = [{'Constant': 2, 'Var1': 2.11}, {'Constant': 1.2}] data = array([[1, 1.1], [1, -0.25], [1, 3.13], [1, -0.11]]) self.choiceset1 = DataArray(ma.array([[0, 1], [0, 1], [1, 1], [1, 1]]), ['SOV', 'HOV']) self.data = DataArray(data, ['Constant', 'Var1']) self.specification = Specification(choices, coefficients) self.utils_array_act = zeros( (self.data.rows, self.specification.number_choices)) self.utils_array_act[:, 0] = self.data.data[:, 0] * 2 + self.data.data[:, 1] * 2.11 self.utils_array_act[:, 1] = self.data.data[:, 0] * 1.2 self.exp_utils_array_act = exp(self.utils_array_act) self.prob_array_act = ( self.exp_utils_array_act.transpose() / self.exp_utils_array_act.cumsum(-1)[:, -1]).transpose() # for the selected data, and seed = 1, chosen alternatives are self.selected_act = array([['sov'], ['hov'], ['sov'], ['sov']]) self.selected_act1 = array([['hov'], ['hov'], ['sov'], ['sov']])
def calc_chosenalternative(self, data, choiceset=None, seed=1): """ The method returns the selected choice among the available alternatives. Inputs: data = DataArray object choiceset = DataArray object """ if choiceset is None: choiceset = DataArray(array([]), []) probabilities = DataArray(self.calc_probabilities(data, choiceset), self.specification.choices) prob_model = AbstractProbabilityModel(probabilities, seed) return prob_model.selected_choice()
def resolve_consistency(self, data, seed): print data.varnames actList = [] actListJoint = [] data.sort([self.activityAttribs.hidName, self.activityAttribs.pidName, self.activityAttribs.scheduleidName]) # Create Index Matrix self.create_indices(data) self.create_col_numbers(data._colnames) #if self.childDepProcessingType == 'Dummy': # return data for hhldIndex in self.hhldIndicesOfPersons: ti = time.time() firstPersonRec = hhldIndex[1] lastPersonRec = hhldIndex[2] persIndicesForActsForHhld = self.personIndicesOfActs[firstPersonRec: lastPersonRec, :] #if hhldIndex[0] not in [8843,20008,20440,30985,48365,54850,60085,64006,68037,73093,77192,84903,84963]: # 1047 # continue householdObject = Household(hhldIndex[0]) for perIndex in persIndicesForActsForHhld: personObject = Person(perIndex[0], perIndex[1]) schedulesForPerson = data.data[perIndex[2]:perIndex[3],:] activityList = self.return_activity_list_for_person(schedulesForPerson) personObject.add_episodes(activityList) workStatus, schoolStatus, childDependency = self.return_status_dependency(schedulesForPerson) personObject.add_status_dependency(workStatus, schoolStatus, childDependency) householdObject.add_person(personObject) #print 'household object created in - %.4f' %(time.time()-ti) if self.specification.terminalEpisodesAllocation: householdObject.allocate_terminal_dependent_activities(seed) elif self.childDepProcessingType == 'Allocation': householdObject.allocate_dependent_activities(seed) elif self.childDepProcessingType == 'Resolution': householdObject.lineup_activities(seed) elif self.childDepProcessingType == 'Fix Trip Purpose': householdObject.fix_trippurpose(seed) elif self.childDepProcessingType == 'Extract Tour Attributes': householdObject.extract_tripattributes(seed) #raw_input('extract tour attributes') pass reconciledSchedules = householdObject._collate_results() actList += reconciledSchedules #print 'created and reconciled in - %.4f' %(time.time()-ti) return DataArray(actList, self.colNames)
def setUp(self): choice = ['SOV'] coefficients = [{'constant': 2, 'Var1': 2.11}] data = array([[1, 1.1], [1, -0.25], [1, 3.13], [1, -0.11]]) self.data = DataArray(data, ['Constant', 'VaR1']) self.specification = Specification(choice, coefficients)
def create_choiceset(self, shape, criterion, names): #TODO: Should setup a system to generate the choicesets dynamically #based on certain criterion # this choiceset creation criterion must be an attribute of the # SubModel class choiceset = ones(shape) return DataArray(choiceset, names)
def setUp(self): choices = ['Episodes1', 'Episodes2', 'Episodes3'] coefficients = [{'Constant':2, 'Var1':2.11}] data = array([[1, 1.1], [1, -0.25], [1, 3.13], [1, -0.11]]) self.data = DataArray(data, ['CONSTANT', 'VAR1']) self.specification = CountSpecification(choices, coefficients) self.specification1 = [choices, coefficients]
def setUp(self): choice = ['age_tt_product'] coefficients = [{'age': 1, 'tt': 1}] data = array([[1, 15], [2, 10]]) self.data = DataArray(data, ['Age', 'TT']) self.specification = Specification(choice, coefficients)
def calc_probabilities(self, data, choiceset): """ The method returns the selection probability associated with the the different choices. Inputs: data - DataArray object choiceset - DataArray object """ exp_expected_utilities = self.calc_exp_choice_utilities( data, choiceset) # missing_choices = choiceset.varnames #spec_dict = self.specification.specification for parent in self.parent_list: child_names = self.specification.child_names(parent) # calculating the sum of utilities across children in a branch util_sum = 0 for child in child_names: # For utils with missing values they are converted to zero # before summing the utils across choices in a parent # to avoid the case where missing + valid = missing child_column = exp_expected_utilities.column(child) child_column = child_column.filled(0) util_sum = util_sum + child_column # calculating the probability of children in a branch for child in child_names: exp_expected_utilities.setcolumn(child, exp_expected_utilities.column(child) / util_sum) # Dummy check to ensure that within any branch the probs add to one prob_sum = 0 for child in child_names: prob_sum = prob_sum + exp_expected_utilities.column(child) for choice in self.specification.actual_choices: parent_names = self.specification.all_parent_names(choice) for parent in parent_names: parent_column = exp_expected_utilities.column(parent) choice_column = exp_expected_utilities.column(choice) exp_expected_utilities.setcolumn(choice, choice_column * parent_column) self.specification.actual_choices.sort() rows = exp_expected_utilities.rows cols = len(self.specification.actual_choices) probabilities = DataArray(zeros((rows, cols)), self.specification.actual_choices, data.index) for choice in self.specification.actual_choices: probabilities.setcolumn( choice, exp_expected_utilities.column(choice)) return probabilities
def resolve_consistency(self, data, seed): verts = [] data.sort([ self.activityAttribs.hidName, self.activityAttribs.pidName, self.activityAttribs.scheduleidName ]) # Create Index Matrix self.create_indices(data) self.create_col_numbers(data._colnames) for hhldIndex in self.hhldIndicesOfPersons: firstPersonRec = hhldIndex[1] lastPersonRec = hhldIndex[2] firstPersonFirstAct = self.personIndicesOfActs[firstPersonRec, 2] lastPersonLastAct = self.personIndicesOfActs[lastPersonRec - 1, 3] schedulesForHhld = DataArray( data.data[firstPersonFirstAct:lastPersonLastAct, :], data.varnames) persIndicesForActsForHhld = self.personIndicesOfActs[ firstPersonRec:lastPersonRec, :] householdObject = Household(hhldIndex[0]) for perIndex in persIndicesForActsForHhld: personObject = Person(perIndex[0], perIndex[1]) schedulesForPerson = DataArray( data.data[perIndex[2]:perIndex[3], :], data.varnames) activityList = self.return_activity_list_for_person( schedulesForPerson) personObject.add_episodes(activityList) householdObject.add_person(personObject) hhldVerts = householdObject.retrieve_fixed_activity_vertices(seed) #for i in hhldVerts: # print i #raw_input() verts += hhldVerts return DataArray(verts, self.colNames)
def setUp(self): choices = ['SOV', 'HOV'] variables = [['Constant', 'Var1'], ['Constant']] coefficients = [{'Constant': 2, 'Var1': 2.11}, {'Constant': 1.2}] self.data = DataArray(array([[1, 1.1], [1, -0.25]]), ['Constant', 'Var1']) self.specification = Specification(choices, coefficients) self.model = Model(self.specification)
def setUp(self): choices = ['Veh1', 'Veh2', 'Veh3'] coefficients = [{'Constant': 2, 'Var1': 2.11}] thresholds = [1.2, 2.1] data = array([[1, 1.1], [1, -0.25], [1, 3.13], [1, -0.11]]) self.data = DataArray(data, ['CONSTANT', 'VAR1']) self.specification = OLSpecification(choices, coefficients, thresholds) self.specification1 = [choices, coefficients, thresholds]
def setUp(self): choice = ['DURATION'] coefficients = [{'constant':2, 'Var1':2.11}] data = array([[1, 1.1], [1, -0.25], [1, 3.13], [1, -0.11]]) variance = array([[1]]) self.data = DataArray(data, ['Constant', 'VaR1']) self.specification = Specification(choice, coefficients) self.errorspecification = LinearRegErrorSpecification(variance)
def update_houseids(self, hhldSyn, persSyn, hhldVars, persVars, highestHid): hhldSynDataObj = DataArray(hhldSyn, hhldVars) persSynDataObj = DataArray(persSyn, persVars) maxFreqCol = amax(hhldSynDataObj.columns(['frequency']).data) powFreqCol = floor(log(maxFreqCol, 10)) + 1 coefficients = {'frequency': 1, 'hhid': 10**powFreqCol} newHid = hhldSynDataObj.calculate_equation(coefficients) hhldSynDataObj.setcolumn('hhid', newHid) newHid = persSynDataObj.calculate_equation(coefficients) persSynDataObj.setcolumn('hhid', newHid) hhldSynDataObj.sort([self.idSpec.hidName]) persSynDataObj.sort([self.idSpec.hidName, self.idSpec.pidName]) hidIndex_popgenH = hhldVars.index('hhid') hidIndex_popgenP = persVars.index('hhid') self.create_indices(persSynDataObj) hhldSyn = hhldSynDataObj.data persSyn = persSynDataObj.data row = 0 for hhldIndex in self.hhldIndicesOfPersons: firstPersonRec = hhldIndex[1] lastPersonRec = hhldIndex[2] #print hhldIndex[0], highestHid + 1, firstPersonRec, lastPersonRec hhldSyn[row, hidIndex_popgenH] = highestHid + 1 persSyn[firstPersonRec:lastPersonRec, hidIndex_popgenP] = highestHid + 1 highestHid += 1 row += 1 return hhldSyn, persSyn
def setUp(self): data_ = zeros((5,8)) random.seed(1) data_[:,:4] = random.rand(5,4) data_ = DataArray(data_, ['Const', 'Var1', 'Var2', 'Var3', 'choice2_ind', 'choice1', 'choice2', 'choice3']) var_list = [('table1', 'Var1'), ('table1', 'Var2'), ('table1', 'Var3'), ('table1', 'Const')] variance = array([[1]]) choice1 = ['choice1'] choice2 = ['choice2'] choice3 = ['SOV', 'HOV'] coefficients1 = [{'const':2, 'Var1':2.11}] coefficients2 = [{'const':1.5, 'var2':-.2, 'var3':16.4}] coefficients3 = [{'Const':2, 'Var1':2.11}, {'Const':1.2}] specification1 = Specification(choice1, coefficients1) specification2 = Specification(choice2, coefficients2) specification3 = Specification(choice3, coefficients3) errorspecification = LinearRegErrorSpecification(variance) model1 = LinearRegressionModel(specification1, errorspecification) model2 = LinearRegressionModel(specification2, errorspecification) model3 = LogitChoiceModel(specification3) data_filter2 = DataFilter('choice2_ind', 'less than', 25, {'choice2_ind':1, 'choice2':1}) #Run Until Condition #Subset for the model condition data_filter1 = DataFilter('Const', 'less than', 0.3) model_seq1 = SubModel(model1, 'regression', 'choice1') model_seq2 = SubModel(model2, 'regression', 'choice2', data_filter=data_filter1, run_until_condition=data_filter2) model_seq3 = SubModel(model3, 'choice', 'choice3', run_until_condition=data_filter2) model_list = [model_seq1, model_seq2, model_seq3] # SPECIFY SEED TO REPLICATE RESULTS, DATA FILTER AND # RUN UNTIL CONDITION component = AbstractComponent('DummyComponent', model_list, var_list) component.run(data_) print component.data.data
def setUp(self): choice = ['Frontier'] coefficients = [{'constant': 2, 'Var1': 2.11}] data = array([[1, 1.1], [1, -0.25], [1, 3.13], [1, -0.11]]) variance = array([[1., 0], [0, 1.1]]) self.data = DataArray(data, ['Constant', 'VaR1']) self.specification = Specification(choice, coefficients) self.errorspecification = StochasticRegErrorSpecification( variance, 'start')
def testmodelresults(self): model = LogitChoiceModel(self.specification) choiceset = DataArray(array([]), []) probabilities_model = model.calc_probabilities(self.data, choiceset) probabilities_diff = all(self.prob_array_act == probabilities_model) self.assertEqual(True, probabilities_diff) selected_model = model.calc_chosenalternative(self.data) selected_diff = all(self.selected_act == selected_model) self.assertEqual(True, selected_diff)
def setUp(self): self.data = genfromtxt( "/home/kkonduri/simtravel/test/mag_zone/schedule_txt.csv", delimiter=",", dtype=int) colNames = [ 'houseid', 'personid', 'scheduleid', 'activitytype', 'locationid', 'starttime', 'endtime', 'duration' ] self.actSchedules = DataArray(self.data, colNames)
def setUp(self): self.data = genfromtxt( "/home/karthik/simtravel/test/mag_zone_dynamic/schedule_txt_small.csv", delimiter=",", dtype=int) colNames = [ 'scheduleid', 'houseid', 'personid', 'activitytype', 'locationid', 'starttime', 'endtime', 'duration', 'dependentpersonid' ] self.actSchedules = DataArray(self.data, colNames)
def resolve_consistency(self, data, seed, numberProcesses): pschedulesGrouped = data.data.groupby(level=[0, 1], sort=False) verts = df(columns=self.colNames) verts[self.hidName] = pschedulesGrouped[self.hidName].min() verts[self.pidName] = pschedulesGrouped[self.pidName].min() verts[self.starttimeName] = pschedulesGrouped[self.starttimeName].min() verts[self.endtimeName] = pschedulesGrouped[self.endtimeName].max() return DataArray(verts.values, self.colNames, indexCols=[self.hidName, self.pidName]) """
def calc_chosenalternative(self, data, choiceset=None, seed=1): """ The method returns the chosen alternaitve among the count choices for the choice variable under consideration Inputs: data - DataArray object """ probabilities = DataArray(self.calc_probabilities(data), self.specification.choices) prob_model = AbstractProbabilityModel(probabilities, seed) return prob_model.selected_choice()
def sample_choices(self, data, destLocSetInd, zoneLabels, count, sampleVarName, seed): destLocSetInd = destLocSetInd[:, 1:] #print 'number of choices - ', destLocSetInd.shape #raw_input() ti = time.time() for i in range(count): destLocSetIndSum = destLocSetInd.sum(-1) #print 'Number of choices', destLocSetIndSum probLocSet = (destLocSetInd.transpose() / destLocSetIndSum).transpose() zeroChoices = destLocSetIndSum.mask #print 'zero choices', zeroChoices if (~zeroChoices).sum() == 0: continue #probLocSet = probLocSet[zeroChoices,:] #print probLocSet.shape, len(zoneLabels), 'SHAPES -- <<' probDataArray = DataArray(probLocSet, zoneLabels) # seed is the count of the sampled destination starting with 1 probModel = AbstractProbabilityModel(probDataArray, self.projectSeed + seed + i) res = probModel.selected_choice() # Assigning the destination # We subtract -1 from the results that were returned because the # abstract probability model returns results indexed at 1 # actual location id = choice returned - 1 colName = '%s%s' % (sampleVarName, i + 1) nonZeroRows = where(res.data <> 0) #print 'SELECTED LOCATIONS FOR COUNT - ', i+1 #print res.data[:,0] #actualLocIds = res.data[nonZeroRows] #actualLocIds[nonZeroRows] -= 1 #print actualLocIds data.setcolumn(colName, res.data) #print data.columns([colName]).data[:,0] #raw_input() # Retrieving the row indices dataCol = data.columns([colName]).data rowIndices = array(xrange(dataCol.shape[0]), int) #rowIndices = 0 colIndices = res.data.astype(int) destLocSetInd.mask[rowIndices, colIndices - 1] = True
def setUp(self): choices = ['Veh1', 'Veh2', 'Veh3'] self.coefficients = [{'Constant': 2, 'Var1': 2.11}] self.thresholds = [1.2, 2.1] self.data = DataArray(array([[1, 1.1], [1, -0.25], [1, 3.13], [1, -0.11]]), ['CONSTANT', 'VAR1']) specification = OLSpecification( choices, self.coefficients, self.thresholds) self.model = OrderedModel(specification) specification1 = OLSpecification(choices, self.coefficients, self.thresholds, distribution='probit') self.model1 = OrderedModel(specification1)
def calc_predvalue(self, data, seed=1): """ The method returns evaluates the product for the different choices using the coefficients in the specification input as power. Inputs: data - DataArray object """ # raw_input() expected_value = self.calculate_expected_values(data) # print data.columns(['tt1']) # print expected_value return DataArray(expected_value.data, self.specification.choices)
def setUp(self): choices = ['SOV', 'HOV'] coefficients = [{'Constant': 2, 'Var1': 2.11}, {'Constant': 1.2}] data = array([[1, 1.1], [1, -0.25], [1, 3.13], [1, -0.11]]) variance = array([[1.1]]) variance1 = array([[1.1, 1.2], [2.1, 2.2]]) self.data = DataArray(data, ['Constant', 'VAR1']) self.specification = Specification(choices, coefficients) self.errorspecification = ErrorSpecification(variance, 'normal') self.errorspecification1 = ErrorSpecification(variance1, 'normal')
def returnTable(self, tableName, id_var, colNames): tableRef = self.returnTableReference(tableName) idVarColumn = tableRef.col(id_var) uniqueIds = unique(idVarColumn) table = zeros((max(uniqueIds) + 1, len(colNames))) for i in range(len(colNames)): table[uniqueIds, i] = tableRef.col(colNames[i]) #print table return DataArray(table, colNames), uniqueIds
def resolve_consistency(self, data, seed, numberProcesses): print "Number of splits - ", numberProcesses splits = split_df(data.data, houseidCol=self.hidName, workers=numberProcesses) args = [(split, seed, self.activityAttribs, self.dailyStatusAttribs, self.dependencyAttribs) for split in splits] resultList = [] resultList += resolve_by_multiprocessing( func=clean_aggregate_schedules, args=args, workers=numberProcesses) return DataArray(resultList, self.colNames) """
def calculate_expected_values(self, data): """ The method returns the product using the coefficients in the specification input as power. Inputs: data - DataArray object """ num_choices = self.specification.number_choices expected_value_array = DataArray(zeros((data.rows, num_choices)), self.choices) self.inverse = self.specification.inverse[0] for i in range(num_choices): coefficients = self.coefficients[i] expected_value_array.data[:,i] = data.calculate_product(coefficients, inverse=self.inverse) return expected_value_array
def calc_predvalue(self, data, seed=1): """ The method returns the predicted value for the different choices in the specification input. Inputs: data - DataArray object """ if seed == None: raise Exception, "linear" expected_value = self.calc_expected_value(data).data expValueZero = expected_value == 0 expected_value[expValueZero] = 1 pred_value = floor(log(expected_value)/log(self.error_specification.variance[0,0])) return DataArray(pred_value, self.specification.choices)
def selected_choice(self): """ The method returns the selected alternative for each agent. Inputs: None """ choice = zeros(self.num_agents) random_numbers = self.generate_random_numbers() self.prob_cumsum = self.cumprob().filled(-1) for i in range(self.num_choices): # Indicator for the zero cells in the choice array #indicator_zero_cells = ones(self.num_agents) indicator = array([True] * self.num_agents) zero_indices = choice == 0 #indicator_zero_cells[~zero_indices] = ma.masked indicator[~zero_indices] = False # Indicator for the cells where the random number # is less than the probability #indicator_less_cells = ones(self.num_agents) #indicator_less_cells = array([True]*self.num_agents) less_indices = random_numbers < self.prob_cumsum[:, i] #indicator_less_cells[~less_indices] = ma.masked #indicator_less_cells indicator[~less_indices] = False #indicator_less_zero_cells = indicator_zero_cells + indicator_less_cells #indicator_less_zero_cells = indicator_less_zero_cells == 2 choice[indicator] = i + 1 choice.shape = (self.num_agents, 1) #alt_text = [] #for i in choice: # alt_text.append(self.choices[int(i[0])-1]) #alt_text = array(alt_text) #alt_text.shape = (self.num_agents, 1) #return alt_text #print choice return DataArray(choice, ['selected choice'])
def calculate_expected_values(self, data): """ The method returns the expected values for the different choices using the coefficients in the specification input. Inputs: data - DataArray object """ num_choices = self.specification.number_choices expected_value_array = DataArray(zeros((data.rows, num_choices)), self.choices) for i in range(num_choices): coefficients = self.coefficients[i] expected_value_array.data[:, i] = data.calculate_equation( coefficients) return expected_value_array