mirror of
https://github.com/Z3Prover/z3
synced 2025-08-07 03:31:23 +00:00
Add MSF plugins
This commit is contained in:
parent
58f8181a74
commit
5cc4cc8226
24 changed files with 2683 additions and 0 deletions
60
examples/msf/SolverFoundation.Plugin.Z3.Tests/App.config
Normal file
60
examples/msf/SolverFoundation.Plugin.Z3.Tests/App.config
Normal file
|
@ -0,0 +1,60 @@
|
|||
<?xml version="1.0"?>
|
||||
<configuration>
|
||||
<configSections>
|
||||
<section name="MsfConfig"
|
||||
type="Microsoft.SolverFoundation.Services.MsfConfigSection, Microsoft.Solver.Foundation"
|
||||
allowLocation="true"
|
||||
allowDefinition="Everywhere"
|
||||
allowExeDefinition="MachineToApplication"
|
||||
restartOnExternalChanges="true"
|
||||
requirePermission="true"/>
|
||||
</configSections>
|
||||
<MsfConfig>
|
||||
<MsfPluginSolvers>
|
||||
<MsfPluginSolver name="Microsoft Z3 MILP Solver"
|
||||
capability="MILP"
|
||||
assembly="SolverFoundation.Plugin.Z3.dll"
|
||||
solverclass="Microsoft.SolverFoundation.Plugin.Z3.Z3MILPSolver"
|
||||
directiveclass="Microsoft.SolverFoundation.Plugin.Z3.Z3MILPDirective"
|
||||
parameterclass="Microsoft.SolverFoundation.Plugin.Z3.Z3MILPParams"/>
|
||||
<MsfPluginSolver name="Microsoft Z3 MILP Solver"
|
||||
capability="LP"
|
||||
assembly="SolverFoundation.Plugin.Z3.dll"
|
||||
solverclass="Microsoft.SolverFoundation.Plugin.Z3.Z3MILPSolver"
|
||||
directiveclass="Microsoft.SolverFoundation.Plugin.Z3.Z3MILPDirective"
|
||||
parameterclass="Microsoft.SolverFoundation.Plugin.Z3.Z3MILPParams"/>
|
||||
|
||||
<MsfPluginSolver name="Microsoft Z3 Term Solver"
|
||||
capability="MILP"
|
||||
assembly="SolverFoundation.Plugin.Z3.dll"
|
||||
interface="Microsoft.SolverFoundation.Services.ITermSolver"
|
||||
solverclass="Microsoft.SolverFoundation.Plugin.Z3.Z3TermSolver"
|
||||
directiveclass="Microsoft.SolverFoundation.Plugin.Z3.Z3TermDirective"
|
||||
parameterclass="Microsoft.SolverFoundation.Plugin.Z3.Z3TermParams"/>
|
||||
<MsfPluginSolver name="Microsoft Z3 Term Solver"
|
||||
capability="LP"
|
||||
assembly="SolverFoundation.Plugin.Z3.dll"
|
||||
interface="Microsoft.SolverFoundation.Services.ITermSolver"
|
||||
solverclass="Microsoft.SolverFoundation.Plugin.Z3.Z3TermSolver"
|
||||
directiveclass="Microsoft.SolverFoundation.Plugin.Z3.Z3TermDirective"
|
||||
parameterclass="Microsoft.SolverFoundation.Plugin.Z3.Z3TermParams"/>
|
||||
<MsfPluginSolver name="Microsoft Z3 Term Solver"
|
||||
capability="MINLP"
|
||||
assembly="SolverFoundation.Plugin.Z3.dll"
|
||||
interface="Microsoft.SolverFoundation.Services.ITermSolver"
|
||||
solverclass="Microsoft.SolverFoundation.Plugin.Z3.Z3TermSolver"
|
||||
directiveclass="Microsoft.SolverFoundation.Plugin.Z3.Z3TermDirective"
|
||||
parameterclass="Microsoft.SolverFoundation.Plugin.Z3.Z3TermParams"/>
|
||||
<MsfPluginSolver name="Microsoft Z3 Term Solver"
|
||||
capability="NLP"
|
||||
assembly="SolverFoundation.Plugin.Z3.dll"
|
||||
interface="Microsoft.SolverFoundation.Services.ITermSolver"
|
||||
solverclass="Microsoft.SolverFoundation.Plugin.Z3.Z3TermSolver"
|
||||
directiveclass="Microsoft.SolverFoundation.Plugin.Z3.Z3TermDirective"
|
||||
parameterclass="Microsoft.SolverFoundation.Plugin.Z3.Z3TermParams"/>
|
||||
</MsfPluginSolvers>
|
||||
</MsfConfig>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
|
||||
</startup>
|
||||
</configuration>
|
|
@ -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("SolverFoundation.Plugin.Z3.Tests")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("SolverFoundation.Plugin.Z3.Tests")]
|
||||
[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("27657eee-ca7b-4996-a905-86a3f4584988")]
|
||||
|
||||
// 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,86 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
using Microsoft.SolverFoundation.Common;
|
||||
using Microsoft.SolverFoundation.Solvers;
|
||||
using Microsoft.SolverFoundation.Services;
|
||||
using Microsoft.SolverFoundation.Plugin.Z3;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace Microsoft.SolverFoundation.Plugin.Z3.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class ServiceTests
|
||||
{
|
||||
[TestInitialize]
|
||||
public void SetUp()
|
||||
{
|
||||
SolverContext context = SolverContext.GetContext();
|
||||
context.ClearModel();
|
||||
}
|
||||
|
||||
private void TestService1(Directive directive)
|
||||
{
|
||||
SolverContext context = SolverContext.GetContext();
|
||||
Model model = context.CreateModel();
|
||||
|
||||
Decision x1 = new Decision(Domain.RealRange(0, 2), "x1");
|
||||
Decision x2 = new Decision(Domain.RealRange(0, 2), "x2");
|
||||
|
||||
Decision z = new Decision(Domain.IntegerRange(0, 1), "z");
|
||||
|
||||
model.AddDecisions(x1, x2, z);
|
||||
|
||||
model.AddConstraint("Row0", x1 - z <= 1);
|
||||
model.AddConstraint("Row1", x2 + z <= 2);
|
||||
|
||||
Goal goal = model.AddGoal("Goal0", GoalKind.Maximize, x1 + x2);
|
||||
|
||||
Solution solution = context.Solve(directive);
|
||||
Assert.IsTrue(goal.ToInt32() == 3);
|
||||
context.ClearModel();
|
||||
}
|
||||
|
||||
private void TestService2(Directive directive)
|
||||
{
|
||||
SolverContext context = SolverContext.GetContext();
|
||||
Model model = context.CreateModel();
|
||||
|
||||
Decision x1 = new Decision(Domain.RealNonnegative, "x1");
|
||||
Decision x2 = new Decision(Domain.RealNonnegative, "x2");
|
||||
|
||||
Decision z = new Decision(Domain.IntegerRange(0, 1), "z");
|
||||
|
||||
Rational M = 100;
|
||||
|
||||
model.AddDecisions(x1, x2, z);
|
||||
|
||||
model.AddConstraint("Row0", x1 + x2 >= 1);
|
||||
model.AddConstraint("Row1", x1 - z * 100 <= 0);
|
||||
model.AddConstraint("Row2", x2 + z * 100 <= 100);
|
||||
|
||||
Goal goal = model.AddGoal("Goal0", GoalKind.Maximize, x1 + x2);
|
||||
|
||||
Solution solution = context.Solve(directive);
|
||||
Assert.IsTrue(goal.ToInt32() == 100);
|
||||
context.ClearModel();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestService1()
|
||||
{
|
||||
TestService1(new Z3MILPDirective());
|
||||
TestService1(new Z3TermDirective());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestService2()
|
||||
{
|
||||
TestService2(new Z3MILPDirective());
|
||||
TestService2(new Z3TermDirective());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
<?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>{280AEE2F-1FDB-4A27-BE37-14DC154C873B}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Microsoft.SolverFoundation.Plugin.Z3.Tests</RootNamespace>
|
||||
<AssemblyName>SolverFoundation.Plugin.Z3.Tests</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.Solver.Foundation">
|
||||
<HintPath>..\Microsoft.Solver.Foundation.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ServiceTests.cs" />
|
||||
<Compile Include="SolverTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SolverFoundation.Plugin.Z3\SolverFoundation.Plugin.Z3.csproj">
|
||||
<Project>{7340e664-f648-4ff7-89b2-f4da424996d3}</Project>
|
||||
<Name>SolverFoundation.Plugin.Z3</Name>
|
||||
</ProjectReference>
|
||||
</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>
|
132
examples/msf/SolverFoundation.Plugin.Z3.Tests/SolverTests.cs
Normal file
132
examples/msf/SolverFoundation.Plugin.Z3.Tests/SolverTests.cs
Normal file
|
@ -0,0 +1,132 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
using Microsoft.SolverFoundation.Common;
|
||||
using Microsoft.SolverFoundation.Solvers;
|
||||
using Microsoft.SolverFoundation.Services;
|
||||
using Microsoft.SolverFoundation.Plugin.Z3;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace Microsoft.SolverFoundation.Plugin.Z3.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class SolverTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void TestMILPSolver1()
|
||||
{
|
||||
var solver = new Z3MILPSolver();
|
||||
int goal;
|
||||
|
||||
solver.AddRow("goal", out goal);
|
||||
int x1, x2, z;
|
||||
|
||||
// 0 <= x1 <= 2
|
||||
solver.AddVariable("x1", out x1);
|
||||
solver.SetBounds(x1, 0, 2);
|
||||
|
||||
// 0 <= x2 <= 2
|
||||
solver.AddVariable("x2", out x2);
|
||||
solver.SetBounds(x2, 0, 2);
|
||||
|
||||
// z is an integer in [0,1]
|
||||
solver.AddVariable("z", out z);
|
||||
solver.SetIntegrality(z, true);
|
||||
solver.SetBounds(z, 0, 1);
|
||||
|
||||
//max x1 + x2
|
||||
solver.SetCoefficient(goal, x1, 1);
|
||||
solver.SetCoefficient(goal, x2, 1);
|
||||
solver.AddGoal(goal, 1, false);
|
||||
|
||||
// 0 <= x1 -z <= 1
|
||||
int row1;
|
||||
solver.AddRow("rowI1", out row1);
|
||||
solver.SetBounds(row1, 0, 1);
|
||||
solver.SetCoefficient(row1, x1, 1);
|
||||
solver.SetCoefficient(row1, z, -1);
|
||||
|
||||
// 0 <= x2 + z <= 2
|
||||
int row2;
|
||||
solver.AddRow("rowI2", out row2);
|
||||
solver.SetBounds(row2, 0, 2);
|
||||
solver.SetCoefficient(row2, x2, 1);
|
||||
solver.SetCoefficient(row2, z, 1);
|
||||
|
||||
var p = new Z3MILPParams();
|
||||
solver.Solve(p);
|
||||
|
||||
Assert.IsTrue(solver.Result == LinearResult.Optimal);
|
||||
Assert.AreEqual(solver.GetValue(x1), 2 * Rational.One);
|
||||
Assert.AreEqual(solver.GetValue(x2), Rational.One);
|
||||
Assert.AreEqual(solver.GetValue(z), Rational.One);
|
||||
Assert.AreEqual(solver.GetValue(goal), 3 * Rational.One);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestMILPSolver2()
|
||||
{
|
||||
var solver = new Z3MILPSolver();
|
||||
int goal, extraGoal;
|
||||
|
||||
Rational M = 100;
|
||||
solver.AddRow("goal", out goal);
|
||||
int x1, x2, z;
|
||||
|
||||
// 0 <= x1 <= 100
|
||||
solver.AddVariable("x1", out x1);
|
||||
solver.SetBounds(x1, 0, M);
|
||||
|
||||
// 0 <= x2 <= 100
|
||||
solver.AddVariable("x2", out x2);
|
||||
solver.SetBounds(x2, 0, M);
|
||||
|
||||
// z is an integer in [0,1]
|
||||
solver.AddVariable("z", out z);
|
||||
solver.SetIntegrality(z, true);
|
||||
solver.SetBounds(z, 0, 1);
|
||||
|
||||
solver.SetCoefficient(goal, x1, 1);
|
||||
solver.SetCoefficient(goal, x2, 2);
|
||||
solver.AddGoal(goal, 1, false);
|
||||
|
||||
solver.AddRow("extraGoal", out extraGoal);
|
||||
|
||||
solver.SetCoefficient(extraGoal, x1, 2);
|
||||
solver.SetCoefficient(extraGoal, x2, 1);
|
||||
solver.AddGoal(extraGoal, 2, false);
|
||||
|
||||
// x1 + x2 >= 1
|
||||
int row;
|
||||
solver.AddRow("row", out row);
|
||||
solver.SetBounds(row, 1, Rational.PositiveInfinity);
|
||||
solver.SetCoefficient(row, x1, 1);
|
||||
solver.SetCoefficient(row, x2, 1);
|
||||
|
||||
|
||||
// x1 - M*z <= 0
|
||||
int row1;
|
||||
solver.AddRow("rowI1", out row1);
|
||||
solver.SetBounds(row1, Rational.NegativeInfinity, 0);
|
||||
solver.SetCoefficient(row1, x1, 1);
|
||||
solver.SetCoefficient(row1, z, -M);
|
||||
|
||||
// x2 - M* (1-z) <= 0
|
||||
int row2;
|
||||
solver.AddRow("rowI2", out row2);
|
||||
solver.SetBounds(row2, Rational.NegativeInfinity, M);
|
||||
solver.SetCoefficient(row2, x2, 1);
|
||||
solver.SetCoefficient(row2, z, M);
|
||||
|
||||
var p = new Z3MILPParams();
|
||||
p.OptKind = OptimizationKind.BoundingBox;
|
||||
|
||||
solver.Solve(p);
|
||||
Assert.IsTrue(solver.Result == LinearResult.Optimal);
|
||||
Assert.AreEqual(solver.GetValue(goal), 200 * Rational.One);
|
||||
Assert.AreEqual(solver.GetValue(extraGoal), 200 * Rational.One);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue