VITyarthi Fundamentals of AI and ML Module 6 Challenging Task Solution (General Question to use all topics)

Table of Contents

Note : If you directly copy – paste this solution , you may get any error.

Question 1

Suppose you have a list of animals in a zoo, along with their species, age, and feeding schedule. Write a Prolog program that allows the zookeeper to determine which animal to feed next, based on the following rules:
→ Animals that are “carnivores” and are over 10 years old should be fed first.

→ If no such animals exist, then feed animals that are “herbivores” and are over 5 years old. 

→ If no such animals exist, then the youngest animal in the zoo, regardless of species or feeding schedule, should be fed. 

Assume that the list of animals is represented as a Prolog fact, with each animal represented as a term of the form animal(Name, Species, Age, Diet) and the feeding schedule represented as a Prolog rule, with each diet represented as a predicate of the form diet(Diet). 

% Facts representing animals in the zoo

 

Animal(lion, carnivore, 12). Animal(tiger, carnivore, 15). Animal(elephant, herbivore, 8). Animal(giraffe, herbivore, 6). Animal(monkey, omnivore, 4).

 

 

 

% Rules representing feeding schedule

 

Carnivore(meat). Herbivore(grass). Omnivore(fruits, insects).

 

 

 

% Predicate to determine the animal to feed next

 

Next_animal_to_feed(Animal) :-

 

Findall(Age-Animal, eligible_animal(Age, Animal), EligibleAnimals), Sort(EligibleAnimals, SortedAnimals),

(SortedAnimals = [_-Animal|_] -> true ; youngest_animal(Animal)).

 

 

 

 

% Predicate to find eligible animals based on the rules

 

Eligible_animal(Age, Animal) :- Animal(Animal, carnivore, Age), Age > 10.

 

 

 

Eligible_animal(Age, Animal) :- Animal(Animal, herbivore, Age), Age > 5.

 

 

 

% Predicate to find the youngest animal

 

Youngest_animal(Animal) :- Animal(Animal, _, _),

\+ (animal(_, _, Age), Age < Animal).

Question 2

Suppose you are developing a movie recommendation system for a streaming service. You have a database of movies with information on their genre, rating, and release year. Create a Prolog program that takes in a user’s preferences and returns a list of recommended movies. The recommendations should be based on the user’s preferred genre, desired rating, and preferred release year range. The program should optimize its performance by filtering out movies that don’t meet the user’s preferences. If the program is unable to find suitable recommendations, then it should notify the user.

% Facts representing movies in the database Movie(avengers_endgame, action, 8.4, 2019). 

Movie(the_shawshank_redemption, drama, 9.3, 1994). 

Movie(inception, thriller, 8.8, 2010). Movie(pulp_fiction, crime, 8.9, 1994).

Movie(fight_club, drama, 8.8, 1999). 

Movie(the_dark_knight, action, 9.0, 2008). 

Movie(the_matrix, sci_fi, 8.7, 1999). 

Movie(titanic, romance, 7.8, 1997). 

Movie(jurassic_park, adventure, 8.1, 1993). 

Movie(braveheart, war, 8.3, 1995).

% Predicate to recommend movies based on user preferences

Recommend_movies(PreferredGenre, DesiredRating, StartYear, EndYear, RecommendedMovies) :-

Findall(Movie, eligible_movie(Movie, PreferredGenre, DesiredRating, StartYear, EndYear), RecommendedMovies),

RecommendedMovies \= [].

% Predicate to check if a movie is eligible based on user preferences

Eligible_movie(Movie, PreferredGenre, DesiredRating, StartYear, EndYear) :- Movie(Movie, Genre, Rating, Year),

Genre = PreferredGenre, Rating >= DesiredRating, Year >= StartYear,

Year =< EndYear.

If you find anything wrong in this Solution, feel free to reach us in the comment section.

Sharing Is Caring:

2 thoughts on “VITyarthi Fundamentals of AI and ML Module 6 Challenging Task Solution (General Question to use all topics)”

Leave a Comment