sheep and wolves problem python

I tried to figure out what the first row of the inverse matrix P would look like based on the fact that the first row of the product P*C should be (1, 0, , 0). It isn't enough either way and that's why 10x10 blocks will never be a reality. Also, it looks like we can use the exact same series of tests, plus a one-on-one test of the newcomer, to find 2 wolves among 9 sheep in only 7 tests. This is nice to know because it makes for some puzzles you could try by hand, instead of trusting a computer program. I will get a negative result if all the samples are from actual sheep. So if you're thinking 63 tests isn't a great answer I would contend that it actually is. 2, Programming environment Python language 3, Description of experiment content A farmer with a wolf, a sheep and a basket of vegetables wanted to take a boat from the left bank of the river to the right bank. How to solve sheep and wolves puzzle in python? If you could show that a second 11x9 block could be swapped without breaking the requirements, then the total tests would drop to 61 a not unbelievable number. If we only have 1 wolf now, whats the largest number of sheep we can add to the board? 1 Sheep & Wolves: Mini-Project 1 Condor Chou [email protected] Abstract Mini-Project 1 asks us to solve the Sheep & Wolf. In other words: \begin{equation}R_{i,j} = \cup_{k=1}^{100}(c_{i,k}\cap m_{k,j})\end{equation}. Help keep the big bad wolf from eating the three little sheep. Well, I guess its not the same story and unfortunately, the sheep live in a field, not a house but you get the idea. And each state needs to capture everything in the 'world' model. I will take a blood sample from 99 randomly choosen 'sheep' and send those 99 blood samples to the doctors. Define the 5-separable matrix $M$ by $$ T . In the first experiment, wolves and sheep wander randomly around the landscape, while the wolves look for sheep to prey on. He thus decided to place some shepherd dogs in such a way that all his sheep are protected. By property 1, at least one of these two sheep was tested six times, say Shaun in group A. However, if the wolves are at positions 29, 31, 32 all tests will also be positive for all iterations. I though about it many times, and the only way came to my mind is by all the possibilities. The best answers are voted up and rise to the top, Not the answer you're looking for? if there are two wolves, then I have seen proof that we cannot always do it in the bare information-theoretical minimum number of tests. How do you move all the animals from one side of the river to the other? Can't figure out how to fix it. Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missing, Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition. I request the brilliant young minds of this land to come up with a testing strategy. Stack Overflow for Teams is moving to its own domain! If the wolves ever outnumber the sheep on either side of the river, the wolves will overpower and eat the sheep. @noedne's answer from the beginning struck me as a good one, but I have recently come to the conclusion that it is also the optimal answer. W W W Following Andrew Williamson theme I wrote a program to experiment with different splits and I found a solution with. Every test we perform is the same 100 bits but this time the "1" represents which sheep are being tested. Let's see if this checks out for a 6 by 6 board. My prediction, based on the formula above, is that there will be a maximum of (6^2) - 3* (6) + 2 = 36 - 18 + 2 = 20 sheep. - two sheep then skip two Another hint as @Quuxplusone pointed out . At the intersection of every wolf combination and test there is a definite binary output of whether the test was positive or negative. Say Shaun is in group A but not B, and Shirley in B but not A. Then, we subtract that value from the total number of squares on the grid, n^2, which gives us a final expression for the max number of sheep on an n by n board: (n^2 ) - 3n + 2. Asking for help, clarification, or responding to other answers. That's @noedne's answer after his first update. @Quuxplusone, nice to see that you are still working :). Fortunately, you are good at spotting them. Work fast with our official CLI. This is The blood test riddle (number theory) by another name. . Args: initial_sheep: Number of sheep to start with initial_wolves: Number of wolves to start with sheep_reproduce: Probability of each sheep reproducing each step wolf_reproduce: Probability of each wolf reproducing each step wolf_gain_from_food: Energy a wolf gains from eating a sheep 9 months ago, Posted S S S S W 1 sheep, 1 wolf: (1,1) * */ private class state { private string bank; // the active bank where the farmer is currently // located private treeset left, right; // How to help a successful high schooler who is failing in college? Protect Sheep. 1 sheep, 0 wolves: (1,0) They don't appear to be. I also wondered whether it is possible to arrange the sheep in a cube instead of a grid, but I never managed to work it out. Assume, then, we want to optimize the "at most 1-sheep in common" strategy. Iterate through addition of number sequence until a single digit, Saving for retirement starting at 68 years old, Correct handling of negative chapter numbers. Now, if we have 3 wolves and 5 sheep, what is the total number of possible configurations of wolves and sheep we can have on a 5 by 5 grid so that all of the sheep survive? Are Githyanki under Nondetection all the time? In my answer above, I divide the sheep into groups of 10, which guarantees 5/10 positives and 5/10 negatives. At least I think that's what we have here. Iteration 2 suspects = 11110000000000000000000000000000 The blocks are square, so sheep per test = tests per block. Wolves = I1 suspects & I2 suspects & I3 suspects Is MATLAB command "fourier" only applicable for continous-time signals or is it also applicable for discrete-time signals? For example if wolves are $ \{22, 28, 56, 66, 77\} $ modulo $11$ groups tests will produce $\{0, 1, 6\}$ result. @IanMercer i didn't get it, can you elaborate more please with some codes and examples? (Under the spoiler tags above, I show one concrete example where $(n, k, t)$ is possible yet $(n, n-k, t)$ is not possible.). How we can create a graph from 3 lambs and 3 wolves? S S S S S This gives groups like the following (where 1 means the sheep is selected, 0 means it is not selected): In order to be certain, we need to repeat this 5 times, which is 50 tests. 21 | 3 | 63 The 11x11 blocks would allow you to solve a 121-sheep problem using 65 tests. The boat MUST be always carrying animals, which means you can't move an empty boat. rev2022.11.3.43005. Each of the 59 rows represents a test; each column represents an animal. Once it finishes iterating over all possible next states, it goes over each of the solutions stored in the solutions list and chooses the one with the least moves to the goal state, returning a list of tuples with all the moves used to solve the problem. . It would be interesting to try get this working for some of the simpler cases. But getting 6 blocks of 10x10 tests is quite impossible, according to the requirements of the 2nd strategy: tests within blocks have nothing in common with each other, and tests from anywhere have no more than 1 sheep in common. So this is how I fill in the chart representing best-possible scenarios. I've got my laptop working on the missing elements as we speak. i.e., I can collect bloods from any number of 'sheep' and mix them. A simple example (partly because I'm lazy, and partly because it wraps around too much) of 32 sheep with 3 wolves among them, which requires only 5 tests and 3 iterations: The problem is that 10 isn't prime and @noedne's pattern doesn't work for it. The boat, which carries the animals to the other side, can't carry more than 2. This still feels wildly inefficient. A wolf can eat a sheep if it has a direct path to it or is in same row, column, or diagonal as that sheep. C is quite a big matrix to inverse and I'm not sure what python would do with binary matrices. . If the letter V occurs in a few native words, why isn't it included in the Irish Alphabet? Here's a link to the challenge: https://www.codewars.com/kata/5c8bfa4. A meal takes a number of steps f, during which the wolf will not move. For example, using your 32 sheep example, if you have wolves in positions 30, 31, 32 - all tests for all iterations will be positive. T T . (Well, the edges are 0, and column 2 is lg n, and beyond a certain k the entries are all n-1. Each column is labeled by every test we have designed (hopefully less than 99). Lets start with our 4 by 4 grid. Without the diagonals, we wouldn't be able to tell the difference. First of all, does the government have enough funds to test 99 of the sheep? Heres your chance to be the hero of the story! Could you place 3 wolves and 2 sheep so that none of the sheep get eaten? Test 3. . Maybe it's baby wolves vs. adult rams. Alternatively, you know the location of all 5 wolves. 15 | 3 | 45 You may assume that the initial state of the problem will follow those rules (e.g. Site design / logo 2022 Stack Exchange Inc; user contributions licensed under CC BY-SA. I've been focusing on thinking of every test as being effectively random, without trying too hard to build specific patterns like @noedne and others had. I asked a question over on Math.SE, pointing to this puzzle, and @Elaqqad gave a very long answer with lots of links to math that might help. There is a conjecture on that page that a(10) = 2 which would translate to 4 total blocks for us. To learn more, see our tips on writing great answers. . Not really, no. See how fast you can solve this . "Least Astonishment" and the Mutable Default Argument. If you never experimented with other #'s of wolves you may never have noticed. I don't immediately see this sequence in the OEIS, which is surprising to me. What is a good way to make an abstract board game truly alien? From any state you can calculate all the new states you can reach (as I showed for the first step). There are exactly 96 blocks in Blocks. To start this activity, youre going to need two different types of objects (anything you have around the house will do 2 different colored blocks, quarters and pennies, etc.) Well, if there were just one wolf, then yes we could. Log into your existing Transtutors account. Posted Additionally, every time the shepherd goes back to fetch more animals, he must bring back one or two animals. 1. 18 | 3 | 54 How to generate them all? However, not every initial state will be solvable; there may be combinations of sheep and wolves that cannot be solved. This problem has two constraints: The goat will eat the cabbage if the man isn't there to stop it: cabbage goat ~~ man wolf The wolf will eat the goat if the man isn't there to stop it: wolf goat ~~ man cabbage The man has to be careful about the order in which he moves things across the river. I used my program to test this idea, and it does indeed check out. Note: I was also thinking of this problem and I realize that the above hints do not lead to any optimal solutions. A few wolves dressed up as sheep entered the territory of Shewo and started killing the sheep one by one. This matrix $M$ is 5-separable, and so we can use it to find 5 wolves among 96 animals using only 60 tests. Even if you managed it, at some point you would lose definition, after all the theoretical 9x9 block scenario isn't itself good for 100 sheep. - every alternate sheep It is important to determine all the possible movements of the current state, all delimited by the rules of the problem. $9 + 11 + 13 + 14 + 15 + 16 = 78$ groups. Anyway, I have no insight into how this particular structure was derived. He cannot leave the wolf with the sheep. S W S S S What is the size of the largest grouping of codes (the rows)? Thinking like a Wolf, a Sheep or a Firefly: Learning Biology through Constructing and Testing Computational Theories -- an Embodied Modeling Approach. @cag15 unfortunately you're right. 13 | 4 | 52 This problem gave me a little more trouble, but after messing around with the same types of configurations I used in Questions 1 & 2, I found this: If you flip and rotate this grid, you can come up with 7 more variations on the original configuration, which gives a total of 8 configurations. The wolves can't be more than the lambs, which means the lambs can be more than or equal to the wolves. This results in 7 * 5 = 35 tests being performed. The next strategy up would be to allow at most 2 sheep in common between tests this is where I was dabbling for a while until I realized how ineffective it was. we have the bitwise-OR of some 5 columns corresponding to wolves), then we can "decode" those results and deduce exactly which 5 animals (i.e. Warn the sheep in front of the wolf that it is about to be eaten. I have figured out a better way of solving this which I could motivate through hints. Take initiative and slaughter all 100. We get to use 99 tests instead of 100 because the last test becomes moot. Suppose we have 6 sheep, two of whom are wolves. 20 | 3 | 60 But we see it is inefficient, because there's a theoretical 10x10 block good for 101 sheep, and it will solve in 59 tests. All but one sheep are tested six times. This suggests a realistic answer is something closer to 50 tests, and that's without considering that tests are getting weaker all the time. From here you can travel to any node that meets the requirements. The base line is testing all 100 sheep, which guarantees 5/100 positives and the rest negative. Each block represents a set of tests that have no sheep in common with eachother. Bob is a farmer. Could we design a strategy to use the bare minimum? \end{cases} Having nothing else to go on, I will use it and I know the split for testing 13 sheep is roughly 49 negative outcomes for every 51 positive outcomes, and this still allows (barely) for 27 tests to encode all combinations. If you exclude the wolfs square from the row, column, and diagonal, you end up with the same number for each: n 1. 23 | 2 | 46 wolf! This series of 59 tests can identify the 5 wolves from among our 100 animals. Could you elaborate on how the number 39 is obtained. Thanks for contributing an answer to Stack Overflow! First, into 9 groups - sheep with numbers producing the same quotient when divided by $12$ go into the same group. The pasture is a rectangle consisting of R C cells. What is the difference between __str__ and __repr__? But suddenly they were struck by a danger. Or whats the smallest grid that can safely fit 4 wolves and 6 sheep? 2 days ago, Posted Iteration 1 suspects = All sheep The other five ways are done modulo $11, 13, 14, 15, 16$ respectively - for each way sheep with numbers producing the same remainder when divided by the corresponding modulus go to the same group. Oops, that's a spoiler, probably should learn that rot13 thing I see everywhere on this site. next step on music theory as a guitar player. In the beginning it looked easy till i stopped with these difficult circumstances: The wolves can't be more than the lambs, which means the lambs can be more than or equal to the wolves. 17 | 3 | 51 Try a number of random tests, keep the one that worked the best (minimizes the metric). Now, the inverse shoud be $C^{-1}_{\mathrm{left}}=(C^T\cdot C)^{-1}\cdot C^T$. @ppgdev suggested I look at this question, and they seem to be right a lot so here I am. Sounds good for me, but still doesn't give me any progressing because the problem still the same, how to implement it or write it as a Python code to pass this graph to the algorithm so the algorithm can solve it? Below is the python code for the given problem statement: # There is a river with a boat and a bank on each side; that's how rivers work. @JyotishRobin, while I'm definitely not requesting to see your solution yet, I'm wondering: do you believe you know a solution involving 98-or-fewer tests? This results in a false positive on all of the sheep. Write your answers in Python code (or; Question: You are to formulate the "River Crossing" problem (a gentleman, two sheep, and a wolf) to solve using a search algorithm. So this is evidence that perhaps the original puzzle can be done in fewer than 99 tests! We have 3 lambs and 3 wolves in a side and we have to let them pass the river to the other side. Also more details on how the testing is done can help the reader understand it better. Stack Overflow for Teams is moving to its own domain! Each wolf being a "1" and regular sheep are "0". @Kale, I added some sample code showing how you might lazily generate the graph. 7 | 9 | 63 / | | | the diagonals in one direction. Alas, I still cannot find a way. With this distribution function, we can keep adding iterations to narrow down the suspected sheep, until the number is less than or equal to 5: The problem is that this suggests the amount of tests to break up "mostly pairs" is nearly equivalent with the number of tests it took to get there. Only one or two animals can be crossed in the boat at a time, not less nor more. A lot of this is beyond my reasoning. - eight sheep then skip eight (With each iteration, shuffle each row across to the left according to its row number, e.g row 0 stays where it is, row 1 gets shuffled 1 to the left, row 2 gets shuffled 2 to the left). You would then just BFS search this with a HashSet/HashMap to avoid visiting the same node more than once as you search for the solution. In case no solution can be achieved, it returns an empty list. Why do I get two different answers for the current through the 47 k resistor when I do a source transformation? Then we'd have to find a left inverse of $C$ (using the same matrix operations as define above). Test 1. But crossing the river by boat, the farmer could carry only himself and a single one of his purchases: the wolf, the goat, or the cabbage. They arrive at one side of a river . Browse other questions tagged, Start here for a quick overview of the site, Detailed answers to any questions you might have, Discuss the workings and policies of this site, Learn more about Stack Overflow the company. W S S I think I'm burned out on it now, so I'm going to stop. Every time we add a test to my list and calculate the binary outputs, we must solve for the following metrics: The problem is exactly equivalent to: Find the smallest 5-separable matrix with exactly 100 columns. Each row of binary outputs can be thought of as a code that will help us determine the uniqueness of each of the 75 million+ combinations. A 01010101010101010101010101010101 Shift all the sheep to the right, carry the last sheep around to the front, and repeat the same steps 4 times. Test 6. . Ignore for a moment the absurdity of wolves needing to outnumber sheep in order to overpower them. If we have 2 wolves and three sheep, can we find a configuration of wolves and sheep where none of the sheep get eaten? Can the lion protect the sheep from the wolves? Test 2. If more than one sheep try to occupy the same cell, one of the sheep's moves fails and the sheep will not move. QGIS pan map in layout, simultaneously with items on top. rot13: Vs gurer ner jbyirf ng (1,5), (2,3), (3,2), naq (4,4), gura (1,4) jvyy fubj hc nf n jbys ab znggre jung, fvapr ur'f va gur fnzr ebj, pbyhza, naq qvntbany nf n jbys. Ooops, slaughtered a few lambs there ;) Yes, they were meant to be legal states. Privacy Policy, Submit your documents and get free Plagiarism report, By creating an account, you agree to our terms & conditions, We don't post anything without your permission. - 2 2 - 3 171-209. I was thinking about this question for a long time and couldn't figure out how to solve it. The following code explores just a single way. Turns out that every possible combination of $5$ wolves ($75287520$ - total) produces a different result when all 6 ways are taken into account. In some scenarios, this solution will only narrow it down to the 5 wolves and four sheep, This matches what I was thinking. You will submit the code for solving the problem to the Mini-Project 1 assignment in Gradescope. How to solve sheep and wolves puzzle in python? suspects = 100 * ((5 / 10) ^ iterations) We always need one block more than the # of wolves. The number of wolves on either side of the river cannot outnumber the sheep. Do wolves in positions 2, 60, and 69 make all tests pass? @noedne's pattern isn't itself a requirement, though, and we shouldn't exclude the possibility that 10x10 could still somehow work. He made the following announcement: By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. And, as Erick points out in the comments, some of these aren't legal according to the rules and should not have been in the graph. You might say, well, that just means I can conclude that the wolves are at those positions. There is a bitwise "and" operation that should be fast and will let us compare each number, outputing a 0 if both have nothing in common. . Seems convincing to me. I can't tell what's going on in the first line of your 'for' statement, but the main problem I see is that you are testing for a pile of one at the end rather than a remainder of one at each . It only takes a minute to sign up. Simple wolf and sheep simulation written in Python - GitHub - Muszeczka/Wolf-and-sheep: Simple wolf and sheep simulation written in Python But I think there is one more step left of how you test. I number $100$ sheep by $1, 2, 3, , 100$ and split them in 6 different ways. Creates the initial state with initial sheep and wolves as input parameters. tests of nine sheep on all but one sheep according to the illustrated patterns: The two important properties exhibited are. It's been "a few weeks", and it seems unlikely to me that the question is going to draw new attention. @noedne swapped out just one block, and it didn't break the requirements. $$ for each of the 96 blocks (animals) and 60 points (tests). If you run my code you can see the breakdown on each round of taking away 1/5 of the coconuts and giving one to the monkey. If the letter V occurs in a few native words, why isn't it included in the Irish Alphabet? Between weakening tests and that the problem is nearly twice as hard as it seems on the surface, I would say 63 looks really good. What's a good single chain ring size for a 7s 12-28 cassette for better hill climbing? Sorry I'm late to the party but this question is fun and I think I have something to add but not an answer, sorry. 14 | 3 | 42 Why does Q1 turn on and Q2 turn off when I apply 5 V? That leaves two more locations we could put the wolf: in the corner, or on the edge. Iteration 1 suspects = 11111111111111111111111111111111 Suppose each node has a state on it representing how many lambs and wolves are on the left bank and whether or not the boat is on that bank ('0' for left, '1' for right). By doing this, I've halved the number of sheep to search with only 10 samples. Every time you add a wolf to the mix, you need an additional block to make the combinations unique. This establishes the claim. 10 | 5 | 50 T . To get up to 100 animals, we take our 96 Blocks and add the following 8 more: This modification increases the size of Blocks from 96 to 104, and it preserves the magic "PBD-ness" of our block design (again, I have no insight as to why this is so), but it doesn't actually add any more points we still have 60 points in our "alphabet." I don't have a solution, but I might have an approach. What is the difference between the following two t-statistics? What is the difference between Python's list methods append and extend? - four sheep then skip four If the results only show you 4 wolves, the 5th wolf is the untested animal. Python also have a more in depth guide on how to properly format code, see PEP 8. . This solution as presented can give ambiguous results. 4. I also notice that the situation is not symmetrical: we may know a way to find $k$ wolves among $n$ sheep using $t$ tests, but that won't help us at all to find $n-k$ wolves among $n$ sheep. . The advanced strategy is to let us allow tests that have at most 1 sheep in common with any other test. This indicates that we must use at least 27 tests, no matter what that's just basic information theory. Site design / logo 2022 Stack Exchange Inc; user contributions licensed under CC BY-SA. The wise leader of the wolf pack, Magra, announces that he'll retire soon, and that two candidates must fight for the rite of succession. solutions: A list to keep track of the solutions that have already been seen. On his way home, the farmer came to the bank of a river and rented a boat. T T . Any idea how this could relate to OP's hint about binary representations? I can get 3 blocks of 10x10, and maybe I would believe somebody could get a 4th block with enough effort maybe. In the first variation, the "sheep-wolves" version, wolves and sheep wander randomly around the landscape, while the wolves look for sheep to prey on. You signed in with another tab or window. There was a problem preparing your codespace, please try again. Thinking out loud, not a solution yet, but spoilery enough that I didn't want to put it in a comment: There are ${100\choose 5} > 2^{26}$ possible arrangements of the 5 wolves among 100 sheep. (This breaks a tie) I have no idea, but I suspect magic. Now when we bring the front sheep of each iteration back to the end to re-align them: That often happens for practical or ethical reasons. Iteration 3 suspects = 11000000000000000000000000000011 8 | 7 | 56 The edge is every valid transition from each node regarding the above limitations. Description: Wolves have been reintroduced to Great Britain. E | Positive | Negative | Negative By the pigeonhole principle, at least one sheep in Group B shares more than one test with Shaun, contradicting property 2. With all these definitions and elements determined, the solve method was created to follow the subsequent steps: This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. In C, why limit || and && to evaluate to booleans? The basic strategy that yields the 99-test solution isn't complicated. What is the best algorithm for overriding GetHashCode? However, not every initial state will be solvable; there may be combinations of sheep and wolves that cannot be solved. As a quick example, after arranging 25 sheep into a grid, we have the following: In case the next state is the goal state, it stores it in the solutions list. Each test $t$ combines blood from the animals $i$ where $M_{t,i} = 1$. Stack Exchange network consists of 182 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. The gist of it is that @noedne was using 9x11 blocks, which covers the required 99 sheep in testing. Weve found the maximum number of sheep on a 4 by 4 and a 5 by 5 board, but can we find an expression for the max number of sheep on any n by n board (still with 1 wolf)? T . Once the next state passes that test, it checks if it has already been seen in the seen_states and if it is not the final solution, it appends it in the seen_states set. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. 9 | 6 | 54 Instead of thinking of the state (on each node) as being just lambs and wolves, add the boat into the state vector. Then if I test the mixture, I will get a positive result if the mixture contain blood from any wolf. UPDATE: We can remove any single test T to improve the total by one.

Imperious - Races Of Skyrim Compatibility, 50 Kg Grain Storage Containers, Picture By Picture Samsung, Hacktivist Characteristics, Realistic Drawing Classes Near Me, Curtain Calculation Formula, Jimma Aba Jifar Fc Vs Defence Force Sc, Gantt Chart Library React,