[Solved] FIT2004-Assignment 2 Dynamic Programming

$25

File Name: FIT2004-Assignment_2_Dynamic_Programming.zip
File Size: 376.8 KB

SKU: [Solved] FIT2004-Assignment 2 Dynamic Programming Category: Tag:
5/5 - (1 vote)
  1. Think of test cases that you can use to check if your algorithm works.
    • Use the edge cases you found during the previous phase to inspire your test cases.
    • It is also a good idea to generate large random test cases.
    • Sharing test cases is allowed, as it is not helping solve the assignment.
  2. Code up your algorithm, (remember decomposition and comments) and test it on the tests you have thought of.
  3. Try to break your code. Think of what kinds of inputs you could be presented with which your code might not be able to handle.
    • Large inputs
    • Small inputs
    • Inputs with strange properties
    • What if everything is the same?
    • What if everything is different?
    • ..

Documentation

For this assignment (and all assignments in this unit) you are required to document and comment your code appropriately. This documentation/commenting must consist of (but is not limited to)

  • For each function, high level description of that function. This should be a one or two sentence explanation of what this function does. One good way of presenting this information is by specifying what the input to the function is, and what output the function produces (if appropriate)
  • For each function, the Big-O complexity of that function, in terms of the input. Make sure you specify what the variables involved in your complexity refer to. Remember that the complexity of a function includes the complexity of any function calls it makes.
  • Within functions, comments where appropriate. Generally speaking, you would comment complicated lines of code (which you should try to minimise) or a large block of code which performs a clear and distinct task (often blocks like this are good candidates to be their own functions!).

1 Game Master

You and your friends are playing a table-top role playing game, and you are the game master. You want to design an encounter, and you have a certain difficulty target that you want to reach. You also have a list of monsters, and you want to select a group of monsters whose difficulty ratings sum up to the target difficulty.

You are interested in finding out how many different possible encounters there are which satisfy this requirement. To solve this problem, you will write a function count_encounters(target_difficulty, monster_list).

1.1 Input

target_difficulty is a non-negative integer.

monster_list is a list of tuples. Each tuple represents a type of monster. The first value in each tuple is a string, which is the name of the type of monster. The second value is a positive integer, representing the difficulty of that particular type of monster.

1.2 Output

count_encounters returns an integer, which is the number of different sets of monsters whose difficulties sum to target_difficulty. A type of monster may be used more than once in an encounter.

1.3 Example

target_difficulty = 15 monster_list = [(bear, 5), (imp, 2), (kobold, 3), (dragon, 10)] print(count_encounters(target_difficulty, monster_list)) >>> 9

In the above example, the possible encounters are:

1 dragon, 1 bear

1 dragon, 1 kobold, 1 imp

3 bear

2 bear, 1 kobold, 1 imp

1 bear, 2 kobold, 2 imp

1 bear, 5 imp

5 kobold

3 kobold, 3 imp

1 kobold, 6 imp

Your answer does not need to compute these possible sets of monsters, this list is provided to help you understand the example answer of 9

1.4 Complexity

count_encounters should run in O(DM) where

  • D is the value of target_difficulty
  • M is the length of monster_list

2 Greenhouse

You are a gardener in charge of a greenhouse. You are growing a variety of exotic plants as decoration for a party. You want to maximise the chance that all the plants are grown by the day of the party. In order to cause plants to grow more quickly, you can put sun lamps above the plants to give them more nutrients.

You have calculated the probability that each plant will be ready on time, based on the number of lamps you assign to it. You cannot move the lamps around once you assign them to a plant. You want to maximise the chance that all the plants are fully grown by the day of the party. To solve this problem, write a function best_lamp_allocation(num_p, num_l, probs)

2.1 Input

num_p and num_l are positive integers representing the number of plants and lamps respectively.

probs is a list of lists, where probs[i][j] represents the probability that plant i will be ready in time if it is allocated j lamps. Values in probs are floats between 0 and 1 inclusive.

Plants do not always grow faster with more light, so it is possible for the probabilities to decrease as well as increase, as the number of lamps increases. In other words, the lists within probs may not be sorted ascending.

2.2 Output

best_lamp_allocation returns an float, which is the highest probability of all plants being ready by the party that can be obtained by allocating lamps to plants optimally.

2.3 Example

probs = [[0.5, 0.5, 1],[0.25,0.1,0.75]] best_lamp_allocation(2,2,probs)

>>> 0.375

probs = [[0.5, 0.75, 0.25],[0.75,0.25,0.8]] print(best_lamp_allocation(2,2,probs))

>>> 0.5625

2.4 Explanation of example

In the first example, we have 2 lamps to use. If we assign 0 lamps to plant 0, it has a 0.5 probability of being ready. We would need to assign the full 2 lamps to it to improve its chance (to 1). We have 2 lamps left, so the best thing to do is assign both to plant 1, for a probability of 0.75. This gives an overall probability of 0.75*0.5=0.375.

We again have 2 lamps and 2 plants, but with different probabilities. This time, the most efficient thing is to not use all the lamps! Giving 1 lamp to plant 0 and 0 lamps to plant 1 give a probability of 0.5625. It would be ideal to give 1 lamp to plant 0 and 2 lamps to plant 1, but we do not have 3 lamps so this is impossible.

2.5 Approach

A good place to start would be answering one or both of the following questions:

  • Come up with an idea for what your memo array looks like. What are its dimensions? What values will be stored in each cell (i.e. what are the overlapping subproblems)
  • Come up with a recursive solution to this problem. If we have solved all previous (what does previous mean here?) subproblems, how can we use that information to solve the current subproblem?

To help with the above, here are some things to think about:

  • Which memo cells can we fill in trivially (i.e. with no reference to smaller subproblems)?
  • If we are considering how many lamps to give to plant i, and we decide to give it x lamps, what is the maximum number of lamps we can give to plants 0,1,i 1? When is the answer num_lx? When is it not num_lx?
  • Having answered the question above, what does this tell us about the subproblems that we should be solving?
  • What options do we have to try while solving each subproblem?
  • In What order should we traverse the cells of the memo array?

2.6 Complexity

best_lamp_allocation should run in O(PL2) time and O(PL) space, where P is num_p, and

L is num_l

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] FIT2004-Assignment 2 Dynamic Programming
$25