initial commit
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
|
||||
</startup>
|
||||
</configuration>
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Google.OrTools.LinearSolver;
|
||||
|
||||
namespace knapsack
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
private static Tuple<int, IEnumerable<Tuple<int, int>>> GetData(string filename)
|
||||
{
|
||||
var lines = File.ReadAllLines(filename);
|
||||
var sizeWeight = lines[0];
|
||||
var strings = sizeWeight.Replace(" ", " ").Split(' ');
|
||||
var weight = int.Parse(strings[1]);
|
||||
var items = lines.Skip(1).Select(line => line.Replace(" ", " ").Split(' ')).Select(split => new Tuple<int, int>(int.Parse(split[0]), int.Parse(split[1])));
|
||||
return new Tuple<int, IEnumerable<Tuple<int, int>>>(weight, items);
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var data = GetData(args[0]);
|
||||
var maxWeight = data.Item1;
|
||||
var items = data.Item2;
|
||||
|
||||
var solver = Solver.CreateSolver("IntegerProgramming", "CBC_MIXED_INTEGER_PROGRAMMING");
|
||||
solver.SetMaximization();
|
||||
var weightConstraint = solver.MakeConstraint(double.NegativeInfinity, maxWeight, "weight");
|
||||
|
||||
var i = 0;
|
||||
var variables = new List<Variable>();
|
||||
foreach (var tuple in items)
|
||||
{
|
||||
var value = tuple.Item1;
|
||||
var weight = tuple.Item2;
|
||||
var var = solver.MakeIntVar(0.0, 1.0, "i" + i++);
|
||||
variables.Add(var);
|
||||
solver.SetObjectiveCoefficient(var, value);
|
||||
weightConstraint.SetCoefficient(var, weight);
|
||||
}
|
||||
|
||||
var resultStatus = solver.Solve();
|
||||
|
||||
// The objective value of the solution.
|
||||
Console.WriteLine(solver.ObjectiveValue() + " 0");// + (resultStatus == Solver.OPTIMAL ? "1" : "0"));
|
||||
|
||||
// The value of each variable in the solution.
|
||||
var takeItems = string.Join(" ", variables.Select(v => v.SolutionValue()).ToArray());
|
||||
|
||||
Console.WriteLine(takeItems);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("knapsackcs")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("knapsackcs")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2013")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("f4e9ee87-7e9e-4d66-ac34-5aa320bc3900")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
@@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{C56A1DC5-0CB3-41EB-83E0-973A65683397}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>knapsack</RootNamespace>
|
||||
<AssemblyName>knapsack</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>..\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Google.OrTools.LinearSolver">
|
||||
<HintPath>..\..\..\..\Documents\or-tools.Windows64\bin\Google.OrTools.LinearSolver.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2012
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "knapsackcs", "knapsackcs.csproj", "{C56A1DC5-0CB3-41EB-83E0-973A65683397}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{C56A1DC5-0CB3-41EB-83E0-973A65683397}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{C56A1DC5-0CB3-41EB-83E0-973A65683397}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C56A1DC5-0CB3-41EB-83E0-973A65683397}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C56A1DC5-0CB3-41EB-83E0-973A65683397}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
Reference in New Issue
Block a user