-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathInstanceCore.cs
126 lines (115 loc) · 5.28 KB
/
InstanceCore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using SC.ObjectModel.Additionals;
using SC.ObjectModel.Elements;
using SC.ObjectModel.Interfaces;
using SC.ObjectModel.IO.Json;
using SC.ObjectModel.Rules;
using SC.Toolbox;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Xml;
namespace SC.ObjectModel
{
/// <summary>
/// Defines the basic problem instance
/// </summary>
public partial class Instance
{
/// <summary>
/// The name of the instance
/// </summary>
public string Name { get; set; }
/// <summary>
/// Contains all pieces which are part of this instance
/// </summary>
public List<VariablePiece> Pieces = new List<VariablePiece>();
/// <summary>
/// Contains all pieces including possible virtual ones
/// </summary>
public IEnumerable<Piece> PiecesWithVirtuals { get { return Pieces.Cast<Piece>().Concat(Containers.SelectMany(c => c.VirtualPieces)); } }
/// <summary>
/// Contains all containers which are part of this instance
/// </summary>
public List<Container> Containers = new List<Container>();
/// <summary>
/// Contains all rule definitions to adhere to when solving the problem instance.
/// </summary>
public RuleSet Rules { get; set; } = new RuleSet();
/// <summary>
/// Writes out basic information about the instance
/// </summary>
/// <param name="tw">The writer to use</param>
public void OutputInfo(TextWriter tw)
{
if (Containers != null)
{
tw.WriteLine("Container:");
foreach (var container in Containers.Where(c => c != null).OrderBy(c => c.ID))
tw.WriteLine(container.ToString());
}
if (Pieces != null)
{
tw.WriteLine("Pieces:");
foreach (var piece in Pieces.Where(c => c != null).OrderBy(p => p.ID))
tw.WriteLine(piece.ToString());
}
}
/// <summary>
/// Returns a semi-ident which can be used to name the instance or identify it later
/// </summary>
/// <returns>The ident string.</returns>
public string GetIdent()
{
if (Containers.Count == 0)
throw new InvalidOperationException("Instance is empty!");
// Build the ident string
return
Containers.Count() + "-" +
Containers.Average(c => c.Mesh.Length).ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) + "-" +
Containers.Average(c => c.Mesh.Width).ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) + "-" +
Containers.Average(c => c.Mesh.Height).ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) + "-" +
Pieces.Count() + "-" +
((!Pieces.Any()) ?
(0.0).ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) :
Pieces.Min(p => Math.Min(Math.Min(p.Original.BoundingBox.Length, p.Original.BoundingBox.Width), p.Original.BoundingBox.Height))
.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER)) + "-" +
((!Pieces.Any()) ?
(0.0).ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) :
Pieces.Max(p => Math.Max(Math.Max(p.Original.BoundingBox.Length, p.Original.BoundingBox.Width), p.Original.BoundingBox.Height))
.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER)) +
(Pieces.Any(p => p.Original.Components.Count > 1) ?
"-tetris-" + Pieces.Average(p => p.Original.Components.Count).ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) :
"");
}
/// <summary>
/// All solutions supplied for this instance
/// </summary>
public HashSet<COSolution> Solutions = new HashSet<COSolution>();
/// <summary>
/// Counts the solutions created for this instance
/// </summary>
private int _solutionCounter = 0;
/// <summary>
/// Creates a new solution and registers it to the instance
/// </summary>
/// <param name="unofficial">Indicates whether to keep track of the created solution or not</param>
/// <param name="tetris">Indicates whether the solution shall be capable of keeping track of tetris based information about itself</param>
/// <param name="meritType">The merit type to use</param>
/// <returns>The newly created solution</returns>
public COSolution CreateSolution(bool tetris, MeritFunctionType meritType, bool unofficial = false)
{
COSolution solution = new COSolution(this, tetris, meritType);
if (!unofficial)
{
solution.ID = ++_solutionCounter;
Solutions.Add(solution);
}
return solution;
}
}
}