CBR/Retrieval.pm

filter: Statementstmt 98.2% 55 / 56 Branchbran 71.4% 10 / 14 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 86.6% 84 / 97 CC 14 SCAR 17.7
CC
14
Cov
88%
compute_sims
10.5
new
3.0
first_confirmed_candidate
3.0
CRAP 5.9
« CBR/Case/Compound.pm ↑ Summary CBR/Sim.pm »
1 package AI::CBR::Retrieval;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 AI::CBR::Retrieval - retrieve similar cases from a case-base
11  
12  
13 =head1 SYNOPSIS
14  
15 Retrieve solutions for a case from a case-base
16  
17 use AI::CBR::Retrieval;
18  
19 my $r = AI::CBR::Retrieval->new($case, \@case_base);
20 $r->compute_sims();
21 my $solution = $r->most_similar_case();
22 ...
23  
24 =head1 METHODS
25  
26 =head2 new
27  
28 Creates a new object for retrieval.
29 Pass your case specification object as the first parameter.
30 Pass the reference of an array of hash references as the case-base.
31 The hashes should contain all attributes of the specification.
32 These will be called candidate cases internally.
33  
34 =cut
35  
36 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
37 2 my ($classname, $spec, $candidates) = @_;
38 2 ▶ croak('new case without candidates') unless @$candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @$candidates 0 2 2
39 2 ▶ my $self = {
Branch: 1 branch
BranchTrueFalseTotal
ref $spec eq 'AI::CBR::Case' ? : 1 1 2
40 candidates => $candidates,
41 # we accept single specs as hash-ref or composed specs as array-ref
42 # internally both will be handled as a composed array-ref
43 queries => ref $spec eq 'AI::CBR::Case' ? [$spec] : $spec,
44 };
45 2 bless $self, $classname;
46 2 return $self;
47 }
48  
49  
50 =head2 compute_sims
51  
52 If the case-specification is complete,
53 you may call this method to compute the similarities
54 of all candidate cases to this specification.
55 After this step, each candidate of the case-base will have an
56 additional attribute C<_sim> indicating the similarity.
57  
58 =cut
59  
60 2 ▶sub compute_sims {
Subroutine compute_sims: called CC=10 SCAR=23.5
Pod documented
61 2 my ($self) = @_;
62
63 # pre-allocate variables used in loop
64 2 my ($sum_sims, $sum_weights, $att_key, $att, $weight, $x, $y);
65
66 2 my $num_queries = int @{$self->{queries}};
67 2 foreach my $candidate (@{$self->{candidates}}) {
68 6 $candidate->{_sim} = 1;
69 6 foreach my $query (@{$self->{queries}}) {
70 8 $sum_sims = 0;
71 8 $sum_weights = 0;
72
73 ATTRIBUTES:
74 8 ▶ while(($att_key, $att) = each(%{$query})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$query;}) 26 8 34
75 26 ▶ next ATTRIBUTES unless $weight = $att->{weight};
Branch: 1 branch
BranchTrueFalseTotal
unless $weight = $att->{"weight"} 0 26 26
76 26 $sum_weights += $weight;
77 26 $x = $att->{value};
78 26 $y = $candidate->{$att_key};
79 $sum_sims += $weight * (
80 !defined $x && !defined $y ? 1
81 : !defined $x || !defined $y ? 0
82 26 ▶ : &{$att->{sim}}($x, $y, $att->{param} || 0)
Branch: 2 branches
BranchTrueFalseTotal
!(defined $x || defined $y) ? : 0 26 26
!(defined $x && defined $y) ? : 0 26 26
Condition50%4/8
defined $x || defined $y
l!l&&r!l&&!r
2600
defined $x && defined $y
!ll&&!rl&&r
0026
$att->{'param'} || 0
l!l
620
MC/DC0%0/2
defined $x || defined $y
defined $x defined $y
MC/DC0%0/2
defined $x && defined $y
defined $x defined $y
MC/DC100%1/1
$att->{'param'} || 0
$att->{'param'}
Truth table33%1/3
A || B
A= defined $xB= defined $y
ABresult
1X1
011
000
Truth table33%1/3
A && B
A= defined $xB= defined $y
ABresult
0X0
100
111
Truth table100%2/2
A
A= $att->{'param'}
Aresult
11
00
83 );
84 }
85
86 8 $candidate->{_sim} *= _nrt($num_queries, $sum_sims / $sum_weights);
87 }
88 }
89 2 my @candidates_sorted = sort { $b->{_sim} <=> $a->{_sim} } @{$self->{candidates}};
90 2 $self->{candidates} = \@candidates_sorted;
91 }
92  
93  
94 =head2 RETRIEVAL METHODS
95  
96 Use one of these methods to get the similar cases you are interested into.
97  
98 =head3 most_similar_candidate
99  
100 Returns the most similar candidate.
101 No parameters.
102  
103 =cut
104  
105 1 ▶sub most_similar_candidate {
Subroutine most_similar_candidate: called CC=1 SCAR=0.0
Pod documented
106 1 my ($self) = @_;
107 1 return $self->{candidates}->[0];
108 }
109  
110 =head3 n_most_similar_candidates
111  
112 Returns the n most similar candidates.
113 n is the only parameter.
114  
115 =cut
116  
117 1 ▶sub n_most_similar_candidates {
Subroutine n_most_similar_candidates: called CC=1 SCAR=0.0
Pod documented
118 1 my ($self, $n) = @_;
119 1 my $last_index = min($n - 1, int @{$self->{candidates}});
120 1 return map { $self->{candidates}->[$_] } (0 .. $last_index);
121 }
122  
123 =head3 first_confirmed_candidate
124  
125 Returns the first candidate that is confirmed by a later candidate.
126 Confirmation is based on an attribute value
127 whose key is passed as parameter.
128 In case there is no confirmed candidate at all,
129 simply returns the most similar one.
130  
131 =cut
132  
133 1 ▶sub first_confirmed_candidate {
Subroutine first_confirmed_candidate: called CC=3 SCAR=11.0
Pod documented
134 1 my ($self, $key) = @_;
135 1 my %candidate_with;
136 my $value;
137 1 foreach my $candidate (@{$self->{candidates}}) {
138 4 $value = $candidate->{$key};
139 4 ▶ if($candidate_with{$value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($candidate_with{$value}) { } 1 3 4
140 1 return $candidate_with{$value};
141 } else {
142 3 $candidate_with{$value} = $candidate;
143 }
144 }
145  
146 # no confirmed candidate found, fall back
147 0 return $self->most_similar_candidate();
148 }
149  
150  
151 # internal method for n-th root
152 8 ▶sub _nrt {
Subroutine _nrt: called CC=1 SCAR=0.0
153 8 return $_[1] ** (1 / $_[0]);
154 }
155  
156  
157 =head1 SEE ALSO
158  
159 See L<AI::CBR> for an overview of the framework.
160  
161  
162 =head1 AUTHOR
163  
164 Darko Obradovic, C<< <dobradovic at gmx.de> >>
165  
166 =head1 BUGS
167  
168 Please report any bugs or feature requests to C<bug-ai-cbr at rt.cpan.org>, or through
169 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AI-CBR>. I will be notified, and then you'll
170 automatically be notified of progress on your bug as I make changes.
171  
172  
173  
174  
175 =head1 SUPPORT
176  
177 You can find documentation for this module with the perldoc command.
178  
179 perldoc AI::CBR::Retrieval
180  
181  
182 You can also look for information at:
183  
184 =over 4
185  
186 =item * RT: CPAN's request tracker
187  
188 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=AI-CBR>
189  
190 =item * AnnoCPAN: Annotated CPAN documentation
191  
192 L<http://annocpan.org/dist/AI-CBR>
193  
194 =item * CPAN Ratings
195  
196 L<http://cpanratings.perl.org/d/AI-CBR>
197  
198 =item * Search CPAN
199  
200 L<http://search.cpan.org/dist/AI-CBR>
201  
202 =back
203  
204  
205 =head1 COPYRIGHT & LICENSE
206  
207 Copyright 2009 Darko Obradovic, all rights reserved.
208  
209 This program is free software; you can redistribute it and/or modify it
210 under the same terms as Perl itself.
211  
212  
213 =cut
214  
215 4 1; # End of AI::CBR::Retrieval
« CBR/Case/Compound.pm ↑ Summary CBR/Sim.pm »