Τρίτη 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.

Δεν υπάρχουν σχόλια: