Jesus. You have no idea how much did you help me! That's exactly where the 
bug was.
Now I made it work, but I simply did not expect it to be so friggin slow. 
As you could see, I hardcoded the paramethers in the "boss" function, 
because all I wanted, for now, is to make it work correctly. 
I left it running for half a hour circa, and I obtained, like ~330 
different board configurations and still counting, since there were, like, 
other ~400 still to be processed.
I simply did not believe that I'd spend THAT much of time to calculate a 
tree where the absolute maximum of times I would have to launch a worker 
would be about 16!/9! .
Now, I reduced the board to a 3x3 one with only 3 chess pieces. It's absurd 
that it has to take this much.
And my assignment was to calculate the whole tree of possibilities given a 
7x7 board with 7 chesspieces...

Since I believe that I'm doing something wrong, is there some jvm profiling 
instrument that's compatible with clojure code? Maybe I'm parrallelizing in 
a wrong way...

On Monday, April 25, 2016 at 4:44:10 AM UTC+2, James Reeves wrote:
>
> It's hard to tell for sure from your code sample, because you don't 
> include "place" or "get-free-squares", but it's possible your error is in 
> the reducing function here:
>
> (reduce #(when (not (keyword? %2))
>            (conj! %1
>                   (if nxt
>                     {:board %2 :pawns (update pwns nxt dec)}
>                     brd)))
>         (transient [])
>         (map #(place nxt (first %) (last %) brd)
>              (get-free-squares brd)))
>
> In your reducing function, if %2 is not a keyword, then you return nil. 
> This means that the next %1 will be nil, and you'll end up with a null 
> pointer exception.
>
> I wonder if you meant instead to write:
>
> (reduce #(if (not (keyword? %2))
>            (conj! %1
>                   (if nxt
>                     {:board %2 :pawns (update pwns nxt dec)}
>                     brd))
>            %1)
>         (transient [])
>         (map #(place nxt (first %) (last %) brd)
>              (get-free-squares brd)))
>
> - James
>
>
> On 25 April 2016 at 01:26, Vandr0iy <vandr0iy...@gmail.com <javascript:>> 
> wrote:
>
>> Hi, Clojurists!
>>
>> I'm writing some clojure in these days, and I stumbled upon an error that 
>> I'm unable to easily understand.
>> My goal is to asynchronously process a 2D vector in order to solve a 
>> parametric version of the 8 chess queens problem (where the parameters are: 
>> the board's dimensions and the chess pieces to be put on the board).
>> My solution mainly consists in two functions: the worker and the boss. 
>> The worker takes in input a board (2d array) and yields a collection of 
>> boards that contain another chess piece placed in a certain way. This means 
>> that I:
>> > take all of the free squares from the board
>> > try to place the chesspiece on every one of them
>> > if I succeed (do not hit other pieces) - it goes to the output.
>>
>> Until I use the version without the transients - it appears to be 
>> working. 
>> But I wanted to optimize my code, because the last time I solved it using 
>> recursive pmap - when I gave it an output that's a bit larger - it went 
>> into a bats**t crazy heap overflow. So I tried to use a transient 
>> collection.
>> And here my problems began. When I try to execute this code it dies after 
>> just a couple of moves processed, giving me a nullpointer on the row where 
>> conj! is situated:
>>
>> NullPointerException   clojure.core/conj! (core.clj:3257)
>>
>> What a hellish issue may it be? Clojure is not helping - its error 
>> messages are always cryptic, and really hard to understand.
>>
>> Here is my code. It's not complete though - some functions are omitted, 
>> for the sake of brevity. Please, help me to understand what am I missing 
>> here...
>>
>> Thank you.
>> (def fw (agent {}))
>>
>> (defn worker [subj]
>>   (let [{brd :board pwns :pawns} subj
>>         nxt (get-next-pawn pwns)]
>>     (when (map? subj)
>>       (when (and brd pwns)
>>          (into #{}
>>                (persistent!
>>                 (reduce #(when (not (keyword? %2))
>>                            (conj! %1
>>                                   (if nxt
>>                                     {:board %2 :pawns (update pwns nxt 
>> dec)}
>>                                     brd)))
>>                         (transient [])
>>                         (map #(place nxt (first %) (last %) brd)
>>                              (get-free-squares brd)))))))))
>> (defn boss []
>>
>>       (send fw
>>             #(into {} %2)
>>             {:complete 0
>>              :coll
>>              #{{:board (blank-board 4 4)
>>                 :pawns
>>                 {:rook 1
>>                  :knight 3
>>                  :bishop 2
>>                  }}}})
>>          (loop []
>>            (await fw)
>>            @fw
>>            (let [{done :complete col :coll} @fw]
>>              (if (= done (count col))
>>                col
>>                (do (send-off fw
>>                              #(identity {:complete (+ (:complete %1)
>>                                                       (count (filter 
>> vector? %2)))
>>                                          :coll (into (:coll %1) %2)})
>>                              (reduce #(into %1 %2)
>>                                      #{}
>>                                      (pmap #(worker %) col)))
>>                    (recur))))))     
>>
>>
>> -- 
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clo...@googlegroups.com 
>> <javascript:>
>> Note that posts from new members are moderated - please be patient with 
>> your first post.
>> To unsubscribe from this group, send email to
>> clojure+u...@googlegroups.com <javascript:>
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to clojure+u...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to