> For the complete documentation index, see [llms.txt](https://swarmreasoning.gitbook.io/miao16z/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://swarmreasoning.gitbook.io/miao16z/technical/reasoning.md).

# Reasoning

**Temperature reasoning** in the Miao Swarm framework is a mechanism for controlling the level of creativity and randomness in the decision-making process of sub-agents. By modulating the temperature parameter, the system balances exploratory (creative) behaviors with exploitation (precise, task-focused) actions. This adaptability allows sub-agents to effectively handle tasks that vary in complexity and ambiguity.

<figure><img src="/files/NupmAqydSgHhyv2jw15Y" alt=""><figcaption></figcaption></figure>

***

#### **How Temperature Reasoning Works**

1. **High-Temperature Agents:**
   * Operate with high randomness and creativity.
   * Generate a wide range of solutions, including unconventional or “off-topic” ideas.
   * Suitable for exploration phases, where innovative or novel solutions are needed.
   * Example: Brainstorming a new optimization method for logistics.
2. **Low-Temperature Agents:**
   * Prioritize precision and deterministic logic.
   * Narrow their focus to proven or efficient solutions.
   * Suitable for execution or refinement phases, where task completion is paramount.
   * Example: Fine-tuning transport routes based on existing logistics data.
3. **Dynamic Temperature Adjustment:**
   * The framework adjusts the temperature of agents in real time based on the phase of the task or feedback from swarm reasoning.
   * For ambiguous problems, the temperature starts high, encouraging exploration, and decreases as clarity emerges.
   * During refinement stages, the temperature is set low to ensure focus and precision.

***

#### **Temperature-Based Workflow**

1. **Task Initialization:**
   * Tasks are analyzed for complexity, ambiguity, and the need for creativity.
   * Initial temperature values are assigned to sub-agents accordingly.
2. **Exploration Phase (High Temperature):**
   * Sub-agents generate diverse solutions by leveraging probabilistic models and random sampling.
   * Outputs may include novel or unconventional ideas, which are fed into the swarm reasoning stage.
3. **Refinement Phase (Low Temperature):**
   * The swarm filters and integrates the most viable solutions from the exploration phase.
   * Sub-agents focus on precision, enhancing the selected solutions or aligning them with task requirements.
4. **Final Decision:**
   * Unified results are generated, balancing creativity with task-specific accuracy.

***

#### **Practical Example**

**Scenario:** Optimizing disaster response logistics.

1. **High Temperature (Exploration Phase):**
   * Sub-Agent 1 proposes new, unconventional transport routes.
   * Sub-Agent 2 suggests combining medical supply chains with local food distribution.
   * Sub-Agent 3 explores alternate inventory stocking strategies.
2. **Low Temperature (Refinement Phase):**
   * Sub-Agent 1 refines its most efficient transport route based on travel time and cost.
   * Sub-Agent 2 adjusts its supply chain model to align with medical demand.
   * Sub-Agent 3 eliminates unfeasible inventory strategies, focusing on viable options.
3. **Outcome:**
   * Unified disaster relief plan integrating creativity and precision.

***

#### **Benefits of Temperature Reasoning**

1. **Exploration vs. Exploitation Trade-off:**
   * Balances creative problem-solving with task-focused execution.
   * Ensures innovation without sacrificing precision.
2. **Dynamic Adaptability:**
   * Adjusts agent behavior in real time to match task phases or feedback.
3. **Improved Collaboration:**
   * High-temperature agents generate diverse inputs, enriching the swarm reasoning process.
   * Low-temperature agents ensure alignment with task goals.
4. **Enhanced Problem-Solving:**
   * Enables sub-agents to tackle ambiguous, multi-dimensional problems effectively.

***

#### **Challenges and Considerations**

1. **Temperature Tuning:**
   * Setting appropriate temperature values is critical to balancing exploration and exploitation.
   * Requires robust metrics to evaluate task complexity and phase transitions.
2. **Computational Overhead:**
   * High-temperature phases may generate large volumes of data, requiring efficient filtering mechanisms.
3. **Inter-Agent Coordination:**
   * Ensuring that temperature adjustments in individual agents align with swarm-wide goals.

```
import random

class SubAgent:
    def __init__(self, name, temperature):
        self.name = name
        self.temperature = temperature  # High = creative, Low = precise

    def generate_solution(self, task):
        # Simulate creativity with randomness
        base_solution = f"{self.name} tackles {task}"
        if random.uniform(0, 1) < self.temperature:
            return f"{base_solution} with a creative twist!"
        else:
            return f"{base_solution} using a standard approach."

    def adjust_temperature(self, phase):
        if phase == "exploration":
            self.temperature = 0.8
        elif phase == "refinement":
            self.temperature = 0.2


# Example Usage
task = "Optimize Logistics"
agent = SubAgent(name="TransportAI", temperature=0.8)

# Exploration Phase
agent.adjust_temperature("exploration")
print(agent.generate_solution(task))

# Refinement Phase
agent.adjust_temperature("refinement")
print(agent.generate_solution(task))

```
