TP1 topic: balance chemical equations
We will examine a simplified version of stoichiometric calculations that allow one to ensure that a chemical equation respects the law of conservation of matter (commonly known as balancing chemical equations).
Consider this equation: Al + O2 → Al2O3
Notice that on both sides the number of Al and O differ.
For this equation to be correct we must determine a coefficient for each term in the equation such that the number of atoms of each element on both sides of the equation is the same.
To balance the equation, we would then write (verify this): 4Al+3O2 →2Al2O3
Code Help
TP1 topic: balance chemical equations
Each coefficient multiplies the number of atoms of each element in each term.
There can be an arbitrary number of terms on both sides of the equation.
You will find a more detailed explanation with examples at the following link: Chem Example
We will have a simplified model of chemical equations where we will balance equations for simple formulas.
Our goal is to determine the coefficients when given an equation.
You will use what you have learned about CLP and about the CLP(FD) library in particular, to tackle this problem.
TP1 topic: balance chemical equations
Your main objective:
Define a predicate balance/2 that balances a given equation The first argument defines the left side of the equation The second argument defines the right side of the equation
You will use a structured representation that represents the equation and its coefficients within a single term as follows:
A Al + B O2 is represented as [A-[al-1],B-[o-2]].
C Al2O3 is represented as [C-[al-2,o-3]].
A Al + B O2 → C Al2O3 is balance([A-[al-1],B-[o-2]],[C-[al-2,o-3]]).
This representation is based on constructing pairs (tuples) with the -/2 infix operator.
You must use library(clpfd), library(pairs) and library(lists) are highly recommended as are predicates like keysort/2.
Programming Help, Add QQ: 749389476
TP1 topic: balance chemical equations
Sample interactions:
?- balance([A-[al-1],B-[o-2]],[C-[al-2,o-3]]).
?- balance([A-[al-1],B-[o-2]],[C-[ab-2,o-3]]).
?- balance([A-[c-4,h-10],B-[o-2]], [C-[c-1,o-2],D-[h-2,o-1]]).
A = 2, B = 13, C = 8, D = 10.
程序代写 CS代考 加QQ: 749389476
TP1 topic: balance chemical equations
Test cases you must successfully balance, each one worth 4/20 of your grade: 1. balance([A-[li-1,fe-1,o-2],B-[h-2,s-1,o-4]],
[C-[fe-1,s-1,o-4],D-[li-2,s-1,o-4],E-[h-2,o-1],F-[o-2]]). A = 4, B= 6, C= 4, D = 2, E = 6, F = 1.
2. balance([A-[li-1,fe-1,p-1,o-4],B-[h-2,s-1,o-4], C-[h-2,o-2]], [D-[fe-1,p-1,o-4],E-[li-2,s-1,o-4],F-[h-2,o-1]]).
A = 2, B = 1, C = 1, D = 2, E =1, F =2
3. balance([A-[li-1,fe-1,o-2],B-[h-1,cl-1]],
[C-[fe-1,cl-2],D-[li-1,cl-1],E-[h-2,o-1],F-[cl-2]]). A = 2, B = 8, C = 2, D = 2, E = 4, F = 1
4. balance([A-[li-1,co-1,o-2],B-[h-2,s-1,o-4],C-[c-6,h-12,o-6]], [D-[co-1,s-1,o-4],E-[li-2,s-1,o-4],F-[c-1,o-2],G-[h-2,o-1]]).
A = 24, B = 36, C = 1, D = 24, E = 12, F = 6, G = 42
TP1 topic: balance chemical equations
Test cases you must successfully balance, each one worth 4/20 of your grade: 5. balance([A-[li-1,fe-1,o-2],B-[h-1,cl-1]],
[C-[fe-1,cl-2],D-[li-1,cl-1],E-[h-2,o-1],F-[o-2]]). A = 4, B= 12, C= 4, D = 4, E = 6, F = 1.
TP1 topic: balance chemical equations – BONUS
BONUS Test cases you should try to successfully balance, each one worth an extra 4/20 of your grade giving a potential 28/20:
6. balance([A-[li-1,fe-1,o-2],B-[h-1,n-1,o-3]], [C-[fe-1,[n-1,o-3]-2],D-[li-1,n-1,o-3],E-[h-2,o-1],F-[o-2]]).
Hint: You might want to try to analyze the structure recursively to get the subgroup.
A = 4, B = 12, C = 4, D = 4, E = 6, F = 1
7. balance([A-[li-1,fe-1,o-2],B-[h-1,c-1,o-1,o-1,h-1],C-[h-2,o-2]],
[D-[fe-1,[c-1,o-1,o-1,h-1]-2],E-[li-1,c-1,o-1,o-1,h-1],
F-[h-2,o-1],G-[o-2]]).
Hint: You must also consider the fact that elements can show up repeatedly in
the same molecule.
A = 2, B = 6, C = 1, D =2, E = 2, F =4, G = 1
TP1 topic: balance chemical equations
Do not use:
Imperative KB update predicates (assert, retract, etc.),
instead pass all computation context as predicate arguments
Prolog’s baseline imperative arithmetic predicates (is, >, <, =:= etc.), instead use CLP(FD) constraints prefixed with #
Prolog’s baseline imperative if-then predicate ->/2 for conditional premises, instead use CLP if-then-else predicate if_/3 from reif CLP library
Prolog’s baseline negated unification \=/2, instead use CLP dif/2 of dif CLP library
Follow the conventions outlined in the course guideline, HOWEVER, deliver a single branch for your homework: tp1