Skip to content
This repository has been archived by the owner on Oct 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystucki committed Nov 1, 2016
2 parents 393fcc7 + 6f3a678 commit 8191ba5
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 14 deletions.
4 changes: 2 additions & 2 deletions NEAT_Visualizer/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ static void Main(string[] args)
var app = new App();

AppBuilder.Configure(app)
.UsePlatformDetect()
.SetupWithoutStarting();
.UsePlatformDetect()
.SetupWithoutStarting();

bootstrapper.InitializeApplication();
app.Start(bootstrapper.StartupWindow);
Expand Down
28 changes: 28 additions & 0 deletions NEAT_Visualizer/Business/DataLoaders/LazyCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace NEAT_Visualizer.Business.DataLoaders
{
public class LazyCache<T> where T : class
{
private readonly Func<T> valueFactory;

private T temp;

public LazyCache(Func<T> valueFactory)
{
if (valueFactory == null)
{
throw new ArgumentException(nameof(valueFactory));
}

this.temp = null;
this.valueFactory = valueFactory;
}

public T Value => temp ?? (temp = valueFactory.Invoke());

public void ResetCache() => temp = null;

public bool IsLoaded => temp != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using NEAT_Visualizer.Business.DataLoaders;
using NEAT_Visualizer.Model;

Expand All @@ -12,10 +13,11 @@ namespace NEAT_Visualizer.Business.GenerationProvider
/// </summary>
public class LazyGenerationProvider : IGenerationProvider
{
public int MaxLoadedGenerations { get; set; } = 10;

//private readonly List<FileInfo> generationFiles;
private readonly IEnumerable<GenerationMetadata> metadata;
private readonly List<Lazy<Generation>> generations = new List<Lazy<Generation>>();
private readonly List<LazyCache<Generation>> generations = new List<LazyCache<Generation>>();

/// <summary>
///
Expand All @@ -28,7 +30,7 @@ public LazyGenerationProvider(Dictionary<GenerationMetadata, FileInfo> generatio

foreach (FileInfo file in generationFiles.Values)
{
generations.Add(new Lazy<Generation>(() => loader.LoadGeneration(file)));
generations.Add(new LazyCache<Generation>(() => loader.LoadGeneration(file)));
}
}

Expand All @@ -40,6 +42,16 @@ public LazyGenerationProvider(Dictionary<GenerationMetadata, FileInfo> generatio
/// <returns></returns>
public Generation GetGeneration(int index)
{
// delete internal references to make sure when the generation is not used anymore,
// it can be removed by the GC.
var loadedGenerations = generations.Where(x => x.IsLoaded).ToList();
if (loadedGenerations.Count > MaxLoadedGenerations)
{
loadedGenerations.ForEach(x => x.ResetCache());
//Task.Run(() => GC.Collect()); // not required, because the GC will do it himself and it
// will be slowing down the application less.
}

return generations[index].Value;
}

Expand Down
4 changes: 1 addition & 3 deletions NEAT_Visualizer/Business/IVisualizerBusiness.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using NEAT_Visualizer.Business.DataLoaders;
using NEAT_Visualizer.Model;
using NEAT_Visualizer.Business.DataLoaders;

namespace NEAT_Visualizer.Business
{
Expand Down
4 changes: 1 addition & 3 deletions NEAT_Visualizer/Business/VisualizerBusiness.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using NEAT_Visualizer.Business.DataLoaders;
using NEAT_Visualizer.Model;
using NEAT_Visualizer.Business.DataLoaders;

namespace NEAT_Visualizer.Business
{
Expand Down
1 change: 1 addition & 0 deletions NEAT_Visualizer/NEAT_Visualizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<Compile Include="Business\DataLoaders\IMetadataLoader.cs" />
<Compile Include="Business\DataLoaders\JsonRepresentation.cs" />
<Compile Include="Business\DataLoaders\JsonToModelMapper.cs" />
<Compile Include="Business\DataLoaders\LazyCache.cs" />
<Compile Include="Business\DataLoaders\MetatdataLoader.cs" />
<Compile Include="Business\GenerationProvider\EmptyGenerationProvider.cs" />
<Compile Include="Business\GenerationProvider\GenerationMetadata.cs" />
Expand Down
32 changes: 30 additions & 2 deletions NEAT_Visualizer/UserControls/NetworkPresenter.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,44 @@ public override void Render(DrawingContext context)
}

uint black = ColorToUInt(new Color(255, 0, 0, 0));
var blackLinePen = new Pen(black, 4);
//uint red = ColorToUInt(new Color(255, 255, 0, 0));
//uint cyan = ColorToUInt(new Color(255, 0, 255, 255));
//uint turquoise = ColorToUInt(new Color(255, 0, 229, 238));
uint turquoise = 0xFF51DBAF;
uint orange = 0xFFFFA347;

var normalConnectionPen = new Pen(turquoise, 4);
var recursiveConnectionPen = new Pen(orange, 2);
var blackOutlinePen = new Pen(black);
var neuronFillColor = new SolidColorBrush(new Color(255, 102, 255, 102));

var normalConnections = new List<LineData>();
var recursiveConnections = new List<LineData>();
// draw all lines (draw connections before neurons, so neurons overlap the connections)
foreach (LineData line in connectionsDrawingInformation.Values)
{
context.DrawLine(blackLinePen, line.Start, line.End);
if (line.End.Y > line.Start.Y)
{
recursiveConnections.Add(line);
}
else
{
normalConnections.Add(line);
}
}

//draw normal connections first, so they dont overlap the red lines that represent recursive connections
foreach (var line in normalConnections)
{
context.DrawLine(normalConnectionPen, line.Start, line.End);
}

foreach (var line in recursiveConnections)
{
context.DrawLine(recursiveConnectionPen, line.Start, line.End);
}


// draw all neurons
foreach (Point neuronCenters in neuronsDrawingInformation.Values)
{
Expand Down
3 changes: 1 addition & 2 deletions NEAT_Visualizer/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
Expand Down

0 comments on commit 8191ba5

Please sign in to comment.