I am trying to solving a set of problems similar to the Einsten problem using prolog.
My input consists of two lists:
- A domain list. Ex: [[domain(brand),volkswagen,gm,audi],[domain(country),germany,spain,italy]].
- A constraint list: [[=,spain,[+,gm,1]],[=,germany,volkswagen],[=,italy,2]]. It means: spain = gm + 1, germany = volkswagen, italy = 2.
I can solve this problem easily hard coding it:
puzzle(Spain,Italy,Germany,Volkswagen,Gm,Audi,X):-
Country = [Spain, Italy, Germany], ins(Country, 1..X), all_different(Country),
Brand = [Volkswagen, Gm, Audi], ins(Brand, 1..X), all_different(Brand),
Spain #= Gm + 1,
Germany #= Volkswagen,
Italy #= 2.
And calling:
275 ?- puzzle(Spain, Italy,Germany, Volkswagen, Gm, Audi,3).
Spain = Audi, Audi = 3,
Italy = Gm, Gm = 2,
Germany = Volkswagen, Volkswagen = 1.
My questions:
- What would be a way to create the domains dynamically from my input data? In this example I only have 2 domains (Country, Brand) but there are another inputs with 5 or 6 domains. Thus, how could I make the number and size of the domains variable?
- How could I create the constraints dynamically from the list I have? How to connect the constants from the constraint's list to the variables of the previous question?
In summary, how to build a solver that depends only on the input?