How to use Microsoft Infer.net in Unity 2017

106 Views Asked by At

I am using Unity 2017.2. The Infer.Net framework requires .NET 4.0 and Unity can use only assemblies that target .NET 3.5. How do I do that?

1

There are 1 best solutions below

0
On

In Unity 2017.2 you can go to:

Edit -> Project Settings -> Player -> Other Settings -> Configuration -> API Compatibility Level

and switch to "Experimental .NET 4.6".

This is your test code:

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 () {

    }
} 

You need to add only Infer.Compiler.dll and for some strange reason FSharp.Core 4.3.0 to Unity's Assembly-CSharp project. For the FSharp assembly install the F# Language pack in Visual Studio. Then copy the assembly from:

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

to your Assets folder and reference it in your C# project. Add the above script to a game object of your choice.

Check the console in Unity for the output messages of the above program.