Introduction Knowledge Engineering

advertisement
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.
2.
3.
4.
5.
6.
7.
8.
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
Go fish (part 2)
With each item, the number of fish you expect to catch per
item (not per unit!) is as follows:
1.
2.
3.
4.
5.
6.
7.
8.
Magic bread: 11
Cookies: 11
Fish Lollies: 7
Fish gum: 7
Fish nuggets: 6
Glow worms: 1
Black worms:1
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…
fish food items
catch per item
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
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
Magic bread: 11
Cookies: 11
Fish Lollies: 7
Fish gum: 7
Fish nuggets: 6
Glow worms: 1
Black worms:1
Red worms: 1
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?
Magic bread
Model 2
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 v
Consider 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).
Download