博弈优化找到极小节点覆盖(智能控制3.2)

博弈优化找到极小节点覆盖(智能控制3.2)_第1张图片
博弈论2-1.png
博弈优化找到极小节点覆盖(智能控制3.2)_第2张图片
博弈论2-2.png
博弈优化找到极小节点覆盖(智能控制3.2)_第3张图片
博弈论2-3.png
博弈优化找到极小节点覆盖(智能控制3.2)_第4张图片
博弈论2-4.png
博弈优化找到极小节点覆盖(智能控制3.2)_第5张图片
博弈论2-5.png

main.py

import random

# 收益矩阵
r=0.02
interest={
    'C':{'C':(1,1),'D':(1-r,1+r)},
    'D':{'C':(1+r,1-r),'D':(0,0)}
}
# 结点数据结构
class Node(object):
    def __init__(self):
        if (random.random()<0.5):
            self.state='C'
        else:
            self.state='D'
        self.value=0
        self.all_value=0
        self.neighbour_number=0
        self.nb=list()
        pass
    pass

class Network(object):
    
    def __init__(self,n):
        self.numbers=n
        self.nodes=list()
        self.edges=list()
        self.generateNode()


    # 生成n个结点
    def generateNode(self):
        for i in range(self.numbers):
            tmp_node = Node()
            self.nodes.append(tmp_node)
    def addEgde(self,es):
        for e in es:
            self.edges.append(e)
    # 保存结点的邻居
    def saveNb(self):
        for a, b in self.edges:
            a.nb.append(self.nodes.index(b))
            b.nb.append(self.nodes.index(a))
        for i in range(self.numbers):
            self.nodes[i].neighbour_number=len(self.nodes[i].nb)
    # 计算每个结点的平均收益
    def calValue(self):
        for i in range(self.numbers):
            self.nodes[i].all_value=0
        for a, b in self.edges:
            a.all_value+=interest[a.state][b.state][0]
            b.all_value+=interest[a.state][b.state][1]
        for i in range(self.numbers):
            self.nodes[i].value=self.nodes[i].all_value/self.nodes[i].neighbour_number
    # 最优邻居法更新状态
    # def updateState(self):
    #     for i in range(self.numbers):
    #         max_value=self.nodes[i].value
    #         for j in range(self.nodes[i].neighbour_number):
    #             if(self.nodes[self.nodes[i].nb[j]].value>max_value):
    #                 self.nodes[i].state=self.nodes[self.nodes[i].nb[j]].state
    #                 max_value=self.nodes[self.nodes[i].nb[j]].value
    # 逐项扫描反转试探法更新状态
    def updateState(self):
        for i in range(self.numbers):
            if(self.nodes[i].state=='C'):
                reward1=self.getReward(i)
                self.nodes[i].state='D'
                reward2=self.getReward(i)
                if(reward2<=reward1):
                    self.nodes[i].state = 'C'
            elif(self.nodes[i].state=='D'):
                reward1=self.getReward(i)
                self.nodes[i].state='C'
                reward2=self.getReward(i)
                if(reward2<=reward1):
                    self.nodes[i].state = 'D'
    # 计算单个结点的收益
    def getReward(self,i):
        all_value=0
        for s in  self.nodes[i].nb:
            all_value+=interest[self.nodes[i].state][self.nodes[s].state][0]
        value = all_value / self.nodes[i].neighbour_number
        return value
    # 获得总收益
    def getAllReward(self):
        self.reward = 0
        for a, b in self.edges:
            self.reward+=sum(interest[a.state][b.state])
        return self.reward

    def outputState(self):
        print('----------------')
        for i in range(self.numbers):
            print('%d:%c %f ' % (i,self.nodes[i].state,self.nodes[i].value))
        print(self.reward)
        print('----------------')

if __name__ == '__main__':
    # 问题1
    def designNet1():
        net = Network(6)
        edge_list = [
            (0,1),(0,2),
            (1,3),
            (2,3),
            (3,4),(3,5),
            (4,5)
        ]
        net.addEgde(({net.nodes[a], net.nodes[b]} for a, b in edge_list))
        return net
    # ######################################
    # 问题2
    def designNet2():
        net=Network(10)
        edge_list=[(0,1),(0,2),(0,3),
                   (1,5),(1,2),
                   (3,4),
                   (4,5),(4,7),
                   (5,6),
                   (6,8),
                   (7,8),(7,9)
                   ]

        net.addEgde(
            ({net.nodes[a], net.nodes[b]} for a, b in edge_list))
        return net
        pass
    mynet=designNet2()
    # print(mynet.edges)
    mynet.saveNb()
    max_reward=0

    # i=0
    # max_reward保持3次才输出
    i1=0
    while True:
        reward=mynet.getAllReward()

        # print(reward)
        # mynet.outputState()
        if(max_reward==reward):
            i1+=1
            if(i1==3):
                mynet.calValue()
                mynet.outputState()
                break
        elif(reward>max_reward):
            i1=0
            max_reward=reward
        mynet.updateState()

你可能感兴趣的:(博弈优化找到极小节点覆盖(智能控制3.2))