# Need help with Implementing a simple planner in Julia in the context of the blocks world

**URL:** <https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928>\
**Category:** Statistics\
**Tags:** question\
**Created:** [July 12, 2020, 9:09am UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928 "2020-07-12T09:09:35Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moreno](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Moreno](https://discourse.julialang.org/u/Moreno)\
**Post date:** [July 12, 2020, 9:09am UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/1 "2020-07-12T09:09:35Z")

</div>

Consider a collection of blocks and several tables. A block A can be in the box or on  
one of the tables at a given time. While on a table, a block A can be on the surface of the table or  
on top of another block B. The following actions are possible:

1. put a block A on a table;
2. put a block A on top of a block B.  
Note that putting a block on a table is possible if the block is in the box or on top of another block  
on the same table. As well, putting a block on top of another block requires the former block to be  
on the same table as the latter. Finally, there is a limit to the number of blocks one can have on  
the surface of a given table.  
Your task is to write a Julia program that fulfils the following functionality:
3. accepts a user input that defines the world (how many blocks and how many tables) and a  
goal (final layout of blocks on the tables) and generates a plan1  
to achieve the goal;
4. accepts a user input that defines the world, a goal and the current configuration (the current  
layout of blocks on the tables) and generates a plan to achieve the goal.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 13, 2020, 8:36am UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/2 "2020-07-13T08:36:18Z")

</div>

Is this a homework problem?

If yes, does the course specify/suggest a set of packages to use for this, or do you need to code the logic from scratch?

Can you show us an attempt at the solution?

---

<div class="post-metadata">

**Author:** ![Moreno](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Moreno](https://discourse.julialang.org/u/Moreno)\
**Post date:** [July 13, 2020, 6:48pm UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/3 "2020-07-13T18:48:51Z")

</div>

Hi, Thanks for your reply.

Yes it is a home work. No, the course doesn’t specify any packages to be used.  
I am new in Julia, so am kinder lost. I would appreciate if you assist.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 13, 2020, 11:08pm UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/4 "2020-07-13T23:08:03Z")

</div>

Can you be a little more specific on how you are lost? Are you having trouble finding good Julia tutorials? Which other language you know? The problem is the datastructures for the problem? Reading input from the terminal?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 14, 2020, 5:52am UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/5 "2020-07-14T05:52:03Z")

</div>

Your best chance of getting help on this forum is showing some work, explaining where you got stuck, and asking _specific_ questions with code.

I am assuming that you got some kind of introduction to Julia programming in this course. Also, there may be local resources you can turn to to get started, eg a TA. I would try those first.

---

<div class="post-metadata">

**Author:** ![Moreno](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Moreno](https://discourse.julialang.org/u/Moreno)\
**Post date:** [July 14, 2020, 5:56am UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/6 "2020-07-14T05:56:27Z")

</div>

Thanks for your feedback.  
I am a new bie in Julia. I have knowledge of C#. my main challenge is implementing the Algorithms like Say the Markov Process or BFS to solve the problem.

---

<div class="post-metadata">

**Author:** ![Moreno](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Moreno](https://discourse.julialang.org/u/Moreno)\
**Post date:** [July 14, 2020, 5:58am UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/7 "2020-07-14T05:58:35Z")

</div>

Thanks for your feedback .

Yes, i have started with some code will soon post the here for further assistance.  
Could possibly share a link to the TA.I? thanks a million for your support

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 14, 2020, 6:00am UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/8 "2020-07-14T06:00:21Z")

</div>

TA is short for “teaching assistant”, I am assuming your course has one.

---

<div class="post-metadata">

**Author:** ![Moreno](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Moreno](https://discourse.julialang.org/u/Moreno)\
**Post date:** [July 14, 2020, 6:09am UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/9 "2020-07-14T06:09:12Z")

</div>

Unfortunately we don’t have that.

---

<div class="post-metadata">

**Author:** ![Moreno](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Moreno](https://discourse.julialang.org/u/Moreno)\
**Post date:** [July 15, 2020, 7:28am UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/11 "2020-07-15T07:28:55Z")

</div>

Hi, so i have tried to prompt the tables and the blocks from the user and now am stuck with how to implement the preconditions, and the goal. Also, i have attempted to write the depth Search to fit my scenario but some how am stuck . Here is my code

using Statistics

using Random

println(“-----------------------”)

println(“- BLOCK WORLD PLANNER -”)

println(“-----------------------”)

print(“”)

function initialState()

```
#prompt number of tables

print("Enter number of tables : ")

teb = chomp(readline())

global num_tables = parse(Int, teb)

#Collection to store tables in array

global tableCollection = Array{String}(undef, num_tables)

for i ∈ 1:num_tables #

    println()

    print("Enter Table $i : ")

    tableCollection[i] = chomp(readline()) #prompt tables 

    println()  

end

println("#####################################")

print("Enter number of Blocks: ")

blc = chomp(readline())

global num_blocks = parse(Int, blc)

global blockCollection = Array{String}(undef, num_blocks)

for i ∈ 1:num_blocks 

    println()

    print("Enter Block $i : ")

    blockCollection[i] = chomp(readline()) #blocks

    

end

#return tableCollection,blockCollection

```

end

######################################################

function problem\_generator(tableCollection, blockCollection)

```
l = tableCollection

b = collect('A':'Z')

list_blocks = 3

```

end

#####################################################

function bredthFirstSearch(initialState,goalState)

```
queue=[]

noOfExpadedNodes = 0

queue.append(initialState)

while queue

    currentState = queue.pop(0)

    #print new queue is

    #print"searching at",currentState.depth

    if currentState.isGoal(goalState)

        #print"goal"

        #print str(currentState)

        return noOfExpadedNodes,currentState

    end

    noOfExpadedNodes = noOfExpadedNodes + 1

    newStateList = currentState.getNewState()

    for newSate in newStateList

        queue.append(newSate)

        newSate.prevState = currentState

        newSate.depth = currentState.depth + 1

    end

    return -1,-1

end

```

end

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 15, 2020, 7:40am UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/12 "2020-07-15T07:40:52Z")

</div>

> [@Moreno](#):
>
> Here is my code

> [@Moreno](#):
>
> ` if currentState.isGoal(goalState)`

Are you sure this is meant to be Julia code? It looks like C++ or similar.

---

<div class="post-metadata">

**Author:** ![Moreno](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Moreno](https://discourse.julialang.org/u/Moreno)\
**Post date:** [July 15, 2020, 8:44am UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/13 "2020-07-15T08:44:33Z")

</div>

Like i said i am a newbie in julia

---

<div class="post-metadata">

**Author:** ![Moreno](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Moreno](https://discourse.julialang.org/u/Moreno)\
**Post date:** [July 15, 2020, 8:46am UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/14 "2020-07-15T08:46:15Z")

</div>

Yes its a julia code

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [July 15, 2020, 10:56am UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/15 "2020-07-15T10:56:47Z")

</div>

In Julia, you typically don’t write `objectA.function(objectB)`. (You could write that, but only if you have a reason to store a function in an object.)

The normal Julia syntax would be `function(objectA, objectB)`. The first argument does not play any special role in Julia like it does in many object oriented languages.

In your specific case the code `currentState.isGoal(goalState)` should therefore probably read `isGoal(currentState, goalState)`. However, it seems the function `isGoal` is not yet defined in your code.

Also, to make your code look nice, put tripple backticks around it in your post, like this:

````julia
```
function foo(x)
    sqrt(5x)
end
```

````

This will make it show up with syntax highlighting

```julia
function foo(x)
    sqrt(5x)
end

```

---

<div class="post-metadata">

**Author:** ![Moreno](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Moreno](https://discourse.julialang.org/u/Moreno)\
**Post date:** [July 15, 2020, 10:52pm UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/16 "2020-07-15T22:52:04Z")

</div>

Thank you for your reply man.

---

<div class="post-metadata">

**Author:** ![Moreno](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Moreno](https://discourse.julialang.org/u/Moreno)\
**Post date:** [July 15, 2020, 11:08pm UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/17 "2020-07-15T23:08:05Z")

</div>

So i came out with this approach of prompting all the blocks and tables from the keyboard and store them in some random arrays which can act as an initial state or problem and later the goal is to have this blocks arranged in tables in an alphabetical order using Backward Search the program should make all the preconditions true using backwards checking and , then it will use forward checking to get to the goal state.

---

<div class="post-metadata">

**Author:** ![Moreno](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Moreno](https://discourse.julialang.org/u/Moreno)\
**Post date:** [July 16, 2020, 12:45am UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/18 "2020-07-16T00:45:15Z")

</div>

Hi guys as i explained above, the function initialState prompts the tables and blocks from the keyboard and store them in array and later define the goal State of how the blocks were suppose to be arranged on tables.  
now i would like to implement backtracking(Depth First search ) and fowardtraking (Breadth First search ) algorithms to get to the goal defined in the initialState functions any help on how i should implement this algorithms in julia? and also i would lie to have the following preconditions to be evaluated to true to guide my program  
‘’‘[“clearTable”,“pickUp”,“putInBox”,“putOnTable”,“putOnTop”,“moveToTable”,“on”]’‘’

````nohighlight

    #prompt number of tables

    print("Enter number of tables : ")

    teb = chomp(readline())

    global num_tables = parse(Int, teb)

    #Collection to store tables in array

    global tableCollection = Array{String}(undef, num_tables)

    for i ∈ 1:num_tables #

        println()

        print("Enter Table $i : ")

        tableCollection[i] = chomp(readline()) #prompt tables 

        println()  

    end

    println(" *************************************")

    print("Enter number of Blocks: ")

    blc = chomp(readline())

    global num_blocks = parse(Int, blc)

    global blockCollection = Array{String}(undef, num_blocks)

    for i ∈ 1:num_blocks 

        println()

        print("Enter Block $i : ")

        blockCollection[i] = chomp(readline()) #blocks

        

    end

    #return tableCollection,blockCollection

    println(" *********************************************")

    println(" **Initial state configuration of blocks**")

    println(" *********************************************")

    for i ∈ 1:num_blocks

        println(blockCollection[i])

    end

    println("")

    println(" ***********************")

    println(" **Number of tables**")

    println(" ***********************")

    for i ∈ 1:num_tables

        println(tableCollection[i])

    end

    println(" **********************")

    println("")

    println(" *******************")

    println(" **Goal state***")

    println(" *******************")

    goalState = sort!(blockCollection)

    print(goalState)

    println(" ****************")

end```
````

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 16, 2020, 10:25am UTC](https://discourse.julialang.org/t/need-help-with-implementing-a-simple-planner-in-julia-in-the-context-of-the-blocks-world/42928/19 "2020-07-16T10:25:02Z")

</div>

I would suggest you break up your problem to a small set of functions manipulating the state of the world and the planner, each doing one little thing, and then test and develop with variables that represent inputs, and see if they return the desired output, and then combine them.

Reading user input from the command line is probably the last thing I would write.

This is not my field so I am sorry but I cannot help with the specifics, but if I wanted to learn classic AI in Julia, I would just implement everything in

> Norvig, Peter. _Paradigms of artificial intelligence programming: case studies in Common LISP_ . Morgan Kaufmann, 1992.

in Julia (using `Vector`s for flat lists, and other appropriate structures instead of `CONS`es, but the algorithms are pretty easy to port).

I am curious what resources you were assigned for this course, can you link the course website or a syllabus?
