Skip to content

Commit

Permalink
Merge pull request #17 from wardzinskim/master
Browse files Browse the repository at this point in the history
loading tspTask from string
  • Loading branch information
pdrozdowski authored Oct 9, 2017
2 parents 54a98fe + 3d5207f commit 7351b9a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
27 changes: 25 additions & 2 deletions TspLibNet/TspLibNet/TSP/TspFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System.Text;

namespace TspLibNet.TSP
{
using System.Collections.Generic;
Expand All @@ -44,7 +47,7 @@ public class TspFile
/// Gets file comment - additional comments from problem author
/// </summary>
public string Comment;

/// <summary>
/// Gets problem dimension, for TSP and ATSP is a number of nodes. For CVRP total number of nodes and depots. For tour it is a dimension of coresponding problem.
/// </summary>
Expand All @@ -64,7 +67,7 @@ public class TspFile
/// Describes format of the edge weights if are given explicitly
/// </summary>
public EdgeWeightFormat EdgeWeightFormat = EdgeWeightFormat.Function;

/// <summary>
/// Describes the format in which the edges of a grap are given, if the graph is not complete
/// </summary>
Expand Down Expand Up @@ -137,5 +140,25 @@ public static TspFile Load(string fileName)

return result;
}

/// <summary>
/// Loads TSP from string
/// </summary>
/// <param name="content">tsp task</param>
/// <returns>Load tsp file</returns>
public static TspFile LoadFromString(string content)
{
TspFile result = null;
TspFileLoader loader = new TspFileLoader();
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(content)))
{
using (StreamReader reader = new StreamReader(ms))
{
result = loader.Load(reader);
reader.Close();
}
}
return result;
}
}
}
5 changes: 5 additions & 0 deletions TspLibNet/TspLibNet/TravelingSalesmanProblem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public static TravelingSalesmanProblem FromFile(string fileName)
return FromTspFile(TspFile.Load(fileName));
}

public static TravelingSalesmanProblem FromString(string content)
{
return FromTspFile(TspFile.LoadFromString(content));
}

/// <summary>
/// Create problem from list of nodes
/// </summary>
Expand Down

0 comments on commit 7351b9a

Please sign in to comment.