introduction knowledge engineering session 2 ronald westra westra@math.unimaas.nl

Post on 18-Dec-2015

219 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Introduction Knowledge Engineering

Session 2

Ronald Westrawestra@math.unimaas.nl

Go fish

You plan to go fishing and plan to take the following fish food items with you:

1. Magic bread: 6 cl per unit2. Cookies: 6 cl per unit3. Fish Lollies: 4 cl per unit4. Fish gum: 4 cl per unit5. Fish nuggets: 4 cl per unit6. Glow worms: 1 cl per unit7. Black worms:1 cl per unit8. Red worms: 1 cl per unit

Go fish (part 2)

With each item, the number of fish you expect to catch per item (not per unit!) is as follows:

1. Magic bread: 112. Cookies: 113. Fish Lollies: 7 4. Fish gum: 7 5. Fish nuggets: 66. Glow worms: 17. Black worms:1 8. Red worms: 1

Go fish (part 3)

• Your knapsack has capacity and can therefore contain no more than 14 cl of items. You want to maximize the number of fish you expect to catch. You can choose only one item of each type.

Real life problem: Which items do you select?

Model & Solution please…

catch per item

• Magic bread: 11• Cookies: 11• Fish Lollies: 7 • Fish gum: 7 • Fish nuggets: 6• Glow worms: 1• Black worms:1 • Red worms: 1

fish food items

• Magic bread: 6 cl per unit• Cookies: 6 cl per unit• Fish Lollies: 4 cl per unit• Fish gum: 4 cl per unit• Fish nuggets: 4 cl per unit• Glow worms: 1 cl per unit• Black worms:1 cl per unit• Red worms: 1 cl per unit

The hacker’s solution

Magic bread & cookies give 11/6 expected fish per cl, which is more than the others. Therefore, I take them. The rest I fill up with worms…

My number of expected fish: 2*11 + 2*1= 24.

Can we model the problem?

Consider the following state definition.

Any solution S can be modelled as an 8 dimensional (0,1) vector (*,*,*,*,*,*,*,*), where the i-th element has value 1 if the item is contained in the knapsack, and 0 otherwise. The value v(S) of solution S equals the corresponding number of expected fish.

A solution is called feasible, if its content doesn’t exceed the capacity of the knapsack.

Solutions which are not feasible are called infeasible

Solution method

Define max = (0,0,0,0,0,0,0,0), Define maxValue = 0.Lexicographically generate all solutions S:

S = (0,0,0,0,0,0,0,0), (0,0,0,0,0,0,0,1)…. (1,1,1,1,1,1,1,1)

For each S:• Check whether S is feasible by seeing whether the sum of

the items doesn’t exceed the knapsack Capacity • If S is feasible, determine v(S). If v(S) > maxValue

Set max = S, and set maxValue = v(S).Output S, v(S).

How many steps does the solution method take?

2*2*2*2*2*2*2*2 = 28 = 256.

What if there were 16 items?

What if there were 100 items?

Model 2Magic bread

Cookies

Fish Lollies

Gums

Nuggets

Glow worms

Black worms

Red worms

cl

Model 2 (cont.)

Cookies

Fish lollies

No cookie in the knapsack:Length 0

Cookie in the Knapsack:Length 11

Occupied space

Model 2

Magic bread

Cookies

Fish Lollies

Gums

Nuggets

Glow worms

Black worms

Red worms

cl

Solution method

Find the longest path from the red vertex to the yellow vertex

Beware: Finding a longest path in general is much trickier than finding a shortest path!!!!

Graphical vizualization

Do you know a simple way how to find a longest path – I don’t….

However….

In our special knapsack graph, the following labelling algorithm works:

Color vertices with incoming arcs blue.Color vertices without incoming arcs orange

(All the time it will hold that orange vertices have no incoming arcs, and blue vertices have incoming arcs….)

Set all vertex labels to zero.

Model 2

Magic bread

Cookies

Fish Lollies

Gums

Nuggets

Glow worms

Black worms

Red worms

cl

Repeat the following step until the yellow vertex is red

Take any orange node vConsider all blue nodes u, such that there is an

arc from v to u.If l(u) < l(v) + d(v,u) (we have found a longer path) : set l(u)

equal to l(v) + d(v,u)

Eliminate arc (v,u).

If all u has no incoming arcs anymore, change the color of u from blue to orange.

Change the color of v from orange to red.

After 1 step

Magic bread

Cookies

Fish Lollies

Gums

Nuggets

Glow worms

Black worms

Red worms

cl

How many vertices in this graph?

(# items +1)* (knapsack cl + 1)

9*15 = 135.

What if there were 16 items?

17 * 15 = 255.

What if there were 100 items?

101 * 15 = 1515

Further analysis

How are the algoritms effected if the content of all items and the knapsack is multiplied by a factor of 1000?

The hacker’s algorithm is not effected

The algorithm of the first model is not effected…

The algorithm of the second model is 1000 times slower – this is not a nice property!!!!!

Which algorithm is better???

1. The Hacker’s algorithm is fast, but doesn’t necessarily return the optimal solution (it is approximate)

2. The second method is exponential in the # of items, but returns an optimal solution (it is exact)

3. A third method – integer programming, not discussed here – is even linear in the knapsack size, and returns an optimal solution (it is exact)

What have we learned so far?

1. We don’t solve problems by starting to find a solution method: we start by finding an appropriate model.

2. We solve models using the corresponding solution methods that are developed by our ancestors!

The bad news

Our ancestors have not been able to find fast and exact solution methods for every model.

For some problems it is not known whether a fast and exact method exists.

2 Thieves

Two thieves are sitting with there stolen property, which consists of a set items 1,2,3….m, all watches and jewelry. The thieves know the value v(i) of each of the items i=1,2…,m. The thieves strive to find a division of the set of items into 2 sets, one for each thief, such that the sets are of the same value. (the value of a set is the sum of the values of the items in the set.)

Can you handle (model) this problem for them?)

?

cl

Exercises for next time

Solve the knapsack problem (that is check the hacker’s solution and the optimal solution in the graph model)

Solve the 2 Thieves problem (that is find an appropriate model and solution algorithm).

top related