Symulacja akapu

Lancaster

Kapłan Pustki
1 158
1 114
Co prawda nie ma obecnie możliwości symulowania akapu, ale można zrobić eksperyment cyfrowy. Czy ktoś by się chciał zabawić w tym wątku w symulacje akapu?
 

noniewiem

Well-Known Member
615
975
Kod:
from mesa import Agent, Model
from mesa.time import RandomActivation
from mesa.space import MultiGrid
import random


# Lista dostępnych wartości dla kategorii
religions = ['religion1', 'religion2', 'religion3']
ideologies = ['ideology1', 'ideology2', 'ideology3']
lifestyles = ['lifestyle1', 'lifestyle2', 'lifestyle3']
reproductive_strategies = ['strategy1', 'strategy2', 'strategy3']


# Lista dostępnych osobowości
personalities = ['aggressive', 'passive', 'rational', 'emotional']


# Lista chorób psychicznych
mental_illnesses = [None, 'depression', 'anxiety', 'bipolar_disorder']


# Lista dostępnych płci
genders = ['male', 'female']


# Lista dostępnych ras
races = ['race1', 'race2', 'race3', 'race4']


# Definicja agenta
class TraderAgent(Agent):
    def __init__(self, unique_id, model):
        super().__init__(unique_id, model)
        self.money = random.randint(50, 100)  # Pieniądze
        self.goods = random.randint(20, 50)   # Towary
        self.security = 0  # Pieniądze wydane na ochronę
        self.religion = random.choice(religions)  # Przypisana religia
        self.ideology = random.choice(ideologies)  # Przypisana ideologia
        self.lifestyle = random.choice(lifestyles)  # Przypisany styl życia
        self.reproductive_strategy = random.choice(reproductive_strategies)  # Przypisana strategia rozrodcza
        self.personality = random.choice(personalities)  # Przypisana osobowość
        self.mental_illness = random.choice(mental_illnesses)  # Przypisana choroba psychiczna
        self.gender = random.choice(genders)  # Przypisana płeć
        self.race = random.choice(races)  # Przypisana rasa
        self.age = random.randint(18, 70)  # Przypisany wiek


    def step(self):
        # Wynajęcie ochrony (losowa decyzja zmodyfikowana przez religię, ideologię, styl życia, strategię rozrodczą, osobowość i czynniki biologiczne)
        if random.random() < self.hire_security_probability():
            self.hire_security()


        # Wybierz losowego agenta do handlu lub wymuszenia
        other_agent = self.random.choice(self.model.schedule.agents)
        
        if other_agent != self:
            if random.random() < self.coerce_probability():
                self.coerce_trade(other_agent)
            else:
                self.voluntary_trade(other_agent)


    def hire_security_probability(self):
        base_probability = 0.1
        if self.religion == 'religion1':
            base_probability += 0.1
        elif self.ideology == 'ideology1':
            base_probability += 0.05
        elif self.lifestyle == 'lifestyle1':
            base_probability -= 0.05
        elif self.reproductive_strategy == 'strategy1':
            base_probability += 0.05


        if self.personality == 'aggressive':
            base_probability += 0.1
        elif self.personality == 'passive':
            base_probability -= 0.05
        elif self.personality == 'rational':
            base_probability += 0.05
        elif self.personality == 'emotional':
            base_probability -= 0.05


        if self.mental_illness == 'depression':
            base_probability -= 0.05
        elif self.mental_illness == 'anxiety':
            base_probability += 0.1
        elif self.mental_illness == 'bipolar_disorder':
            base_probability += random.choice([-0.1, 0.1])  # Zmienność nastroju


        if self.gender == 'male':
            base_probability += 0.05
        elif self.gender == 'female':
            base_probability -= 0.05


        if self.age < 30:
            base_probability += 0.05
        elif self.age > 50:
            base_probability -= 0.05


        return base_probability


    def coerce_probability(self):
        base_probability = 0.05
        if self.personality == 'aggressive':
            base_probability += 0.15
        elif self.personality == 'passive':
            base_probability -= 0.05
        if self.mental_illness == 'bipolar_disorder' and random.random() < 0.5:
            base_probability += 0.1  # Zwiększone ryzyko wymuszenia w stanach maniakalnych


        if self.gender == 'male':
            base_probability += 0.05
        elif self.gender == 'female':
            base_probability -= 0.05


        if self.age < 30:
            base_probability += 0.05
        elif self.age > 50:
            base_probability -= 0.05


        return base_probability


    def hire_security(self):
        cost = random.randint(5, 15)
        if self.money >= cost:
            self.money -= cost
            self.security += cost


    def voluntary_trade(self, other_agent):
        if self.money > 0 and other_agent.goods > 0:
            trade_amount = random.randint(1, min(self.money, other_agent.goods))
            self.money -= trade_amount
            self.goods += trade_amount
            other_agent.money += trade_amount
            other_agent.goods -= trade_amount


    def coerce_trade(self, other_agent):
        if other_agent.goods > 0:
            trade_amount = random.randint(1, other_agent.goods)
            other_agent.goods -= trade_amount
            self.goods += trade_amount
            print(f"Agent {self.unique_id} coerced Agent {other_agent.unique_id} for {trade_amount} goods.")


# Definicja modelu
class TradeModel(Model):
    def __init__(self, N):
        self.num_agents = N
        self.schedule = RandomActivation(self)
        self.grid = MultiGrid(10, 10, True)
        
        for i in range(self.num_agents):
            agent = TraderAgent(i, self)
            self.schedule.add(agent)
            x = self.random.randrange(self.grid.width)
            y = self.random.randrange(self.grid.height)
            self.grid.place_agent(agent, (x, y))


    def step(self):
        self.schedule.step()
        # Losowe konflikty i rozwiązania przez sąd
        if random.random() < 0.05:
            self.resolve_conflict()


    def resolve_conflict(self):
        agent1, agent2 = random.sample(self.schedule.agents, 2)
        if agent1.goods > 0 and agent2.goods > 0:
            winner = random.choice([agent1, agent2])
            loser = agent1 if winner == agent2 else agent2
            penalty = random.randint(1, min(loser.goods, 10))
            loser.goods -= penalty
            winner.goods += penalty
            print(f"Conflict resolved: Agent {winner.unique_id} wins, Agent {loser.unique_id} loses {penalty} goods.")


# Inicjalizacja modelu
model = TradeModel(10)


# Uruchomienie modelu na kilka kroków
for i in range(10):
    model.step()
    for agent in model.schedule.agents:
        print(f"Agent {agent.unique_id}: Money = {agent.money}, Goods = {agent.goods}, Security = {agent.security}, Religion = {agent.religion}, Ideology = {agent.ideology}, Lifestyle = {agent.lifestyle}, Reproductive Strategy = {agent.reproductive_strategy}, Personality = {agent.personality}, Mental Illness = {agent.mental_illness}, Gender = {agent.gender}, Race = {agent.race}, Age = {agent.age}")
 

jkruk3

Member
50
96
Symulowanie jest jak najbardziej możliwe i to przy użyciu różnych frameworków. Ba nawet można mówić o elementach istnienia w ramch walki państw. Jak tam ten projekt tego czecha?
 
Do góry Bottom