Part 3: Machine learning Lesson 12 of 20 in this part
Logic, constraints and planning
What you will learn. Solve a constraint problem by backtracking, check a logical rule with a truth table, describe how a planner turns a goal into steps, and see how a genetic algorithm searches by mutation and selection.
Before this lesson: Search and games, the classic AI way. Already know these? Carry on.
In plain words
Not every AI problem is learned from data. Some are puzzles with rules: schedule these exams so no student has two at once, colour this map so neighbours differ, prove that a conclusion follows from some facts. This lesson covers four classic tools. A constraint satisfaction problem lists variables (regions), the values each may take (colours) and constraints between them (neighbours must differ). Backtracking fills them in one at a time and undoes a choice as soon as it breaks a rule. Logic writes facts and rules as statements that are true or false, and checks what follows; a truth table tries every combination. Planning takes a starting situation, a goal and a set of actions, each with conditions and effects, and searches for a sequence of actions that reaches the goal. When a problem has no clear rules but a way to score an answer, a genetic algorithm evolves a population of candidates by selection, crossover and mutation.
- InputPick the next variableFor example the next region to colour
- ProcessTry each value in turnRed, then green, then blue
- RiskOn a broken rule, undo and try the next valueDead ends are dropped early
- OutputIf every variable has a value, stopA solution has been found
The key saving is undoing a choice as soon as it breaks a rule, instead of finishing a bad assignment.
Propositional logic
- Statements that are true or false
- Combined with and, or, not, if-then
- A truth table can check everything
First-order logic
- Adds objects, properties, "for all", "exists"
- Can say "every bird can fly except penguins"
First-order logic can say more, and is harder to reason with.
- InputStart with a random populationEach candidate is a string of bits
- ProcessScore each candidateThe fitness
- ModelPick good parents and mix themCrossover, then rare mutations
- OutputReplace the population and repeatAverage fitness tends to rise
Nobody tells it the answer. It only scores candidates and keeps the better ones.
State
- A description of the world right now
Action
- Has conditions that must hold first
- Has effects that change the state
Plan
- A sequence of actions from the start to the goal
A plan is a path through the space of possible situations.
Real-world example
A hospital must roster nurses. Every shift needs enough staff, nobody may work more than five days in a row, certain pairs must not be on together, and everybody has days off requested. This is a constraint problem, and a solver fills in the roster with backtracking and prunes any partial roster that already breaks a rule. A warehouse robot, in contrast, needs a plan: pick up the box, carry it to the shelf, put it down. Each action has conditions (the hands must be empty) and effects (the box is now on the shelf), and a planner searches for a sequence. A company designing a delivery network might use a genetic algorithm when each design can be scored by simulation but cannot be written as a formula.
How it works
- Constraints. List the variables, the allowed values and the constraints. Backtracking picks a variable, tries a value, checks every constraint among the variables already set, and goes back one step on a failure.
- Logic. Write each fact and rule as a statement. To check whether a conclusion follows, list every row of the truth table and see whether the conclusion holds in all rows where all the premises hold.
- Planning. Describe each action by its conditions and effects. Search the states reachable from the start, using the same search methods as before (breadth-first or A* with a guess of the steps to the goal) until a state satisfies the goal.
- Genetic algorithm. Keep a population. Pick parents by a small tournament, join the front of one with the back of the other (crossover), flip a bit rarely (mutation), keep the best one unchanged, and repeat.
- Other classic ideas. Expert systems store an expert's rules as if-then statements and chain them together. Fuzzy logic lets a statement be partly true, for example "warm" to degree 0.7. Swarm methods are search methods in the same spirit as genetic algorithms.
Live simulation
A rule-based solver searches for choices that satisfy every rule. Order matters more than it seems.
The question. How does a program solve a puzzle with rules, and why does the order of its choices matter?
What to watch
- Neighbouring regions must have different colours. The program tries a colour, moves on, and undoes its choice when it hits a dead end.
- Run the fixed order to the end and note the colours tried and the backtracks.
- Then choose the most constrained region first and compare.
This simulation needs JavaScript, which is switched off or not loaded. The lists above say what it shows, and the example programs below print the same kind of numbers.
Try this
- Run the most-constrained-first order to the end. It should need no backtracks.
What this leaves out. Seven regions and three colours. Real solvers (for timetables, routes or configurations) also remove colours that can no longer work before choosing, and learn from past dead ends. Building a plan step by step is not shown.
Python program that prints the same headline numbers: examples/sim-ai/ai.py.
Python example
Python: example run The example programme behind this lesson's quoted output ran on 2026-11-19 (Python 3.12.10). 1 code block in the lesson passed a syntax check, 1 also ran on their own.
Three small demonstrations: map colouring with backtracking, a truth-table check that "if P then Q, and P" guarantees Q, and a genetic algorithm that evolves a 10-bit string towards all ones, with a small repeatable random generator so that the numbers repeat.
"""Constraint satisfaction (map colouring), a truth-table check of a logical rule, and a small genetic algorithm.
The genetic algorithm uses a small repeatable random generator, so the numbers repeat and match the C# version.
Run: python logic.py
"""
import itertools
# 1. Colour five regions with three colours so that neighbours differ
regions = ["A", "B", "C", "D", "E"]
edges = [("A", "B"), ("A", "C"), ("B", "C"), ("B", "D"), ("C", "D"), ("D", "E")]
colours = ["red", "green", "blue"]
def ok(assign):
return all(assign[a] != assign[b] for a, b in edges if a in assign and b in assign)
brute = sum(1 for combo in itertools.product(colours, repeat=5) if ok(dict(zip(regions, combo))))
tried = 0
def backtrack(assign):
global tried
if len(assign) == len(regions):
return assign
r = regions[len(assign)]
for c in colours:
tried += 1
assign[r] = c
if ok(assign):
found = backtrack(assign)
if found: return found
del assign[r]
return None
solution = backtrack({})
print("brute force would check %d assignments and finds %d that work" % (3 ** 5, brute))
print("backtracking found a solution after %d colour choices: %s" % (tried, " ".join("%s=%s" % (r, solution[r]) for r in regions)))
# 2. Does "if P then Q, and P" guarantee Q? Check every row of the truth table
rows = [(p, q, ((not p) or q) and p) for p in (True, False) for q in (True, False)]
holds = all(q for p, q, premise in rows if premise)
print("rows where both statements are true:", [(p, q) for p, q, premise in rows if premise], "-> Q is always true there:", holds)
# 3. Genetic algorithm: evolve a 10-bit string towards all ones
state = 12345
def rnd():
global state
state = (1103515245 * state + 12345) % (2 ** 31)
return state / 2 ** 31
pop = [[1 if rnd() < 0.5 else 0 for _ in range(10)] for _ in range(6)]
fit = lambda g: sum(g)
print("generation best fitness average fitness")
for gen in range(0, 9):
print("%10d %12d %15.2f" % (gen, max(fit(g) for g in pop), sum(fit(g) for g in pop) / len(pop)))
def pick(): # tournament of two
a, b = pop[int(rnd() * 6)], pop[int(rnd() * 6)]
return a if fit(a) >= fit(b) else b
nxt = [max(pop, key=fit)[:]] # keep the best one
while len(nxt) < 6:
p1, p2, cut = pick(), pick(), 1 + int(rnd() * 9)
child = p1[:cut] + p2[cut:]
child = [1 - b if rnd() < 0.05 else b for b in child] # a rare mutation flips a bit
nxt.append(child)
pop = nxtbrute force would check 243 assignments and finds 12 that work
backtracking found a solution after 9 colour choices: A=red B=green C=blue D=red E=green
rows where both statements are true: [(True, True)] -> Q is always true there: True
generation best fitness average fitness
0 6 4.50
1 6 4.17
2 7 6.00
3 7 6.00
4 7 6.33
5 7 6.17
6 7 6.50
7 8 6.00
8 8 6.33Brute force would check 243 assignments and finds 12 that work. Backtracking reaches a valid colouring after 9 colour choices. In the truth table the only row where both statements are true is P true and Q true, so Q follows. The genetic algorithm starts with a best fitness of 6 and an average of 4.50, and after eight generations the best is 8 and the average 6.33. It did not reach 10 in this short run.
.NET (C#) example
C#: compiled and run The C# sample compiled on 2026-11-19 with .NET SDK 10.0.400 (no extra packages). It also ran to the end without error.
The same three demonstrations written from scratch in C# with no packages, printing the same output. For large constraint problems in .NET, a dedicated solver library is the usual choice; for logic and planning there is no standard .NET library, and the small algorithms here are easy to write directly.
// Map colouring by backtracking, a truth-table check, and a small genetic algorithm. Same output as logic.py. No packages.
string[] regions = { "A", "B", "C", "D", "E" };
(string, string)[] edges = { ("A", "B"), ("A", "C"), ("B", "C"), ("B", "D"), ("C", "D"), ("D", "E") };
string[] colours = { "red", "green", "blue" };
bool Ok(Dictionary<string, string> a) => edges.All(e => !(a.ContainsKey(e.Item1) && a.ContainsKey(e.Item2)) || a[e.Item1] != a[e.Item2]);
int brute = 0;
for (int code = 0; code < 243; code++) // every one of the 3^5 complete assignments
{
var a = new Dictionary<string, string>(); int x = code;
foreach (var r in regions) { a[r] = colours[x % 3]; x /= 3; }
if (Ok(a)) brute++;
}
int tried = 0;
Dictionary<string, string>? Backtrack(Dictionary<string, string> a)
{
if (a.Count == regions.Length) return a;
string r = regions[a.Count];
foreach (var c in colours)
{
tried++; a[r] = c;
if (Ok(a)) { var found = Backtrack(a); if (found != null) return found; }
a.Remove(r);
}
return null;
}
var solution = Backtrack(new Dictionary<string, string>())!;
Console.WriteLine($"brute force would check 243 assignments and finds {brute} that work");
Console.WriteLine($"backtracking found a solution after {tried} colour choices: " + string.Join(" ", regions.Select(r => $"{r}={solution[r]}")));
// Does "if P then Q, and P" guarantee Q? Check every row of the truth table
var rows = new List<(bool p, bool q, bool premise)>();
foreach (bool p in new[] { true, false }) foreach (bool q in new[] { true, false }) rows.Add((p, q, (!p || q) && p));
string B(bool v) => v ? "True" : "False";
bool holds = rows.Where(t => t.premise).All(t => t.q);
Console.WriteLine("rows where both statements are true: [" + string.Join(", ", rows.Where(t => t.premise).Select(t => $"({B(t.p)}, {B(t.q)})")) + $"] -> Q is always true there: {B(holds)}");
// Genetic algorithm: evolve a 10-bit string towards all ones
long state = 12345;
double Rnd() { state = (1103515245L * state + 12345) % (1L << 31); return state / (double)(1L << 31); }
var pop = Enumerable.Range(0, 6).Select(_ => Enumerable.Range(0, 10).Select(__ => Rnd() < 0.5 ? 1 : 0).ToArray()).ToList();
int Fit(int[] g) => g.Sum();
Console.WriteLine("generation best fitness average fitness");
for (int gen = 0; gen <= 8; gen++)
{
Console.WriteLine($"{gen,10} {pop.Max(Fit),12} {pop.Average(g => (double)Fit(g)),15:F2}");
int[] Pick() { var a = pop[(int)(Rnd() * 6)]; var b = pop[(int)(Rnd() * 6)]; return Fit(a) >= Fit(b) ? a : b; } // tournament of two
int[] bestOne = pop[0]; foreach (var g in pop) if (Fit(g) > Fit(bestOne)) bestOne = g; // keep the best one
var next = new List<int[]> { (int[])bestOne.Clone() };
while (next.Count < 6)
{
var p1 = Pick(); var p2 = Pick(); int cut = 1 + (int)(Rnd() * 9);
var child = p1.Take(cut).Concat(p2.Skip(cut)).ToArray();
for (int k = 0; k < 10; k++) if (Rnd() < 0.05) child[k] = 1 - child[k]; // a rare mutation flips a bit
next.Add(child);
}
pop = next;
}Maths behind it
- Powers: how a search space grows (arithmetic)
- Truth tables and Boolean logic (logic)
- Counting and averaging fitness (arithmetic)
- Probability of a mutation (probability)
Common mistakes
- Checking constraints only after the whole assignment is complete, which wastes most of the work.
- Treating a truth table as practical for dozens of statements.
- Writing a plan's actions without clear conditions, so impossible steps are allowed.
- Using a genetic algorithm when a direct method exists. It is a general tool and often slower.
- Reading a short genetic-algorithm run as proof that it works. Repeat with different random seeds.
Quick check
Why is backtracking much faster than trying all complete assignments?
Show answer
It undoes a choice as soon as it breaks a rule, so every assignment that contains that mistake is never examined.
How many rows does a truth table for 4 statements have?
Show answer
2^4 = 16.
What are the two parts of an action in a planner?
Show answer
Conditions that must be true before it can be done, and effects that change the situation afterwards.
What do crossover and mutation do in a genetic algorithm?
Show answer
Crossover builds a child from parts of two parents. Mutation flips a bit now and then, which keeps variety in the population.
Completion
Evidence of completion. A map coloured by backtracking with the count of choices tried, a truth table that proves or refutes one rule, and the best and average fitness over generations of a small genetic algorithm.
Saved in this browser only. Reading and exercise are ticked separately because they are different achievements. A tick records your own work; it is not a test result. See all progress.