27.07.2013 Views

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Systems with Complex State 207<br />

several actions with the same reward, then one of the actions is chosen randomly.<br />

The GetEnabledActions method is defined in the basic strategy; it enumerates all<br />

the actions with the given action symbols that are enabled in the current state.<br />

partial class CustomStrategy<br />

{<br />

public override Action SelectAction(Set actionSymbols)<br />

{<br />

double bestRewardSoFar = 0;<br />

Set bestActionsSoFar = Set.EmptySet;<br />

foreach (Action a in GetEnabledActions(actionSymbols))<br />

{<br />

double reward = GetReward(a);<br />

if (reward == bestRewardSoFar)<br />

bestActionsSoFar = bestActionsSoFar.Add(a);<br />

else if (reward > bestRewardSoFar)<br />

{<br />

bestRewardSoFar = reward;<br />

bestActionsSoFar = new Set(a);<br />

}<br />

}<br />

return (bestActionsSoFar.IsEmpty ? null<br />

: bestActionsSoFar.Choose());<br />

}<br />

}<br />

The idea behind the reward function is that it maps each enabled action in the<br />

current state to a numeric real value that characterizes how “rewarding” it would<br />

be to explore that action. Suppose that we wish to reward those actions more that<br />

either cover new coverage points or cover more coverage points. <strong>We</strong> can calculate<br />

a reward value as follow:<br />

partial class CustomStrategy<br />

{<br />

double GetReward(Action a)<br />

{<br />

double reward = 0;<br />

Bag cps = GetCoverage(CurrentState, a);<br />

foreach (Term cp in cps)<br />

{<br />

int newC = cps.CountItem(cp);<br />

int oldC = coveragePoints.CountItem(cp);<br />

more free ebooks download links at:<br />

http://www.ebook-x.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!