Example #1
0
def inform(detailed=False):
    ''' Inform about current point of evaluation.
    '''
    # Antibugging.
    assert (detailed in [True, False])

    # Initialize.
    initObj = initCls()

    initObj.read()

    initObj.lock()

    initDict = initObj.getAttr('initDict')

    # Auxiliary objects.
    parasObj = initDict['PARAS']

    open('inform.struct.out', 'w').close()

    # Detailed information.
    if (detailed):

        simEconomy = simulate(update=True, observed=False, inform=True)

        obsEconomy = pkl.load(open(initDict['EST']['file']))

        _writeDescriptive(simEconomy, obsEconomy)

    # Parametrization.
    if (os.path.exists('stepInfo.struct.out')):

        parasObj.update(readStep('paras'), 'internal', 'all')

    _writeParameters(parasObj, initDict)
Example #2
0
def testA():
    ''' Testing if a the evaluation results are the same regardless if the
        toolbox was compiled either normal or fast.
    '''
    # Constraints.
    _randominit.generateInitFile()

    # Simulation.
    simulate()

    # Perturb
    fval, scale = None, np.random.uniform(-0.5, 0.5)

    perturb(scale = scale, seed = 123)

    # Evaluation of function


    _auxiliary.compileToolbox()

    # Estimate
    estimate(useSimulaton = True)

    # Evaluate success.
    rslt = readStep('fval')

    if(fval is None): fval = rslt

    err = (np.allclose(rslt, fval) != True)

    assert (err == False)
Example #3
0
    def test_case_1(self):

        shutil.copy('../dat/testB.ini', 'model.struct.ini')

        shutil.copy('../dat/optimizers.ini', 'optimizers.struct.ini')

        simulate()
        ''' Process initialization file.
        '''
        initObj = initCls()

        initObj.read()

        initObj.lock()
        ''' Distribute information.
        '''
        obsEconomy = pkl.load(open('simEconomy.struct.pkl', 'rb'))

        initDict = initObj.getAttr('initDict')

        optimization = initDict['OPT']

        parasObj = initDict['PARAS']

        derived = initDict['DERIV']

        fval = None

        for static in [True, False]:

            derived['static'] = static

            requestObj = requestCls()

            requestObj.setAttr('parasObj', parasObj)

            requestObj.setAttr('obsEconomy', obsEconomy)

            requestObj.setAttr('derived', derived)

            requestObj.setAttr('single', True)

            requestObj.setAttr('optimization', optimization)

            requestObj.lock()
            ''' Call optimization.
            '''
            optimize(requestObj)
            ''' Check results.
            '''
            rslt = readStep('fval')

            if (fval is None): fval = rslt

            assert_true(np.allclose(fval, -165.628510699) == True)
Example #4
0
    def _process(self, names):
        ''' Process multiple logging files.
        '''
        # Antibugging.
        assert (self.getStatus() == True)
        assert (isinstance(names, list))

        # Distribute class attributes.
        count = self.attr['count']
        
        # Process files.
        numActive = 0
        
        for name in names:

            stepFval = self.attr['stepFval']
            
            # Count active processes.
            if(os.path.exists(name + '/.optimization.struct.out')):
            
                numActive = numActive + 1
            
            try:
                
                dict_ = pkl.load(open(name + '/stepInfo.struct.pkl', 'r')) 
                
                fval, vals = dict_['fval'], dict_['vals']
                
            except:
                        
                continue
                
            # Determine event.  
            isStep = (fval < stepFval)
                
            if(not isStep): continue
                
            # Check.
            os.chdir(name)
            
            assert (len(readStep('paras') == len(vals)))
            
            os.chdir('../')

            # Update logging.
            writeStep(vals, fval, count, pkl_ = False)
  
            # Update attributes.        
            self.attr['stepFval'] = fval
            
            self.attr['count']    = count + 1
        
        return numActive
Example #5
0
def perturb(scale = 0.1, seed = 123, update = False):
    ''' Perturb current values of structural parameters.
    '''
    ''' Obtain starting values.
    '''
    initObj = initCls()
        
    initObj.read()
        
    initObj.lock()
    
    ''' Distribute attributes.
    '''
    initDict = initObj.getAttr('initDict')
    
    parasObj = initDict['PARAS']
    
    ''' Update parameter object.
    '''
    if(update):

        # Antibugging.
        assert (os.path.isfile('stepInfo.struct.out'))
        
        values = readStep('paras')
        
        # Update parameter objects.
        parasObj.update(values, 'internal', 'all')
        
    ''' Perturb external values.
    '''
    np.random.seed(seed)
    
    baseValues = parasObj.getValues('external', 'free')
    
    perturb    = (np.random.sample(len(baseValues)) - 0.5)*scale
    
    evalPoints = baseValues + perturb
    
    ''' Transform evaluation points.
    '''
    parasObj.update(evalPoints, 'external', 'free')
    
    evalPoints = parasObj.getValues('internal', 'all')
    
    ''' Finishing.
    '''
    writeStep(evalPoints, fval = '---', count = 0)
Example #6
0
def _checkQuality(identifiers):
    ''' Check quality of new material.
    '''
    # Checks.
    initObj = initCls()
    
    initObj.read()
    
    initObj.lock()
    
    
    initDict = initObj.getAttr('initDict')
    
    parasObj = initDict['PARAS']

    if(os.path.exists('stepInfo.struct.out')):
        
        parasObj.update(readStep('paras'), 'internal', 'all')

    assert (all(identifier in range(parasObj.getAttr('numParas')) \
                for identifier in identifiers))
Example #7
0
def estimate(resume = False, single = False, static = False):
    ''' Run estimation. 
    '''
    # Antibugging.
    assert (os.path.exists('model.struct.ini'))
    assert (resume in [True, False])
    assert (static in [True, False])
    
    cleanup(resume)
    
    ''' Process initialization file.
    '''
    initObj = initCls()
    
    initObj.read()
    
    initObj.lock()
    
    
    ''' Distribute information.
    '''
    initDict = initObj.getAttr('initDict')
    
    
    optimization = initDict['OPT']
    
    estimation   = initDict['EST']
    
    parasObj     = initDict['PARAS']

    derived      = initDict['DERIV']


    file_        = initDict['EST']['file']
    
    if(static): derived['static'] = True
    
    ''' Load dataset.
    '''
    obsEconomy = pkl.load(open(file_, 'rb'))
    
    
    ''' Subset.
    '''
    numSubset = estimation['agents']
   
    obsEconomy.subset(numSubset)
    
    
    ''' Update.
    '''
    if(resume): 
        
        parasObj.update(readStep('paras'), 'internal', 'all')
    
    
    ''' Construct request.
    ''' 
    requestObj = requestCls()

    requestObj.setAttr('optimization', optimization)

    requestObj.setAttr('obsEconomy', obsEconomy)
    
    requestObj.setAttr('parasObj', parasObj)
    
    requestObj.setAttr('derived', derived)
    
    requestObj.setAttr('single', single)

    requestObj.lock()
    
    ''' Call optimization.
    '''
    optimize(requestObj)
Example #8
0
def modify(action, identifiers, values = None):
    ''' Modify parameters.
    '''                                          
    # Antibugging.
    assert (action in ['free', 'fix', 'change'])
    assert (isinstance(identifiers, list))

    # Auxiliary objects.
    str_ = '\t {0:<8}{1:<8}\t{2:<8}\n'
    
    for i, id_ in enumerate(identifiers):
        
        fin, fout = open('model.struct.ini'), open('.model.struct.ini', 'wt')
    
        if(action == 'change'): value = values[i]
                        
                        
        count = -1
        
        for line in fin:
            
            # Process line.
            currentLine = shlex.split(line)
        
            # Check for parameters.
            isPara, type_, pos, info = _processCases(currentLine)
                    
            if(isPara): count = count + 1    
            
            # Check for relevance
            isRelevant = ((count == id_) and (isPara))

            if(not isRelevant):
                
                fout.write(line)
                        
            else:

                if(action in ['free','fix']):
                 
                    assert (type_ not in ['subsidy', 'cost', 'discount'])
                    
                    info = _statusChange(action, info)

                elif(action in ['change']):
                    
                    info = _valueChange(action, info, value)
                                
                fout.write(str_.format(type_, pos, info))
        
                ''' Update stepInfo.log 
                '''
                if(os.path.exists('stepInfo.struct.out')):
                
                    updatedValue = info.replace('!', '')
                
                    new = readStep('paras')
                    
                    new[id_] = float(updatedValue)
                    
                    writeStep(new)
                                          
        fin.close(); fout.close()
                
        shutil.move('.model.struct.ini', 'model.struct.ini')                            
        
    # Check quality.
    _checkQuality(identifiers)
Example #9
0
def simulate(update=False, observed=False, inform=False, source=None):
    ''' Simulation of agent population.
    '''
    # Antibugging.
    assert (os.path.exists('model.struct.ini'))
    assert (observed in [True, False])
    assert (update in [True, False])
    assert (inform in [True, False])
    ''' Process initialization file.
    '''
    initObj = initCls()

    initObj.read()

    initObj.lock()
    ''' Distribute information.
    '''
    initDict = initObj.getAttr('initDict')

    numPeriods = initDict['SIM']['periods']

    numAgents = initDict['SIM']['agents']

    seed = initDict['SIM']['seed']

    allPos = initDict['DERIV']['pos']

    parasObj = initDict['PARAS']

    treeObj = initDict['TREE']
    ''' Update parameter object.
    '''
    if (update):

        x = readStep('paras')

        parasObj.update(x, 'internal', 'all')
    ''' Simulate agent attributes.
    '''
    np.random.seed(seed)

    attrs = []

    if (source is not None):

        sourceAgents = _getSources(source, numAgents)

        for i in range(numAgents):

            attr = {}

            for key_ in ['experience', 'children', 'utility', \
                             'spouse', 'wage']:

                attr[key_] = sourceAgents[i].getAttr('attr')[key_]

            attrs = attrs + [attr]

    else:

        for _ in range(numAgents):

            attr, values = {}, {}

            for t in range(numPeriods):

                values[t] = {}

                for j in allPos:

                    values[t][j] = np.random.rand()

            # Spouse.
            attr['spouse'] = _constructSpouse(numPeriods)

            # Children.
            pos = initDict['UTILITY']['child']['pos']

            attr['children'] = _constructChildren(numPeriods)

            # Experience.
            pos = initDict['WAGE']['exper']['pos']

            attr['experience'] = [0.0]

            # Utility.
            attr['utility'] = []

            list_ = initDict['UTILITY']['coeffs']['pos']

            for t in range(numPeriods):

                cand = []

                for pos in list_:

                    cand = cand + [values[t][pos]]

                attr['utility'] = attr['utility'] + [cand]

            # Wage.
            attr['wage'] = []

            list_ = initDict['WAGE']['coeffs']['pos']

            for t in range(numPeriods):

                cand = []

                for pos in list_:

                    cand = cand + [values[t][pos]]

                attr['wage'] = attr['wage'] + [cand]

            attrs = attrs + [attr]

    # Initialize agents.
    agentObjs = []

    for attr in attrs:

        agentObj = agentCls()

        agentObj.setAttr('attr', attr)

        agentObj.setAttr('parasObj', parasObj)

        agentObj.setAttr('treeObj', treeObj)

        agentObj.lock()

        # Collect agents.
        agentObjs = agentObjs + [agentObj]
    ''' Initialize economy.
    '''
    economyObj = economyCls()

    economyObj.setAttr('parasObj', parasObj)

    economyObj.setAttr('agentObjs', agentObjs)

    economyObj.lock()
    ''' Simulate economy.
    '''
    for _ in range(numPeriods):

        economyObj.simulate()
    ''' Storage.
    '''
    if (inform):

        return economyObj

    else:

        economyObj.store('simEconomy.struct.pkl')

        _writeInfo(economyObj, parasObj)

        if (observed): _writeData(economyObj, initDict)