~ubuntu-branches/ubuntu/vivid/banshee-community-extensions/vivid

« back to all changes in this revision

Viewing changes to .pc/mirage-new-randomby-api.patch/src/Mirage/Banshee.Mirage/SimilarityContext.cs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2010-11-24 14:59:52 UTC
  • Revision ID: james.westby@ubuntu.com-20101124145952-6xcdipawrpnn00n0
Tags: 1.9.0-1ubuntu2
* [5c94c08] Disable OpenVP extension temporarily.
* [609c47e] Enable Ubuntu One Music Store by default
* [6d26f8b] Fix build with new RandomBy APIs included in git snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
using System;
 
3
using System.Linq;
 
4
using System.Collections.Generic;
 
5
 
 
6
using Mirage;
 
7
 
 
8
namespace Banshee.Mirage
 
9
{
 
10
    public class SimilarityContext : BaseSimilarityContext
 
11
    {
 
12
        public const float SelectedWeight = 4.0f;
 
13
        public const float PlayedWeight = 2.0f;
 
14
        public const float ShuffledWeight = 1.0f;
 
15
        public const float DiscardedWeight = 1.0f / 5.0f;
 
16
        public const float SkippedWeight = 1.0f / 10.0f;
 
17
 
 
18
        private List<Seed> seeds = new List<Seed> ();
 
19
 
 
20
        // This is for testing/debugging use
 
21
        private Scms best_scms;
 
22
        private bool debug;
 
23
 
 
24
        private static bool static_avoid_artists;
 
25
        private bool avoid_artists;
 
26
 
 
27
        public SimilarityContext ()
 
28
        {
 
29
            avoid_artists = static_avoid_artists;
 
30
            static_avoid_artists = !static_avoid_artists;
 
31
        }
 
32
 
 
33
        public void AddSeeds (IEnumerable<Seed> seeds)
 
34
        {
 
35
            foreach (var seed in seeds) {
 
36
                //Console.WriteLine ("Adding seed track ({0}) with weight {1}", seed.Uri, seed.Weight);
 
37
                if (seed.Scms != null) {
 
38
                    this.seeds.Add (seed);
 
39
                }
 
40
            }
 
41
 
 
42
            IsEmpty = this.seeds.Count == 0;
 
43
        }
 
44
 
 
45
        public int [] AvoidArtistIds {
 
46
            get {
 
47
                if (avoid_artists) {
 
48
                    return seeds.Select (s => s.ArtistId).ToArray ();
 
49
                } else {
 
50
                    return new int [0];
 
51
                }
 
52
            }
 
53
        }
 
54
 
 
55
        public void DumpDebug ()
 
56
        {
 
57
            var avoid_ids = String.Join (", ", AvoidArtistIds.Select (id => id.ToString ()).ToArray ());
 
58
            Console.WriteLine ("  Avoided artist ids = {0}\n  Seed Distances:", avoid_ids);
 
59
 
 
60
            debug = true;
 
61
            Console.WriteLine ("  Average weighted distance: {0:N1}", Distance (best_scms).Average ());
 
62
            debug = false;
 
63
        }
 
64
 
 
65
        public override IEnumerable<float> Distance (Scms from)
 
66
        {
 
67
            if (from == null) {
 
68
                yield return float.MaxValue;
 
69
                yield break;
 
70
            }
 
71
 
 
72
            bool any_seeds = false;
 
73
            float last_weight = -99;
 
74
 
 
75
            foreach (var seed in seeds) {
 
76
                var distance = Scms.Distance (seed.Scms, from, Config);
 
77
                if (distance < 0) {
 
78
                    // Ignore negative distance values
 
79
                    continue;
 
80
                }
 
81
 
 
82
                if (distance < min_distance) {
 
83
                    min_distance = distance;
 
84
                    best_scms = from;
 
85
                } else if (distance > max_distance) {
 
86
                    max_distance = distance;
 
87
                }
 
88
 
 
89
                float weighted_distance = distance / seed.Weight;
 
90
                if (debug) {
 
91
                    if (seed.Weight != last_weight) {
 
92
                        last_weight = seed.Weight;
 
93
                        Console.WriteLine ("    {0} seeds (weight {1,3:N1})", last_weight == ShuffledWeight ? "Shuffled" : last_weight == PlayedWeight ? "Played" : last_weight == SelectedWeight ? "Manually Selected" : "Skipped/Discarded", last_weight);
 
94
                    }
 
95
 
 
96
                    Console.WriteLine ("      distance: {0,4:N1}, weighted: {1,4:N1} from artist_id {2,4}, uri {3}",
 
97
                                       distance, weighted_distance, seed.ArtistId, seed.Uri);
 
98
                }
 
99
                yield return weighted_distance;
 
100
                any_seeds = true;
 
101
            }
 
102
 
 
103
            if (!any_seeds) {
 
104
                // Return the highest distance possible
 
105
                yield return Single.MaxValue;
 
106
            }
 
107
        }
 
108
    }
 
109
}