Computational Models

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Name three fields where computational models are widely used.

Physics, Engineering, and Chemistry

Other than supplementing experiments, what is another application of computational models?

They can replace experiments.

Beyond predicting the future, what is another application of computational models?

To model and understand events that have occurred in the past.

Identify the components of an optimization problem.

<p>An objective function and a set of constraints.</p> Signup and view all the answers

Describe what the objective function represents in an optimization problem.

<p>It represents what we want to maximize or minimize.</p> Signup and view all the answers

Explain what the constraints represent in an optimization problem.

<p>They represent the requirements that must be satisfied.</p> Signup and view all the answers

In the knapsack problem, what serves as the objective function?

<p>The total value of the stolen items.</p> Signup and view all the answers

In the knapsack problem, what serves as the constraint?

<p>The stolen items must be able to be carried in the knapsack.</p> Signup and view all the answers

What are the two variants of the knapsack problem?

<p>The 0/1 knapsack problem and the fractional knapsack problem.</p> Signup and view all the answers

Describe the key distinction between the 0/1 knapsack problem and the fractional knapsack problem.

<p>In the 0/1 knapsack problem, an item is either taken or left; in the fractional problem, any fraction of the item can be taken.</p> Signup and view all the answers

Explain what a greedy algorithm is.

<p>One in which the best remaining move is made at each step.</p> Signup and view all the answers

Is a greedy algorithm guaranteed to give the optimal solution to the knapsack problem?

<p>No.</p> Signup and view all the answers

What is the total value of stolen items if "best" is taken to mean the least heavy?

<p>$170</p> Signup and view all the answers

What is the total value of stolen items if "best" is taken to mean the most valuable?

<p>$200</p> Signup and view all the answers

What is the total value of stolen items if "best" is taken to mean the highest value-to-weight ratio?

<p>$255</p> Signup and view all the answers

With respect to implementing a greedy algorithm, why do we use the inverse of the weight?

<p>So that “best” will correspond to the largest inverse weight, similar to the largest value and largest value-to-weight ratio.</p> Signup and view all the answers

What is the purpose of the keyFunction when implementing the greedy algorithm?

<p>It allows different definitions of &quot;best&quot; to be used.</p> Signup and view all the answers

What is the algorithmic complexity of Python's sort?

<p>$O(n log n)$ where n is the length of items.</p> Signup and view all the answers

What is the algorithmic complexity of the loop in the implemented greedy algorithm?

<p>$O(n)$</p> Signup and view all the answers

What is the algorithmic complexity of the implemented greedy algorithm?

<p>$O(n log n)$</p> Signup and view all the answers

In the brute force algorithm for the knapsack problem, what does the vector $V$ represent?

<p>It indicates which items are stolen by the burglar.</p> Signup and view all the answers

According to the brute force algorithm for the knapsack problem, what is being maximized?

<p>The sum of V[i] * I[i].value</p> Signup and view all the answers

What guarantees that the Brute Force algorithm will find the optimal solution?

<p>It tries all possible combinations of items.</p> Signup and view all the answers

Why is the brute force algorithm impractical for a large number of items?

<p>Because the number of possible combinations grows exponentially.</p> Signup and view all the answers

In the context of the brute force algorithm, what does a bit string represent?

<p>A subset of a set.</p> Signup and view all the answers

What is the algorithmic complexity of the outer loop in the chooseBest function?

<p>$O(2^n)$ where n is the number of items.</p> Signup and view all the answers

What is the overall algorithmic complexity of the chooseBest function?

<p>$O(n2^n)$</p> Signup and view all the answers

What type of solution does a greedy algorithm typically achieve?

<p>A locally optimal solution.</p> Signup and view all the answers

What type of solution does a brute force algorithm typically achieve?

<p>A globally optimal solution.</p> Signup and view all the answers

What is an advantage of using a greedy algorithm over a brute force algorithm?

<p>Greedy algorithms are easier to implement and have shorter run times.</p> Signup and view all the answers

For which type of knapsack problem is a greedy algorithm guaranteed to find an optimal solution?

<p>Fractional (or continuous) knapsack problem.</p> Signup and view all the answers

For the fractional knapsack problem, how should a greedy algorithm decide which item to take?

<p>It should always take as much as possible of the item with the highest remaining value-to-weight ratio.</p> Signup and view all the answers

Outline the initial steps a burglar should take in solving a fractional knapsack problem.

<p>He should start off by filling the knapsack with as much gold dust as he can.</p> Signup and view all the answers

In the fractional knapsack problem, after taking as much gold dust as possible, what should the burglar do next?

<p>If all the gold dust has been taken and the weight of the knapsack is still under the maximum, he should continue filling the knapsack with as much silver dust as he can.</p> Signup and view all the answers

Flashcards

Computational model

Computer programs that simulate complex systems, used in various fields.

Optimization model

A type of computational model used to find the best solution to a problem.

Objective function

A function to maximize or minimize in an optimization problem.

Constraints

Conditions that must be satisfied in an optimization problem.

Signup and view all the flashcards

Knapsack problem

A classic optimization problem about selecting items with max value under weight constraints.

Signup and view all the flashcards

0/1 knapsack problem

Variant of knapsack problem where items are either fully taken or left.

Signup and view all the flashcards

Fractional knapsack problem

Variant where items can be divided and fractions can be taken.

Signup and view all the flashcards

Greedy algorithm

Algorithm where the locally best choice is made at each step.

Signup and view all the flashcards

Brute force solution

Tries all possible combinations to find the optimal solution.

Signup and view all the flashcards

Power set

Set of all possible subsets of a set.

Signup and view all the flashcards

Greedy optimal solution in Fractional Knapsack

In fractional knapsack, always take as much as possible of the item with highest value-to-weight ratio.

Signup and view all the flashcards

Locally optimal solution

A solution that is optimal within a limited scope or immediate choices.

Signup and view all the flashcards

Globally optimal solution

The best possible solution out of all feasible solutions.

Signup and view all the flashcards

Study Notes

  • Computational models use computer programs to simulate and study complex systems.
  • These models are used in physics, engineering, chemistry, biology, economics, psychology, cognitive science, and computer science.
  • Computational models can augment or replace experiments, especially for systems too complex for analytical study.

Types of Computational Models

  • Optimization models
  • Statistical models
  • Simulation models

Uses of Computational Models

  • Model and understand past events such as modelling how the climate has changed over the last several millennia.
  • Make predictions about the future such as predicting the future climate.

Optimization Problems

  • Can be solved by mapping the problem onto an optimization problem.
  • An optimization problem is used to find the best solution to a problem.
  • "Best" solution can be defined as biggest, smallest, most, fewest, fastest, or least expensive.
  • If one knows the solution to the optimization problem, it can be used to solve the original problem.
  • Consist of two parts: an objective function and a set of constraints that must be satisfied.
  • Objective function can be maximized or minimized.
    • Examples include minimizing travel time or airfare between two locations.
  • Constraints are conditions that must be met.
    • Examples include having a maximum budget, needing a travel time of less than a set amount of hours, or having to arrive before a set time.
  • Solved by services like Google Maps and Expedia.

Knapsack Problem

  • Classic optimization problem involving a burglar with a knapsack.
  • The burglar seeks to maximize the total value of stolen items.
  • The house has items of greater value than the burglar can carry.
  • The objective function is the total value of the stolen items.
  • The constraint is that the stolen items must be able to be carried in the knapsack.

Knapsack Problem Variants

  • 0/1 Knapsack: Burglar either takes an item entirely or leaves it.
  • Fractional/Continuous Knapsack: Burglar can take any fraction of an item.

0/1 Knapsack Example

  • A burglar's knapsack can hold at most 20 kilograms worth of items.
  • House contains the following items: clock, painting, radio, vase, book, and computer.
  • Their values, weights and value/weight ratios vary.
    • Clock : value of 175, weight of 10, ratio of 17.5
    • Painting: value of 90, weight of 9, ratio of 10
    • Radio: value of 20, weight of 4, ratio of 5
    • Vase: value of 50, weight of 2, ratio of 25
    • Book: value of 10, weight of 1, ratio of 10
    • Computer: value of 200, weight of 20, ratio of 10

Greedy Algorithms

  • At each step, the best remaining move is made
  • This is the simplest way to find an approximate solution to the knapsack problem.
  • The burglar chooses the best item first, then the next best, until the knapsack is full or no other items are possible.
  • "Best" item can be most valuable, least heavy, or highest value-to-weight ratio.
  • Different definitions of "best" can yield different solutions.
  • Solutions are not guaranteed to be optimal.

Greedy Algorithms according to Value

  • If "best" is most valuable, only the computer is taken.
    • It has a value of $200 and weight of 20 kg, so there is no available weight.
    • Total value of stolen items is $200.

Greedy Algorithms according to Weight

  • If "best" is least heavy, items will be taken in the following order - book, vase, radio, and painting.
    • The book has a value of $10 and weight of 1 kg leaving 19 kg of available weight.
    • The vase has a value of $50 and weight of 2 kg leaving 17 kg of available weight.
    • The radio has a value of $20 and weight of 4 kg leaving 13 kg of available weight.
    • The painting has a value of $90 and weight of 9 kg leaving 8 kg of available weight.
    • Total value of stolen items is $170.

Greedy Algorithms according to Value to Weight Ratio

  • If "best" is highest value-to-weight ratio, items will be taken in the following order - vase, clock, book, and radio
    • The vase has a value of $50 and weight of 2 kg leaving 18 kg of available weight.
    • The clock has a has a value of $175 and weight of 10 kg leaving 8 kg of available weight.
    • The book has a has a value of $10 and weight of 1 kg leaving 7 kg of available weight.
    • The radio has a has a value of $20 and weight of 4 kg leaving 3 kg of available weight.
    • Total value of stolen items is $255.

Representing Items with Python Class

  • Class name called Item(object)
  • It will use the following def functions:
    • __init__(self, name, value, weight)
    • getName(self)
    • getValue(self)
    • getWeight(self)
    • __str__(self)

Representing Definitions of "Best" using Functions

  • Uses Python, and includes:
    • def value(item)
    • def weightInverse(item)
    • def density(item)
  • The smallest weight will have the largest inverse weight, similar to the largest value and largest value-to-weight ratio.

Implementing the Greedy Algorithm

  • Can be implemented as follows in Python:
    • def greedy(items, maxWeight, keyFunction)
    • keyFunction maps elements of items to numbers
  • The items are then sorted, and a result list and total values are assigned
  • The keyFunction parameter allows for different definitions of "best" to be used

Algorithmic Complexity

  • Python's sort is roughly O(n log n) where n is the length of items.
  • The loop is O(n).
  • The whole function is O(max(n log n, n)) = O(n log n).

Testing the Greedy Algorithm

  • We can build the list of items in the house using the code below:
    • def buildItems():
    • Includes function defs for names, values, weights and the items in a list

Testing the Greedy Algorithm with Provided Item Ordering

  • Test the greedy algorithm with a provided item
    • def testGreedy(items, constraint, keyFunction)

Measure of "Best" Algorithm

  • Can test the greedy algorithm with each measure of "best" using the code below:
    • def testGreedys(maxWeight=20)

Brute Force Algorithm

  • The 0/1 knapsack problem can be formalized by:
    • Representing each item by a pair, (value, weight).
    • The knapsack has a maximum weight capacity, w.
    • A vector, I, of length n, that represents the set of available items, where each element of the vector is an item.
    • A vector, V, of length n, is used to indicate which items are stolen by the burglar. -If V[i] = 1, then item I[i] is taken. -If V[i] = 0, then item I[i] is not taken.
  • The goal is to find a V that maximizes the sum of V[i] * I[i].value.
  • It needs to be subject to the constraint that the sum of V[i] * I[i].weight is less than or equal to w.

Brute Force Algorithms cont.

  • Force solution tries all possible combinations of items to find the best one
  • All possible combinations of items are generated as the power set of the set of items.
  • Combinations of items whose total weight exceeds the allowed weight are removed.
  • Returns the combination of items with the highest total value among the remaining combinations.
  • The brute force algorithm guarantees the optimal solution, but is too slow for large sets due to 2^n possible combinations.
    • Represented using a bit string where 1 indicates element is in the subset, and 0 indicates element is not.
  • We can obtain the bit string corresponding to a non-negative integer using the code below:
    • def getBinaryRep(n, numDigits)

Brute Force Algorithm Power Sets

  • The power set of a set of items can be obtained
    • def genPowerset(L)

Best Combination of Items

  • The best combination of items from the power set can be located with a set maximum weight:
    • def chooseBest(pset, maxWeight, getVal, getWeight)
    • getVal and getWeight parameters get the value and weight of items

Brute Force Algorithm cont.

  • Given chooseBest(pset, maxWeight, getVal, getWeight) from before:

    • The outer loop is O(2^n) where n is the number of items, since the power set has 2^n elements.
    • The inner loop is O(n).
    • The whole function is O(n 2^n).
  • Finds the optimal solution for a set of items :

    • def testBest(maxWeight = 20)
  • The 0/1 knapsack problem is inherently exponential in the number of items, with the brute force algorithm presented as O(n 2^n).

Greedy Algorithms vs Brute Force Algorithms

  • The greedy algorithm makes the "best" local choice at each step resulting in a locally optimal solution.
  • This does not guarantee a solution that is globally optimal
  • The brute force algorithm is guaranteed to find the globally optimal solution.
  • Greedy algorithms are easier to implement and have shorter run times.
  • Often greedy algorithms are used in practice when a "good enough" solution is acceptable.

Fractional Knapsack Problem

  • A greedy algorithm is guaranteed to find an optimal solution.
  • Take as much as possible of the item with the highest remaining value-to-weight ratio.
  • The burglar should start with gold dust, then silver dust, then raisins until the knapsack is full.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser