Πέμπτη 3 Μαΐου 2012

Propositional Clausal Logic in Oz

Okay,

Just a little update. I wanted to see how one could implement a model searcher/theorem prover in Oz. I found some very useful presentation slides for Declarative Programming by Coen De Roover at:
http://prog.vub.ac.be/~cderoove/declarative_programming/
 Which I use as a guide for what I am trying to accomplish. Up to now, I have managed to create a PCL model searcher that can do proof by refutation using resolution. Here is some spoiler code:



% Set up a new Propositional Clausal Logic Knowledge base


PCLKB = {New KnowledgeBase init(PCL)}


% Assert some facts in it.


{List.forAll
 [
  [happy ':-' has_friends]
  [friendly ':-' happy]
  [wet ':-' rains]
  [':-' wet]
 ]
 proc {$ I}
    {PCLKB assert(I)}
 end
}


% Prove takes place with resolution by refutation


{List.forAll
 [
  [friendly ':-' has_friends]
  [friendly]
  [':-' rains]
 ]
 proc {$ I}
    {Browse prove(I)}
    {Browse
     {PCLKB prove(I $)}
    }
 end
}

and the necessary browser output:

prove([friendly ':-' has_friends])
true
prove([friendly])
false
prove([':-' rains])
true
For the moment, I will move to relational clausal logic so I am not going to bother with fixing up and releasing the code, unless someone asks for it of course. 

Τρίτη 1 Μαΐου 2012

Set of Subsets in Oz and other stuff

Hello,

I think it is time to start posting to this blog again. I will try and keep it updated with things that have more or less bothered me and other people will more likely find them in their way. These posts will mainly concern Oz and its implementation Mozart, stuff in Inductive Logic Programming, and things about Digital Music .

Generally, what are some interesting things I have been up to these last months:

  • Re-factoring the code I have published with my Diploma Thesis. I am planning to re-create it at last as a pure Oz/Strasheela implementation (I have done some progress on that). 
  • Functional AUdio STream: Faust is a functional programming language that allows rapid development of efficient digital music instruments in C++. It allows easy implementation of VST technology instruments and effects, as well as PureData, etc.
  • Fun with wavelets and music.
I am also searching for postgraduate studies in the fields of digital music/music technology.

I will generally update my blog mainly with progress on the above.

To begin, I have come to the following problem in Oz:
Given a list of distinct elements L, give me a list that contains all the possible sub-lists with distinct elements of L. 
This could be helpful, for example if you want to, given a set S, to construct the powerset of S.

So, I have come to the following implementation. I hope someone finds that useful:


fun {SearchSubsets L N}
   {SearchAll
    proc {$ Sol}
       SolT in
       SolT = {FD.list N 1#{List.length L}}
       {FD.distinct SolT}
       for K in 1..{List.length SolT}-1 do
 {Nth SolT K} <: {Nth SolT K+1}
       end
       {FD.distribute naive SolT}
       {List.map SolT fun {$ I} {Nth L I} end Sol}
    end
   }
end
fun {SearchAllSubsets L N}
   case N of
      0 then
      nil|nil
   else
      {List.append
       {SearchSubsets L N}
       {SearchAllSubsets L N-1}
      }
   end
end


As you can see here, I implemented it using a search strategy and finite domain constraints. I will change it to a purely algorithmic one.

What do the functions do?

  • {SearchSubsets L N}: Returns the subsets of L with exactly N elements. For example, given L=[a b c] and N=2 it will return [a b], [b c] and [a c].
  • {SearchAllSubsets L N}: Returns the subsets of L with at most N elements. In the example above, it will return [a b], [b c] and [a c] as well as [a], [b], [c] and the empty set nil.

In order to produce the powerset, we must call SearchAllSubsets as {SearchAllSubsets L {List.length L}}.

i.e if we feed the following:
S = [a b c d]
{Browse {SearchAllSubsets S {List.length S}}}
we will get in the browser window the list with 16 elements:
[[a b c d] [a b c] [a b d] [a c d] [b c d] [a b]
 [a c] [a d] [b c] [b d] [c d] [a] [b] [c] [d] nil]
OK, I think that is all for now. Stay tuned.

P.S. Is there a way to easily embed code to blogspot posts?

Edit: I just wrote a dummy Propositional Clausal Logic (PCL) model searcher in Oz. I will return to this once it's in a usable form.

Σάββατο 8 Οκτωβρίου 2011

Sooperlooper loses connection to engine

If it happens that sooperlooper shows you this dialog:

Lost connection to SooperLooper engine.
See the Preferences->Connections tab to start a new one




on newer distributions (I tried it with Fedora 16 beta 1), add your
. (substitude with your host and with your domain) to your `/etc/hosts' file on the line for 127.0.0.1 and try again.

For example, my hostname is `mmxgn' and domain is `emergencia' so my hosts file was:

127.0.0.1 localhost.localdomain localhost


and I added mmxgn.emergencia next to localhost.

127.0.0.1 localhost.localdomain localhost mmxgn.emergencia


Now when I run `slgui' I can use sooperlooper without the engine dying.






Τρίτη 7 Ιουνίου 2011

Guitar Pro 6 on Fedora 15

I had some problems installing Guitar Pro 6 for Linux on Fedora 15.
The problem was that, after extracting the .deb file on the cd, on /opt/ the updater would fail when I ran it as a user because of wrong permissions, and if I chmodded 777 the Guitar Pro directory, or ran with sudo, It would not run at all.

So, what I did:

1. I ran ./GPUpdater as the regular user, with the original directory permissions
2. While running, I chmodded 777 /opt/GuitarPro6
3. Updated

And finally (I'm at the third step right now) changing to the original 755 permissions.

Παρασκευή 30 Ιουλίου 2010

Mozart/Oz and Gedit

Mozart/Oz is a very powerful language, which I recently started using.

Unfortunately, the most efficient way to use it, is from within emacs and the OPI.

I tried to find a way to use it with other editors, even for simple functionality like syntax highlighting but with no success.

Today, I tried to make a syntax `.lang' file for gedit, for Mozart/Oz so I took a .lang file
I found in `/usr/share/gtksourceview-2.0/language-specs/' (`scheme.lang'), modified it
and there I have a far-from-complete-yet-working `.lang' file for Mozart/Oz and gedit, as well some `tools' for the `external-tools' plugin (compile & compile-run with and respectively).

If you want to use it, access my github at:http://github.com/mmxgn/mozart-stuff/tree/master/mozart-gedit/

Download mozart-gedit.tar.gz, untar-gzip it and read `INSTALL'.


Here's a screenshot:



Πέμπτη 4 Φεβρουαρίου 2010

Calculating conditional pmf matrices in python (numpy)

Here is the thing that has tormented me for some time now.

I have a conjunctive probability table, with shape, for example (1,2,3,4,5,6) .
And I want to calculate the probability table, conditional to a value for some of the dimensions, for decision-making purposes. We define the values we want

The code I came up with at the moment is the following (the input is the dictionary "vdict" of the form {'variable_1': value_1, 'variable_2': value_2 ... } )


for i in vdict:
dim = self.invardict.index(i) # The index of the dimension that our Variable resides in
val = self.valdict[i][vdict[i]] # The value we want it to be
d = d.swapaxes(0, dim)
d = array([d[val]])
d = d.swapaxes(0, dim)
...


So, what I currently do is:

1. I translate the variables to the corresponding dimension in the cpt.
2. I swap the zero-th axis with the axis I found before.
3. I replace whole 0-axis with just the desired value.

I put the dimension back to its original axis.

Now, the problem is, in order to do step 2, I have (a.) to calculate a submatrix
and (b.) to put it in a list and translate it again to array so I'll have my new array.

Thing is, stuff in bold means that I create new objects, instead of using just the references to the old ones and this, if d is very large (which happens to me) and methods that use d are called many times (which, again, happens to me) the whole result is very slow.

So, has anyone come up with an idea that will subtitude this little piece of code and will run a lot faster? Maybe something that will allow me to calculate the conditionals in place.

Edit: I replaced the command in bold, with the code below:

d = conditionalize(d, dim, val)
where:

def conditionalize(arr, dim, val):
arr = arr.swapaxes(dim, 0)
shape = arr.shape[1:] # shape of the sub-array when we omit the desired
count = array(shape).prod() # count of elements omitted the desired dimension.
arr = arr.reshape(array(arr.shape).prod()) # flatten the array in-place.
arr = arr[val*count:(val+1)*count] # take the needed elements
arr = arr.reshape((1,)+shape) # the desired sub-array shape.
arr = arr. swapaxes(0, dim) # fix dimensions
return arr
Now, what before took 15 minutes to complete, now takes only about 6 seconds!

Τρίτη 22 Δεκεμβρίου 2009

cast from 'void*' to 'int' loses precision

Irritating message, it happens because:

int a = sizeof(b)

is bad, where:

size_t a = sizeof(b)

is better.

So if you happen to try to compile something and it gives you this message, try to figure out what tries to make it an int and change it to a size_t.