/* Survivor problem (PuzzlOR) in Picat. From PuzzlOR Survivor http://www.analytics-magazine.org/july-august-2010/137-puzzlor-survivor.html """ By John Toczek Getting lost while hiking in the wilderness is a dangerous situation to find yourself in. And making your way back to civilization is a difficult task that quickly uses up resources. What you decide to take with you while making the journey back to civilization can determine life or death. Table 1 shows all of the items that are available to you that will aid you in your hike out of the wilderness. Containers of food and water will give you energy, shelter will protect you from the elements, and defense will protect you from wild animals. Each item has a weight indicated by the red number and each item has survival points indicated by the green number. You must take one item from each of the four categories (food, water, shelter, defense). Unfortunately, the backpack you have has a maximum capacity of 25 kg. Your chance for survival is calculated by adding all of the survival points together from the items you choose to take with you. Table 1: Choose items carefully. [ Weight (kg) Survival (pts) Food 5 10 8 20 12 25 Water 3 10 5 20 8 25 Shelter 5 5 8 15 12 20 Defence 1 5 2 15 3 20 ] Question: What is the maximum chance for survival you can achieve? """ This is the unique solution: sum_survival_points = 75 sum_weight = 24 x = [2,2,2,3] Food : 8kg 20pts Water : 5kg 20pts Shelter: 8kg 15pts Defence: 3kg 20pts This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => data(Food,Water,Shelter,Defence,MaxCap), survivor(Food,Water,Shelter,Defence,MaxCap, X,SumWeight,SumSurvivalPoints), println(sum_survival_points=SumSurvivalPoints), println(sum_weight=SumWeight), println(x=X), printf("Food : %2dkg %2dpts\n",Food[X[1],1],Food[X[1],2]), printf("Water : %2dkg %2dpts\n",Water[X[2],1],Water[X[2],2]), printf("Shelter: %2dkg %2dpts\n",Shelter[X[3],1],Shelter[X[3],2]), printf("Defence: %2dkg %2dpts\n",Defence[X[4],1],Defence[X[4],2]), nl. survivor(Food,Water,Shelter,Defence,MaxCap, X,SumWeight,SumSurvivalPoints) => NumTypes = 4, % number of types NumItems = 3, % items per type % decision variables X = new_list(NumTypes), X :: 1..NumItems, matrix_element(Food,X[1],1,FoodWeight), matrix_element(Water,X[2],1,WaterWeight), matrix_element(Shelter,X[3],1,ShelterWeight), matrix_element(Defence,X[4],1,DefenceWeight), SumWeight #= FoodWeight + WaterWeight + ShelterWeight + DefenceWeight, SumWeight #<= MaxCap, matrix_element(Food,X[1],2,FoodPoints), matrix_element(Water,X[2],2,WaterPoints), matrix_element(Shelter,X[3],2,ShelterPoints), matrix_element(Defence,X[4],2,DefencePoints), SumSurvivalPoints #= FoodPoints + WaterPoints + ShelterPoints + DefencePoints, if var(SumSurvivalPoints) then solve($[max(SumSurvivalPoints)],X) else solve($[],X) end. data(Food,Water,Shelter,Defence,MaxCap) => % the items: type, weight, survival pts % Weight (kg) Survival (pts) Food = [[ 5, 10], [ 8, 20], [12, 25]], Water = [[ 3, 10], [ 5, 20], [ 8, 25]], Shelter = [[ 5, 5], [ 8, 15], [12, 20]], Defence = [[ 1, 5], [ 2, 15], [ 3, 20]], % Max capacity MaxCap = 25.