Last active
December 17, 2015 13:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes | |
pysmilebridge = ctypes.CDLL("/path/to/pysmilebridge/libpysmilebridge.so") | |
pysmilebridge.CreateNetwork() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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