Exemplo n.º 1
0
def run():
    # # A 3.1
    # ## Frequency of team meetings
    t0 = timer.time()
    p = Params()

    p.nAgents = 20
    p.nTeams = 4
    p.nDims = 20
    p.agentTeams = m.specializedTeams(p.nAgents, p.nTeams)
    p.teamDims = m.teamDimensions(p.nDims, p.nTeams)  #np.ones([nTeams,nDims])

    p.curatedTeams = True

    varyMeetingTimes = [int(p.steps / i) for i in range(1, 10)]
    varyMeetingTimes.append(100000)

    meetingStartTimes = []
    for meetingTimes in varyMeetingTimes:
        p.meetingTimes = meetingTimes
        print("meeting interval: " + str(meetingTimes))
        if __name__ == '__main__' or 'kaboom.designScienceStudies.iii_teamMeetings':
            pool = multiprocessing.Pool(processes=4)
            allTeams = pool.starmap(teamWorkProcess,
                                    zip(range(p.reps), itertools.repeat(p)))
            pool.close()
            pool.join()
            print('next')
        meetingStartTimes.append(allTeams)
    print("time to complete: " + str(timer.time() - t0))

    allTeams = [t for tt in meetingStartTimes for t in tt]
    #directory = m.saveResults(allTeams,'numberOfTeamMeetings')

    teamScores = [t.getBestScore() for t in allTeams]
    teamScoresGrouped = [[t.getBestScore() for t in tt]
                         for tt in meetingStartTimes]

    nMeetings = [t.nTeamMeetings for tt in meetingStartTimes for t in tt]

    perf = np.array(teamScores) * -1
    plt.scatter(nMeetings, perf, c=[.9, .9, .9])
    means = m.plotCategoricalMeans(nMeetings, perf)
    plt.xlabel("number of team meetings")
    plt.ylabel("performance")

    slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(
        nMeetings, teamScores)
    x = np.linspace(0, 10, 10)
    plt.plot(x, (x * slope + intercept) * -1)
    myPath = os.path.dirname(__file__)
    plt.savefig(myPath + "/results/iii_team_meetings_" + str(timer.time()) +
                ".pdf")
def run():

    # ## B 2.3
    # Robustness on two diagonals
    #

    # PROBLEM SET 1
    #problems favor a range of styles from adaptive to mid to innovative

    t0 = timer.time()
    p = Params()

    p.curatedTeams = True

    p.reps = 32

    allTeams = []
    allTeamScores = []
    p.aiScore = 100
    aiRanges = np.linspace(0, 100, 6)

    #    pComm = 0.2

    #the diagonal across preferred style:
    roughnesses = np.logspace(-1, .7, num=6, base=10)  #[.2,.6,1.8,5.4]
    roughnesses = roughnesses[1:]
    speeds = np.logspace(-1, .7, num=6, base=10) / 100  # [.001,.004,.016,.048]
    speeds = speeds[1:]

    problemSet = [roughnesses, speeds]

    for aiRange in aiRanges:
        p.aiRange = aiRange
        teams = []
        teamScores = []
        for i in range(p.reps):
            scores, t = robustnessTest(p, problemSet)
            teams.append(t)
            teamScores.append(scores)
        allTeams.append(teams)
        allTeamScores.append(teamScores)
        print('next')
    print('time: %s' % (timer.time() - t0))

    #
    #teams = [ t for tt in allTeams for t in tt]
    #directory = saveResults(teams,'robustnessTest_diag1')
    #rFile = directory+'/'+'scoreMatrix.obj'
    #rPickle = open(rFile, 'wb')
    #pickle.dump(allTeamScores,rPickle)
    # f = open('/Users/samlapp/SAE_ABM/results/1542634807.0205839robustnessTest/scoreMatrix.obj','rb')
    # sm = pickle.load(f)

    # In[67]:

    #STADARDIZE for each problem!
    ats = np.array(allTeamScores)
    problemMeans = [np.mean(ats[:, :, i]) for i in range(len(problemSet[0]))]
    problemSds = [np.std(ats[:, :, i]) for i in range(len(problemSet[0]))]
    allTeamScoresStandardized = ats
    for j in range(len(allTeamScoresStandardized)):
        for i in range(len(problemSet[0])):
            for k in range(p.reps):
                allTeamScoresStandardized[
                    j, k, i] = (ats[j, k, i] - problemMeans[i]) / problemSds[i]
    np.shape(allTeamScoresStandardized)

    meanScores = [
        np.mean(t) for teamSet in allTeamScoresStandardized for t in teamSet
    ]
    meanGrouped = [[np.mean(t) for t in teamSet]
                   for teamSet in allTeamScoresStandardized]
    sdScores = [
        np.std(t) for teamSet in allTeamScoresStandardized for t in teamSet
    ]
    sdGrouped = [[np.std(t) for t in teamSet]
                 for teamSet in allTeamScoresStandardized]
    ranges = [t.dAI for teamSet in allTeams for t in teamSet]
    robustness = np.array(meanScores) - np.array(sdScores)
    robustnessGrouped = np.array(meanGrouped) - np.array(sdGrouped)

    # meanScores = np.array(meanScores)*-1

    plt.scatter(ranges, np.array(meanScores), c=[.9, .9, .9])
    # plt.title("9 problem matrix")

    cms = m.plotCategoricalMeans(ranges, meanScores)
    #plt.savefig('results/vii_set1_robustnessInv.pdf')

    stat, pscore = scipy.stats.ttest_ind(meanGrouped[0], meanGrouped[2])
    print("significance: p= " + str(pscore))
    corr, _ = scipy.stats.pearsonr(ranges[0:p.reps * 4],
                                   meanScores[0:p.reps * 4])
    print('Pearsons correlation: %.3f' % corr)

    # In[2]
    # PROBLEM SET 2
    # Now the other diagonal (all 5 problems prefer mid-range style)

    allTeamsD2 = []
    allTeamScoresD2 = []
    #    aiScore = 100
    aiRanges = np.linspace(0, 100, 6)

    #the diagonal across preferred style:
    roughnesses = np.logspace(-1, .7, num=6, base=10)  #[.2,.6,1.8,5.4]
    roughnesses = roughnesses[1:]
    speeds = np.logspace(-1, .7, num=6, base=10) / 100  # [.001,.004,.016,.048]
    speeds = speeds[1:]
    #reverse the order: pair large speed (small space) with small roughness
    speeds = speeds[::-1]

    problemSet = [roughnesses, speeds]

    for aiRange in aiRanges:
        p.aiRange = aiRange
        teams = []
        teamScores = []
        for i in range(p.reps):
            scores, t = robustnessTest(p, problemSet)
            teams.append(t)
            teamScores.append(scores)
        allTeamsD2.append(teams)
        allTeamScoresD2.append(teamScores)
        print('next')
    print('time: %s' % (timer.time() - t0))

    #teams = [ t for tt in allTeamsD2 for t in tt]
    #directory = saveResults(teams,'robustnessTest_diag2')
    #rFile = directory+'/'+'scoreMatrix.obj'
    #rPickle = open(rFile, 'wb')
    #pickle.dump(allTeamScores,rPickle)
    # f = open('/Users/samlapp/SAE_ABM/results/1542634807.0205839robustnessTest/scoreMatrix.obj','rb')
    # sm = pickle.load(f)

    #STADARDIZE for each problem!
    ats = np.array(allTeamScoresD2)
    problemMeans = [np.mean(ats[:, :, i]) for i in range(len(problemSet[0]))]
    problemSds = [np.std(ats[:, :, i]) for i in range(len(problemSet[0]))]
    allTeamScoresStandardized = ats
    for j in range(len(allTeamScoresStandardized)):
        for i in range(len(problemSet[0])):
            for k in range(p.reps):
                allTeamScoresStandardized[
                    j, k, i] = (ats[j, k, i] - problemMeans[i]) / problemSds[i]
    np.shape(allTeamScoresStandardized)

    meanScoresD2 = [
        np.mean(t) for teamSet in allTeamScoresStandardized for t in teamSet
    ]
    meanGroupedD2 = [[np.mean(t) for t in teamSet]
                     for teamSet in allTeamScoresStandardized]
    sdScoresD2 = [
        np.std(t) for teamSet in allTeamScoresStandardized for t in teamSet
    ]
    sdGroupedD2 = [[np.std(t) for t in teamSet]
                   for teamSet in allTeamScoresStandardized]
    ranges = [t.dAI for teamSet in allTeams for t in teamSet]
    # robustness = np.array(meanScores)-np.array(sdScores)
    # robustnessGrouped = np.array(meanGrouped)-np.array(sdGrouped)

    #flip the scores
    meanScoresD2 = np.array(meanScoresD2) * -1

    plt.scatter(ranges, np.array(meanScoresD2), c=[.9, .9, .9])

    cms = m.plotCategoricalMeans(ranges, np.array(meanScoresD2))

    stat, p = scipy.stats.ttest_ind(meanGroupedD2[0], meanGroupedD2[3])
    print("significance: p= " + str(p))
    corr, _ = scipy.stats.pearsonr(ranges[0:p.reps * 4],
                                   meanScoresD2[0:p.reps * 4])
    print('Pearsons correlation: %.3f' % corr)

    # plt.scatter(ranges,np.array(meanScoresD2),c=[.9,.9,.9])

    cms = m.plotCategoricalMeans(ranges, np.array(meanScoresD2))
    cms2 = m.plotCategoricalMeans(ranges, np.array(meanScores))
    plt.xlabel('maximum cognitive gap (style diversity)')
    plt.ylabel('performance')
    plt.legend(['probem set 1', 'problem set 2'])

    myPath = os.path.dirname(__file__)
    plt.savefig(myPath + "/results/vii_diverseTeams_2problemsets.pdf")
Exemplo n.º 3
0
#change team size and specialization
p.nAgents = 16  #32
p.nDims = 56
p.steps = 3  #00
p.reps = 4

nAgentsPerTeam = [1, 2, 3, 4]  #,8,16,32]#[32,16]# [8,4,3,2,1]

resultMatrix = []
teamObjects = []
for i in range(3):  #range(3):
    if i == 0:  #homogeneous
        p.aiRange = 0
        p.aiScore = 95
        p.curatedTeams = True
    elif i == 1:
        p.aiRange = 70
        p.aiScore = 95
        p.curatedTeams = False
    elif i == 2:
        p.aiScore = None
        p.aiRange = None
        p.curatedTeams = False
    scoresA = []
    teams = []
    for subteamSize in nAgentsPerTeam:
        p.nTeams = int(p.nAgents / subteamSize)
        p.agentTeams = m.specializedTeams(p.nAgents, p.nTeams)
        p.teamDims = m.teamDimensions(p.nDims, p.nTeams)
        if __name__ == '__main__':  # or 'kaboom.designScienceStudies.viii_specialization_composition':
Exemplo n.º 4
0
def run():

    # A 1.6 composition vs commRate

    p = Params()

    p.nAgents = 20
    p.nTeams = 4
    p.nDims = 20
    p.agentTeams = m.specializedTeams(p.nAgents, p.nTeams)
    p.teamDims = m.teamDimensions(p.nDims, p.nTeams)  #np.ones([nTeams,nDims])
    #p.reps=1
    pComms = np.linspace(0, 1, 11)

    p.aiScore = 95
    #meetingTimes = 100
    t0 = timer.time()

    resultsA16 = []
    for i in range(3):
        if i == 0:  #homogeneous
            p.aiScore = 95
            p.aiRange = 0
            p.curatedTeams = False
        elif i == 1:  #hetero70
            p.aiScore = 95
            p.aiRange = 70
            p.curatedTeams = True
        elif i == 2:  #organic
            p.aiScore = None
            p.aiRange = None
            p.curatedTeams = False

        allTeamObjects = []
        for pComm in pComms:
            p.pComm = pComm
            if __name__ == '__main__' or 'kaboom.designScienceStudies.ix_composition_structure':
                pool = multiprocessing.Pool(processes=4)
                allTeams = pool.starmap(
                    teamWorkProcess, zip(range(p.reps), itertools.repeat(p)))
                print('next. time: ' + str(timer.time() - t0))
                for team in allTeams:
                    allTeamObjects.append(team)

                pool.close()
                pool.join()
        resultsA16.append(allTeamObjects)
        scores = [t.getBestScore() for t in allTeamObjects]
        pcs = [pc for pc in pComms for i in range(p.reps)]
        m.plotCategoricalMeans(pcs, np.array(scores) * -1)
        plt.show()
        # allTeams = [t for tl in allTeamObjects for t in tl]
    print("time to complete: " + str(timer.time() - t0))

    comps = ['homogeneous', 'heterogeneous70', 'organic']
    for i in range(3):
        allTeamObjects = resultsA16[i]

        allScores = np.array([t.getBestScore() for t in allTeamObjects]) * -1

        nS = [t.nMeetings for t in allTeamObjects]
        #     plt.scatter(nS,allScores, c=[.9,.9,.9])
        pC = [pc for pc in pComms for i in range(p.reps)]
        #     plt.show()
        #     plt.scatter(pC,allScores, label=kai)
        c = m.plotCategoricalMeans(pC, allScores)

    #    name="A1.6_commRate_vStyle_"+comps[i]
    #     directory = saveResults(allTeamObjects,name)
    plt.legend(comps)
    myPath = os.path.dirname(__file__)
    plt.savefig(myPath + "/results/ix_composition_specialization.pdf")
def run():
    t0 = timer.time()
    p=Params()

    p.nAgents = 32
    nAgentsPerTeam = [1,2,3,4,8,16,32]#[32,16]# [8,4,3,2,1]
    p.nDims = 32

    p.reps = 32


    #choose one problem (middle, favors mid-range)
    roughnesses = np.logspace(-1,.7,num=6,base=10)
    speeds = np.logspace(-1,.7,num=6,base=10) / 100
    p.amplitude = roughnesses[3]
    p.AVG_SPEED = speeds[3]


    resultMatrix = []
    teamObjects = []
    for i in range(3):
        if i == 0: #homogeneous
            p.aiRange = 0
            p.aiScore = 95
            p.curatedTeams = True
        elif i == 1:
            p.aiRange = 70
            p.aiScore = 95
            p.curatedTeams = False
        elif i == 2:
            p.aiScore = None
            p.aiRange = None
            p.curatedTeams = False
        scoresA = []
        teams = []
        for subteamSize in nAgentsPerTeam:
            p.nTeams = int(p.nAgents/subteamSize)
            p.agentTeams = m.specializedTeams(p.nAgents,p.nTeams)
            p.teamDims = m.teamDimensions(p.nDims,p.nTeams)
            if __name__ == '__main__' or 'kaboom.designScienceStudies.viii_specialization_composition':
                pool = multiprocessing.Pool(processes = 4)
                allTeams = pool.starmap(teamWorkProcess, zip(range(p.reps),itertools.repeat(p)))
                scoresA.append([t.getBestScore() for t in allTeams])
                teams.append(allTeams)
            pool.close()
            pool.join()
        resultMatrix.append(scoresA)
        teamObjects.append(teams)
        print("completed one")
    print("time to complete: "+str(timer.time()-t0))

    for i in range(3):
        nAgents = [len(team.agents) for teamSet in teamObjects[i] for team in teamSet]
        nTeams = [len(team.specializations) for teamSet in teamObjects[i] for team in teamSet]
        subTeamSize = [int(len(team.agents)/len(team.specializations)) for teamSet in teamObjects[i] for team in teamSet]
        teamScore =  [team.getBestScore() for teamSet in teamObjects[i] for team in teamSet]
        print("Diverse team, size %s in %s dim space: " % (32,p.nDims))
    #     plt.scatter(subTeamSize,teamScore,label='team size: '+str(teamSizes[i]))
        m.plotCategoricalMeans(subTeamSize,np.array(teamScore)*-1)

    plt.xlabel("subteam size")
    #     plt.xticks([1,4,8,16,32])
    plt.ylabel("performance")
    #     plt.show()
    plt.legend(['homogeneous','heterogeneous70','organic'])

    plt.title("composition vs structure")
    myPath = os.path.dirname(__file__)
    plt.savefig(myPath+"/results/viii_structure_composition.pdf")
Exemplo n.º 6
0
def run():
    t0 = timer.time()
    p = Params()
    p.curatedTeams = True

    #    pComm = 0.2 # np.linspace(0,.5,10)

    aiScores = np.linspace(60, 140, 7)
    p.aiRange = 0

    #pick 5 logarithmically spaced values of roughness (amplitude) and speed
    roughnesses = np.logspace(-1, .7, num=6, base=10)
    roughnesses = roughnesses[1:]
    speeds = np.logspace(-1, .7, num=6, base=10) / 100
    speeds = speeds[1:]

    count = 1
    roughness_ai_speed_matrix = []
    for i in range(len(roughnesses)):
        p.amplitude = roughnesses[i]  #,8,16,32,64]:
        ai_speed_matrix = []

        for j in range(len(speeds)):
            p.AVG_SPEED = speeds[j]
            scoresForAI = []
            teams = []
            for aiScore in aiScores:
                p.aiScore = aiScore
                if __name__ == '__main__' or 'kaboom.designScienceStudies.vi_problem_matrix':
                    pool = multiprocessing.Pool(processes=4)
                    allTeams = pool.starmap(
                        teamWorkProcess, zip(range(p.reps),
                                             itertools.repeat(p)))
                    scoresForAI.append([t.getBestScore() for t in allTeams])
                    for t in allTeams:
                        teams.append(t)
                    pool.close()
                    pool.join()
            ai_speed_matrix.append(scoresForAI)

            print("completed %s" % count)
            count += 1

        roughness_ai_speed_matrix.append(ai_speed_matrix)

    print("time to complete: " + str(timer.time() - t0))

    # In[416]:
    #
    #f = open('/Users/samlapp/SAE_ABM/results/B1_matrix_of_problems/ProblemMatrixScores.obj','rb')
    #roughness_ai_speed_matrix = pickle.load(f)
    #np.shape(roughness_ai_speed_matrix)
    #aiScores = np.linspace(60,140,7)
    #aiRanges = np.linspace(0,50,5)
    #reps = 8

    # In[420]:

    index = 0
    for i in range(len(roughness_ai_speed_matrix)):
        r = roughness_ai_speed_matrix[i]
        for j in range(len(r)):
            index += 1
            plt.subplot(len(roughness_ai_speed_matrix), len(r), index)

            s = r[j]
            allScores = [sc for row in s for sc in row]
            allScores = np.array(allScores) * -1
            myKai = [ai for ai in aiScores for i in range(p.reps)]
            #         plt.scatter(myKai,allScores,c=[.9,.9,.9])
            #         plotCategoricalMeans(myKai,allScores)

            pl = np.polyfit(myKai, allScores, 2)
            z = np.poly1d(pl)
            x1 = np.linspace(min(myKai), max(myKai), 100)
            plt.plot(x1, z(x1), color='red')
    #         print(roughnesses[i])
    #         plt.title("roughness %s speed %s" % (roughnesses[i], speeds[i]))
    #         plt.show()
    #         break
    plt.savefig('./results/vi_problemMatrixDetail' + str(timer.time()) +
                '.pdf')

    # In[29]:

    index = 0

    bestScoreImg = np.zeros([len(roughnesses), len(speeds)])
    for i in range(len(roughness_ai_speed_matrix)):
        r = roughness_ai_speed_matrix[i]
        for j in range(len(r)):
            index += 1

            s = r[j]
            allScores = [sc for row in s for sc in row]
            myKai = [ai for ai in aiScores for i in range(p.reps)]
            #         plt.scatter(myKai,allScores,c=[.9,.9,.9])
            #         plotCategoricalMeans(myKai,allScores)

            pl = np.polyfit(myKai, allScores, 2)
            z = np.poly1d(pl)
            x1 = np.linspace(min(myKai), max(myKai), 100)
            y = z(x1)
            bestScore = round(x1[np.argmin(y)])
            bestScoreImg[i, j] = bestScore
    #         plt.plot(x1,z(x1),color='red')
    #         plt.title("roughness %s speed %s" % (roughnesses[i], speeds[i]))
    #         plt.show()
    #         break

    plt.subplot(1, 1, 1)
    cmapRB = matplotlib.colors.LinearSegmentedColormap.from_list(
        "", ["blue", "red"])
    plt.imshow(bestScoreImg, cmap=cmapRB)
    plt.xlabel('decreasing solution space size')
    plt.ylabel('decreasing amplitude of objective function sinusoid')

    plt.colorbar()
    myPath = os.path.dirname(__file__)
    plt.savefig(myPath + "/results/vi_matrixOfProblems_bestStyle.pdf")