What is the Blackboard Design Pattern?

Göksu Deniz
7 min readDec 15, 2022

--

Image is created by DALL-E 2 AI

The blackboard design pattern is a way of decomposing a complex problem into smaller, more manageable subproblems. The idea is to create a central “blackboard” that can be accessed by multiple subproblems, which can then share information and collaborate to find a solution.

The blackboard design pattern is useful for problems that are difficult to solve using traditional techniques, such as problems that involve a large number of interdependent variables or that require the use of multiple algorithms to find a solution. This pattern allows the problem to be divided into smaller pieces, which can be solved independently and then combined to find a solution to the overall problem.

Imagine that you and your friends are on a treasure hunt. You need to solve a series of clues and puzzles to find the treasure. Each of you is good at different things, like solving riddles, finding hidden objects, or navigating with a map.

To solve the treasure hunt, you decide to work together. You set up a “blackboard” where you can share your ideas and work together to figure out the clues. Whenever one of you has an idea or solves a clue, you write it on the blackboard for everyone else to see. This way, you can all work together and use your different strengths to solve the treasure hunt.

It works in a similar way in a computer program. Different parts of the program, called “agents,” each focus on a different aspect of the problem and contribute their solutions to a shared space, called the “blackboard.” This allows the different agents to work together and come up with a solution to the problem.

The blackboard design pattern can be implemented in many different ways, depending on the specific problem being solved and the algorithms being used to solve it. For example, the subproblems could be solved in parallel or sequentially, and the blackboard could be implemented using a variety of data structures, such as a database, a message queue, or a shared memory space.

Here’s a simple example of using the blackboard design pattern in C#:

First, let’s define the blackboard class. This class will contain the shared data that the different agents will use to work on the problem. In this example, the blackboard will contain a list of strings where each agent can add their suggestions for solving the problem.

class Blackboard
{
// The shared data that the agents will use to work on the problem
public List<string> suggestions = new List<string>();
}

Next, let’s define the agents that will work on the problem. In this example, we’ll have two agents: a “riddle solver” and a “map reader.”

class RiddleSolver
{
// This agent will try to solve riddles and add their solutions to the blackboard
public void SolveRiddles(Blackboard blackboard)
{
// Solve some riddles and add the solutions to the blackboard
blackboard.suggestions.Add("The answer is 42");
blackboard.suggestions.Add("The capital of France is Paris");
}
}

class MapReader
{
// This agent will try to read a map and add their suggestions to the blackboard
public void ReadMap(Blackboard blackboard)
{
// Read the map and add some suggestions to the blackboard
blackboard.suggestions.Add("The treasure is hidden in the old oak tree");
blackboard.suggestions.Add("The path to the treasure is marked with red flags");
}
}

Finally, let’s put everything together and see how the agents can work together using the blackboard design pattern.

// Create a new blackboard
Blackboard blackboard = new Blackboard();

// Create the agents
RiddleSolver riddleSolver = new RiddleSolver();
MapReader mapReader = new MapReader();

// Have the agents work on the problem
riddleSolver.SolveRiddles(blackboard);
mapReader.ReadMap(blackboard);

// Print the suggestions that the agents have added to the blackboard
foreach (string suggestion in blackboard.suggestions)
{
Console.WriteLine(suggestion);
}

This simple example shows how the different agents (RiddleSolver and MapReader) can work together to solve a problem using the blackboard design pattern. The agents focus on their individual tasks (solving riddles and reading a map), and they contribute their suggestions to the shared blackboard. This allows the agents to work together and come up with a solution to the problem.

The Blackboard design pattern is a way of organizing knowledge and problem-solving strategies within a system. It can be useful in a variety of scenarios, including:

- When you have a complex problem that can be solved by combining multiple approaches or strategies

- When you have multiple agents or components within a system that need to collaborate and share information in order to solve a problem

- When the problem-solving process involves a lot of uncertainty or incomplete information, and you need a flexible way of representing and updating the knowledge and strategies used by the system.

You could use the Blackboard design pattern to implement a system that uses multiple algorithms or approaches to solve a problem, and that allows those algorithms to share and access information and strategies as needed. For example, you might use the Blackboard design pattern to implement a machine learning system that uses a variety of algorithms to make predictions or to classify data, and that allows those algorithms to share and access relevant data and knowledge as they work.

// This is the blackboard class that will hold all of the information
// that the different AI agents will use to make decisions.
class Blackboard
{
// This is the data that the agents will use.
public string CurrentTask { get; set; }
public bool HasFood { get; set; }
public bool HasWater { get; set; }

// This is the list of agents that will use the blackboard to
// make decisions.
public List<IAgent> Agents { get; set; }

// This method will be called by each agent to make a decision
// based on the current state of the blackboard.
public void MakeDecision()
{
foreach (var agent in Agents)
{
agent.Decide(this);
}
}
}

// This is an interface that defines the methods that each AI agent
// must implement.
interface IAgent
{
void Decide(Blackboard blackboard);
}

// This is an example of an AI agent that will prioritize finding food
// if it doesn't have any, and finding water if it doesn't have any of that.
class PrioritizeFoodAndWaterAgent : IAgent
{
public void Decide(Blackboard blackboard)
{
if (!blackboard.HasFood)
{
// If we don't have food, prioritize finding food.
blackboard.CurrentTask = "FindFood";
}
else if (!blackboard.HasWater)
{
// If we have food but not water, prioritize finding water.
blackboard.CurrentTask = "FindWater";
}
else
{
// If we have both food and water, we can do other tasks.
blackboard.CurrentTask = "DoOtherTasks";
}
}
}

// This is an example of an AI agent that will prioritize finding water
// if it doesn't have any, and finding food if it doesn't have any of that.

class PrioritizeWaterAndFoodAgent : IAgent
{
public void Decide(Blackboard blackboard)
{
if (!blackboard.HasWater)
{
// If we don't have water, prioritize finding water.
blackboard.CurrentTask = "FindWater";
}
else if (!blackboard.HasFood)
{
// If we have water but not food, prioritize finding food.
blackboard.CurrentTask = "FindFood";
}
else
{
// If we have both water and food, we can do other tasks.
blackboard.CurrentTask = "DoOtherTasks";
}
}
}

// This is an example of how the blackboard and agents can be used.
class Program
{
static void Main(string[] args)
{
var blackboard = new Blackboard
{
// Set the initial state of the blackboard.
HasFood = false,
HasWater = false,
// Create a list of agents that will use the blackboard.
Agents = new List<IAgent>
{
new PrioritizeFoodAndWaterAgent(),
new PrioritizeWaterAndFoodAgent()
}
};

// Make the initial decision with the current state of the blackboard.
blackboard.MakeDecision();

// Output the current task.
Console.WriteLine(blackboard.CurrentTask);

// Update the state of the blackboard.
blackboard.HasFood = true;
blackboard.HasWater = true;

// Make a new decision with the updated state of the blackboard.
blackboard.MakeDecision();

// Output the current task.
Console.WriteLine(blackboard.CurrentTask);

// Keep the console open.
Console.ReadKey();
}
}

The output of this program will be:

// FindWater
// DoOtherTasks

UML Diagram

This diagram shows the relationships between the Blackboard, Data, Control, and Agent classes. The Blackboard class has methods for adding and getting Data and Control objects, while the Agent class has methods for reading and writing data to the Blackboard and solving the problem using the Control object. Multiple instances of the Agent class can access the Blackboard and cooperate to solve the problem.

Conclusion

The blackboard design pattern is a powerful tool for solving complex problems that are difficult to tackle using traditional techniques. By dividing the problem into smaller subproblems and allowing them to share information and collaborate, the blackboard design pattern can help to find solutions to problems that might otherwise be impossible to solve.

The blackboard design pattern is typically used in complex systems where multiple agents need to collaborate and share information to make decisions. Some examples of scenarios where the blackboard design pattern may be useful include:

- A multi-agent system for controlling a fleet of robots, where each robot is an agent that uses the blackboard to communicate with the other robots and make decisions about their actions.

- An intelligent tutoring system, where multiple agents (e.g. a student model, a domain expert, and a pedagogical expert) use the blackboard to share information and make decisions about the student’s learning process.

- A traffic control system, where multiple agents (e.g. traffic lights, sensors, and cameras) use the blackboard to share information and make decisions about routing traffic to avoid congestion.

In general, the blackboard design pattern can be useful in any scenario where multiple agents need to share information and make decisions based on that information.

Thanks for reading! If you found the article helpful, you can clap and follow. So you will notified of new articles.

# References

It was created with the help of ChatGPT AI.

--

--

Göksu Deniz

Software Engineer, passionate about creating efficient solutions. Skilled in mentoring teams to deliver successful projects. Always exploring new tech trends.