如何在Unity 2017中使用Microsoft Infer.net

时间:2017-12-10 23:51:37

标签: unity3d probability bernoulli-probability

我正在使用Unity 2017.2。 Infer.Net框架需要.NET 4.0,而Unity只能使用面向.NET 3.5的程序集。我该怎么做?

1 个答案:

答案 0 :(得分:2)

在Unity 2017.2中,您可以访问:

编辑 - >项目设置 - >播放器 - >其他设置 - >配置 - > API兼容级别

并切换到“Experimental .NET 4.6”。

这是您的测试代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using System;
using System.Text;
using MicrosoftResearch.Infer.Models;
using MicrosoftResearch.Infer;

public class TestInferNet : MonoBehaviour {

    // Use this for initialization
    void Start () {

        Debug.Log("Start Infer.net");

        Variable<bool> firstCoin = Variable.Bernoulli(0.5).Named("firstCoin");
        Variable<bool> secondCoin = Variable.Bernoulli(0.5).Named("secondCoin");
        Variable<bool> bothHeads  = (firstCoin & secondCoin).Named("bothHeads");

        InferenceEngine ie = new InferenceEngine();

        if (!(ie.Algorithm is VariationalMessagePassing))
        {
            Debug.Log("Probability both coins are heads: "+ie.Infer(bothHeads));
            bothHeads.ObservedValue=false;
            Debug.Log("Probability distribution over firstCoin: " + ie.Infer(firstCoin));
        }
        else
            Debug.Log("This example does not run with Variational Message Passing");

        Debug.Log("Done start");

    }

    // Update is called once per frame
    void Update () {

    }
} 

你需要只将Infer.Compiler.dll和FSharp.Core 4.3.0的一些奇怪的原因添加到Unity的Assembly-CSharp项目中。对于FSharp程序集,在Visual Studio中安装F#语言包。然后从以下位置复制程序集:

C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\4.3.0.0

到您的Assets文件夹并在C#项目中引用它。将上述脚本添加到您选择的游戏对象中。

检查Unity中的控制台以获取上述程序的输出消息。