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

Commit

Permalink
Merge pull request #21 from 2nd-Semester-Project/static-restructure-main
Browse files Browse the repository at this point in the history
Restructured main file in the static approach
  • Loading branch information
svenons authored May 10, 2024
2 parents 9d3c7dc + 490843b commit 2e8bbd2
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 229 deletions.
13 changes: 6 additions & 7 deletions HeatOptimiser.Tests/OptimiserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@ public class OptimiserTest
public void TestOptimise()
{
// Arrange
SourceDataManager sourceManager = new SourceDataManager();
#pragma warning disable CS8602 // Dereference of a possibly null reference.
string projectDirectory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;
#pragma warning restore CS8602 // Dereference of a possibly null reference.
string file = Path.Combine(projectDirectory, "SourceDataTest.xlsx");
var data = sourceManager.LoadXLSXFile(file, 4, 2);
var data = SourceDataManager.LoadXLSXFile(file, 4, 2);

IAssetManager assetManager = new AssetManager();
assetManager.AddUnit("GB", "none", 5.0, 0, 1.1, 500, 215);
assetManager.AddUnit("OB", "none", 4.0, 0, 1.2, 700, 265);
AssetManager.AddUnit("GB", "none", 5.0, 0, 1.1, 500, 215);
AssetManager.AddUnit("OB", "none", 4.0, 0, 1.2, 700, 265);

Optimiser optimiser = new Optimiser(sourceManager, assetManager);
string startDateStr = "12/02/2023";
string endDateStr = "25/02/2023";
DateTime startDate = DateTime.ParseExact(startDateStr, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
DateTime endDate = DateTime.ParseExact(endDateStr, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);

// Act
Schedule schedule = optimiser.Optimise(startDate, endDate);
Schedule schedule = Optimiser.Optimise(startDate, endDate);

// Assert
Assert.NotNull(schedule);
Expand Down
8 changes: 4 additions & 4 deletions HeatOptimiser.Tests/SourceDataManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ public class SourceDataManagerTest
public void TestLoadXLSXFile()
{
// Arrange
SourceDataManager sourceManager = new SourceDataManager();
#pragma warning disable CS8602 // Dereference of a possibly null reference.
string projectDirectory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;
#pragma warning restore CS8602 // Dereference of a possibly null reference.
string file = Path.Combine(projectDirectory, "SourceDataTest.xlsx");
Console.WriteLine(file);

// Act
var result = sourceManager.LoadXLSXFile(file, 4, 2);
var result = SourceDataManager.LoadXLSXFile(file, 4, 2);

// Assert
Assert.NotNull(result);
Expand All @@ -26,12 +27,11 @@ public void TestGetDataInRange()
{
// Arrange
SourceData data = new SourceData();
SourceDataManager sourceManager = new SourceDataManager();
DateTime startDate = new DateTime(2023, 1, 1);
DateTime endDate = new DateTime(2023, 1, 31);

// Act
var result = sourceManager.GetDataInRange(data, startDate, endDate);
var result = SourceDataManager.GetDataInRange(data, startDate, endDate);

// Assert
Assert.NotNull(result);
Expand Down
29 changes: 15 additions & 14 deletions HeatOptimiser/Classes/AssetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using ReactiveUI;
using System.Reactive;
using UserInterface.ViewModels;
using System.Net;

namespace HeatOptimiser
{
Expand Down Expand Up @@ -68,12 +69,12 @@ public bool IsSelected
set => this.RaiseAndSetIfChanged(ref _isSelected, value);
}
}
public class AssetManager: IAssetManager
public static class AssetManager
{
public string saveFileName = "ProductionAssets.json";
public ObservableCollection<ProductionAsset> _productionAssets = new ObservableCollection<ProductionAsset>();
private JsonAssetStorage _jsonAssetStorage = new JsonAssetStorage();
public void AddUnit(string name, string image, double heat, double electricity, double energy, double cost, double carbonDioxide)
public static string saveFileName = "ProductionAssets.json";
public static ObservableCollection<ProductionAsset> _productionAssets = new ObservableCollection<ProductionAsset>();
private static JsonAssetStorage _jsonAssetStorage = new JsonAssetStorage();
public static void AddUnit(string name, string image, double heat, double electricity, double energy, double cost, double carbonDioxide)
{
if (name != null && image != null && !string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(image))
{
Expand All @@ -95,12 +96,12 @@ public void AddUnit(string name, string image, double heat, double electricity,
throw new ArgumentNullException("Name and Image cannot be null");
}
}
public void DeleteUnit(Guid ID)
public static void DeleteUnit(Guid ID)
{
_productionAssets.Remove(_productionAssets.FirstOrDefault(x => x.ID == ID)!);
_jsonAssetStorage.SaveUnits(_productionAssets, saveFileName); // this is also up for debate, just like on AddUnit.
}
public void EditUnit(Guid ID, int index, string stringValue)
public static void EditUnit(Guid ID, int index, string stringValue)
{
switch (index)
{
Expand All @@ -115,7 +116,7 @@ public void EditUnit(Guid ID, int index, string stringValue)
}
_jsonAssetStorage.SaveUnits(_productionAssets, saveFileName); // this is also up for debate, just like on AddUnit.
}
public void EditUnit(Guid ID, int index, double doubleValue)
public static void EditUnit(Guid ID, int index, double doubleValue)
{
switch (index)
{
Expand All @@ -139,31 +140,31 @@ public void EditUnit(Guid ID, int index, double doubleValue)
}
_jsonAssetStorage.SaveUnits(_productionAssets, saveFileName); // this is also up for debate, just like on AddUnit.
}
public ObservableCollection<ProductionAsset> GetAllUnits()
public static ObservableCollection<ProductionAsset> GetAllUnits()
{
return _productionAssets;
}
public ObservableCollection<ProductionAsset> LoadUnits(string fileName)
public static ObservableCollection<ProductionAsset> LoadUnits(string fileName)
{
_productionAssets = _jsonAssetStorage.LoadUnits(fileName);
return _productionAssets;
}
public void SaveUnits(ObservableCollection<ProductionAsset> AllAssets, string fileName)
public static void SaveUnits(ObservableCollection<ProductionAsset> AllAssets, string fileName)
{
_jsonAssetStorage.SaveUnits(AllAssets, fileName);
}
public ObservableCollection<ProductionAsset> SearchUnits(string name)
public static ObservableCollection<ProductionAsset> SearchUnits(string name)
{
var selection = _productionAssets.Where(x => x.Name!.ToLower().Contains(name.ToLower())).ToList();
ObservableCollection<ProductionAsset> selected = [.. selection];
return selected;
}
public void SetSaveFile(string fileName)
public static void SetSaveFile(string fileName)
{
saveFileName = fileName;
}
}
public class JsonAssetStorage: IAssetStorage
public class JsonAssetStorage
{
public ObservableCollection<ProductionAsset> LoadUnits(string fileName)
{
Expand Down
82 changes: 41 additions & 41 deletions HeatOptimiser/Classes/DataVisualizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,58 @@
namespace HeatOptimiser
{

public class DataVisualizer
{
public SourceData sourceData = new SourceData();
public static class DataVisualizer
{
public static SourceData sourceData = new SourceData();

// public List<DateTime> SummerTimes = new List<DateTime>();
// public List<double?> SummerHeatDemands = new List<double?>();
// public List<double?> SummerElectricityPrices = new List<double?>();
// public List<DateTime> WinterTimes = new List<DateTime>();
// public List<double?> WinterHeatDemands = new List<double?>();
// public List<double?> WinterElectricityPrices = new List<double?>();
// public List<DateTime> SummerTimes = new List<DateTime>();
// public List<double?> SummerHeatDemands = new List<double?>();
// public List<double?> SummerElectricityPrices = new List<double?>();
// public List<DateTime> WinterTimes = new List<DateTime>();
// public List<double?> WinterHeatDemands = new List<double?>();
// public List<double?> WinterElectricityPrices = new List<double?>();

public readonly ObservableCollection<DateTimePoint>? WinterHeatDemandData = new ObservableCollection<DateTimePoint>();
public readonly ObservableCollection<DateTimePoint>? SummerHeatDemandData = new ObservableCollection<DateTimePoint>();
public static readonly ObservableCollection<DateTimePoint>? WinterHeatDemandData = new ObservableCollection<DateTimePoint>();
public static readonly ObservableCollection<DateTimePoint>? SummerHeatDemandData = new ObservableCollection<DateTimePoint>();

public void AccessSummerData()
{
// Accessing the summer data
foreach (var point in sourceData.SummerData)
public static void AccessSummerData()
{
if (point.TimeFrom.HasValue) // Ensuring the time is not null
// Accessing the summer data
foreach (var point in sourceData.SummerData)
{
SummerHeatDemandData!.Add(new DateTimePoint(point.TimeFrom.Value, point.HeatDemand));
// SummerElectricityPrices.Add(point.ElectricityPrice);
if (point.TimeFrom.HasValue) // Ensuring the time is not null
{
SummerHeatDemandData!.Add(new DateTimePoint(point.TimeFrom.Value, point.HeatDemand));
// SummerElectricityPrices.Add(point.ElectricityPrice);
}
}
}
}

public void AccessWinterData()
{
// Accessing the summer data
foreach (var point in sourceData.WinterData)
public static void AccessWinterData()
{
if (point.TimeFrom.HasValue) // Ensuring the time is not null
// Accessing the summer data
foreach (var point in sourceData.WinterData)
{
WinterHeatDemandData!.Add(new DateTimePoint(point.TimeFrom.Value, point.HeatDemand));
// SummerElectricityPrices.Add(point.ElectricityPrice);
if (point.TimeFrom.HasValue) // Ensuring the time is not null
{
WinterHeatDemandData!.Add(new DateTimePoint(point.TimeFrom.Value, point.HeatDemand));
// SummerElectricityPrices.Add(point.ElectricityPrice);
}
}
}
}

// public void AccessWinterData()
// {
// // Accessing the winter data
// foreach (var point in sourceData.WinterData)
// {
// if (point.TimeFrom.HasValue) // Ensuring the time is not null
// {
// WinterTimes.Add(point.TimeFrom.Value);
// WinterHeatDemands.Add(point.HeatDemand);
// WinterElectricityPrices.Add(point.ElectricityPrice);
// }
// }
// }
}
// public void AccessWinterData()
// {
// // Accessing the winter data
// foreach (var point in sourceData.WinterData)
// {
// if (point.TimeFrom.HasValue) // Ensuring the time is not null
// {
// WinterTimes.Add(point.TimeFrom.Value);
// WinterHeatDemands.Add(point.HeatDemand);
// WinterElectricityPrices.Add(point.ElectricityPrice);
// }
// }
// }
}
}
51 changes: 16 additions & 35 deletions HeatOptimiser/Classes/Optimiser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,58 +35,39 @@ public class ScheduleHour
public ObservableCollection<ProductionAsset>? Assets { get; set; }
public ObservableCollection<double>? Demands { get; set; }
}
public class Optimiser: IOptimiserModule
public static class Optimiser
{
private ISourceDataManager sd;
private IAssetManager am;
public Optimiser(ISourceDataManager sourceDataManager, IAssetManager assetManager)
{
sd = sourceDataManager;
am = assetManager;
}

public Schedule Optimise(DateTime startDate, DateTime endDate)
public static Schedule Optimise(DateTime startDate, DateTime endDate)
{
SourceData data = new();
Schedule schedule = new(startDate, endDate);
ProductionAsset gasBoiler = AssetManager.SearchUnits("GB")[0];
ProductionAsset oilBoiler = AssetManager.SearchUnits("OB")[0];

ObservableCollection<ProductionAsset> assets = am.GetAllUnits();

for (int i = 0; i < assets.Count; i++)
foreach (SourceDataPoint hour in SourceDataManager.GetDataInRange(data, startDate, endDate))
{
for (int j = 0; j > assets.Count - i; i++)
if (hour.HeatDemand <= gasBoiler.Heat)
{
if (assets[i].Cost > assets[j].Cost)
{
(assets[i], assets[j]) = (assets[j], assets[i]);
}
double gasDemand = (double)hour.HeatDemand;
schedule.AddHour(hour.TimeFrom, [gasBoiler], [gasDemand]);
}
}

foreach (SourceDataPoint hour in sd.GetDataInRange(data, startDate, endDate))
{
double producedHeat = 0;
int index = 0;
ObservableCollection<ProductionAsset> assetsUsed = [];
ObservableCollection<double> assetDemands = [];
while (producedHeat < hour.HeatDemand)
else
{
assetsUsed.Add(assets[index]);
assetDemands.Add(assets[index].Heat!.Value);
producedHeat += assets[index].Heat!.Value;
index += 1;
double gasCapacity = (double)gasBoiler.Heat!;
double hourDemand = (double)hour.HeatDemand!;
double oilDemand = hourDemand - gasCapacity;
schedule.AddHour(hour.TimeFrom, [gasBoiler, oilBoiler], [gasCapacity, oilDemand]);
}
schedule.AddHour(hour.TimeFrom, assetsUsed, assetDemands);
}
return schedule;
}

public Schedule Optimise2(DateTime startDate, DateTime endDate)
public static Schedule Optimise2(DateTime startDate, DateTime endDate)
{
SourceData data = new();
Schedule schedule = new(startDate, endDate);

ObservableCollection<ProductionAsset> assets = am.GetAllUnits();
ObservableCollection<ProductionAsset> assets = AssetManager.GetAllUnits();

Dictionary<ProductionAsset, double?> netCosts = new();

Expand All @@ -95,7 +76,7 @@ public Schedule Optimise2(DateTime startDate, DateTime endDate)
netCosts.Add(assets[i], assets[i].Cost);
}

foreach (SourceDataPoint hour in sd.GetDataInRange(data, startDate, endDate))
foreach (SourceDataPoint hour in SourceDataManager.GetDataInRange(data, startDate, endDate))
{
Dictionary<ProductionAsset, double?> costs = new(netCosts);
foreach(ProductionAsset asset in costs.Keys)
Expand Down
18 changes: 6 additions & 12 deletions HeatOptimiser/Classes/ResultsDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@

namespace HeatOptimiser
{
public class ResultsDataManager : IResultsDataManager
public class ResultsDataManager
{
private string filePath;
private AssetManager am;
public ResultsDataManager(string passedFilePath, AssetManager assetManager)
{
filePath = passedFilePath;
am = assetManager;
}
public void Save(Schedule schedule)
private static string filePath = "data/resultdata.csv";
public static void Save(Schedule schedule)
{
var csv = new StringBuilder();
foreach (ScheduleHour hour in schedule.schedule)
Expand Down Expand Up @@ -59,7 +53,7 @@ public void Save(Schedule schedule)

File.WriteAllText(filePath, csv.ToString());
}
public void Remove(DateOnly dateFrom, DateOnly dateTo)
public static void Remove(DateOnly dateFrom, DateOnly dateTo)
{
List<string> lines = File.ReadAllLines(filePath).ToList();
List<int> removableIndexes = new List<int>();
Expand All @@ -84,7 +78,7 @@ public void Remove(DateOnly dateFrom, DateOnly dateTo)

File.WriteAllLines(filePath, newLines);
}
public Schedule Load(DateOnly dateFrom, DateOnly dateTo)
public static Schedule Load(DateOnly dateFrom, DateOnly dateTo)
{
var csvConfig = new CsvConfiguration(CultureInfo.CurrentCulture)
{
Expand Down Expand Up @@ -115,7 +109,7 @@ public Schedule Load(DateOnly dateFrom, DateOnly dateTo)
ObservableCollection<double> demands = [];
foreach(string assetID in line[1].Trim().Split('/'))
{
var unit = am.GetAllUnits().FirstOrDefault(x => x.ID.ToString() == assetID);
var unit = AssetManager.GetAllUnits().FirstOrDefault(x => x.ID.ToString() == assetID);
if (unit != null)
{
assets.Add(unit);
Expand Down
Loading

0 comments on commit 2e8bbd2

Please sign in to comment.