26
Introduction Knowledge Engineering Session 2 Ronald Westra [email protected]

Introduction Knowledge Engineering Session 2 Ronald Westra [email protected]

Embed Size (px)

Citation preview

Page 1: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

Introduction Knowledge Engineering

Session 2

Ronald [email protected]

Page 2: Introduction Knowledge Engineering Session 2 Ronald Westra westra@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

Page 3: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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

Page 4: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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?

Page 5: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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

Page 6: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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.

Page 7: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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

Page 8: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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).

Page 9: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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?

Page 10: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

Model 2Magic bread

Cookies

Fish Lollies

Gums

Nuggets

Glow worms

Black worms

Red worms

cl

Page 11: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

Model 2 (cont.)

Cookies

Fish lollies

No cookie in the knapsack:Length 0

Cookie in the Knapsack:Length 11

Occupied space

Page 12: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

Model 2

Magic bread

Cookies

Fish Lollies

Gums

Nuggets

Glow worms

Black worms

Red worms

cl

Page 13: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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!!!!

Page 14: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

Graphical vizualization

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

Page 15: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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.

Page 16: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

Model 2

Magic bread

Cookies

Fish Lollies

Gums

Nuggets

Glow worms

Black worms

Red worms

cl

Page 17: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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.

Page 18: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

After 1 step

Magic bread

Cookies

Fish Lollies

Gums

Nuggets

Glow worms

Black worms

Red worms

cl

Page 19: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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

Page 20: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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!!!!!

Page 21: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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)

Page 22: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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!

Page 23: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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.

Page 24: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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?)

Page 25: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

?

cl

Page 26: Introduction Knowledge Engineering Session 2 Ronald Westra westra@math.unimaas.nl

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).