Skip to content

Instantly share code, notes, and snippets.

@bitwit
Last active December 17, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitwit/5621234 to your computer and use it in GitHub Desktop.
Save bitwit/5621234 to your computer and use it in GitHub Desktop.
Bridging Python and SMILE C++ Libraries For Linux Smile library availiable at: http://genie.sis.pitt.edu/index.php/downloads version used: Linux (x64) / gcc 4.4.5, March 6th 2013
CXX= g++
CXXFILES= pysmilebridge.cpp
CXXINTERMEDIATE= pysmilebridge.o
CXXOUTPUTLIBS= libpysmilebridge.so
CXXFLAGS= -O3 -DNDEBUG -ffast-math -Wall -fPIC
LIBS = -L/path/to/smile/ -lsmile
all: lib
lib:
$(CXX) $(CXXFLAGS) $(LIBS) -c $(CXXFILES)
$(CXX) -shared -o $(CXXOUTPUTLIBS) $(CXXINTERMEDIATE) $(LIBS)
import ctypes
pysmilebridge = ctypes.CDLL("/path/to/pysmilebridge/libpysmilebridge.so")
pysmilebridge.CreateNetwork()
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
#include "smile/smile.h"
extern "C" {
void CreateNetwork(void) {
DSL_network theNet;
// create node "Success"
int success = theNet.AddNode(DSL_CPT,"Success");
// setting number (and name) of outcomes
DSL_idArray someNames;
someNames.Add("Success");
someNames.Add("Failure");
theNet.GetNode(success)->Definition()->SetNumberOfOutcomes(someNames);
// create node "Forecast"
int forecast = theNet.AddNode(DSL_CPT,"Forecast");
// setting number (and name) of outcomes
someNames.Flush();
someNames.Add("Good");
someNames.Add("Moderate");
someNames.Add("Poor");
theNet.GetNode(forecast)->Definition()->SetNumberOfOutcomes(someNames);
// add arc from "Success" to "Forecast"
theNet.AddArc(success,forecast);
// now fill in the conditional distribution for node "Success" using
// direct method. The probabilities are:
// P("Success" = Success) = 0.2
// P("Success" = Failure) = 0.8
DSL_doubleArray theProbs;
theProbs.SetSize(2); // it has to be an array
theProbs[0] = 0.2;
theProbs[1] = 0.8;
theNet.GetNode(success)->Definition()->SetDefinition(theProbs);
// now fill in the conditional distribution for node "Forecast" using a system of
// coordinates. The probabilities are:
// P("Forecast" = Good | "Success" = Success) = 0.4
// P("Forecast" = Moderate | "Success" = Success) = 0.4
// P("Forecast" = Poor | "Success" = Success) = 0.2
// P("Forecast" = Good | "Success" = Failure) = 0.1
// P("Forecast" = Moderate | "Success" = Failure) = 0.3
// P("Forecast" = Poor | "Success" = Failure) = 0.6
DSL_sysCoordinates theCoordinates (*theNet.GetNode(forecast)->Definition());
theCoordinates.UncheckedValue() = 0.4;
theCoordinates.Next();
theCoordinates.UncheckedValue() = 0.4;
theCoordinates.Next();
theCoordinates.UncheckedValue() = 0.2;
theCoordinates.Next();
theCoordinates.UncheckedValue() = 0.1;
theCoordinates.Next();
theCoordinates.UncheckedValue() = 0.3;
theCoordinates.Next();
theCoordinates.UncheckedValue() = 0.6;
theNet.WriteFile("tutorial.dsl");
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment