Reusable simulation run

Typically: "How do I... ", "How can I... " questions
Post Reply
ArthurS
Posts: 28
Joined: 25 Jul 2021, 20:00

Reusable simulation run

Post by ArthurS »

Hey! Points are spawned in my scene as sim.loadModel().
Drones fly to these points. Time is counted.
After the end of the flight, the simulation ends, the timer is displayed in the console. I need to run> 100 experiments. How can i do this? The problem is that 100+ points will spawn in more than 7 seconds, which makes this process very difficult when it comes to looping this process in 1 simulation. Perhaps you can somehow sequentially run 100 simulations in a row without clicking the mouse on the "run simulation" button

coppelia
Site Admin
Posts: 10336
Joined: 14 Dec 2012, 00:25

Re: Reusable simulation run

Post by coppelia »

Hello,

I am not sure I understand where the bottleneck lies... is it because at simulation start, you dynamically load a lot of models?
If you have n simulations to run, you would normally start/stop a simulation n times. Depending on the content of the scene, the start/stop process might add some delays. So if those delays can somehow be reduced then I'd continue with the start/stop simulation approach. But for that we need to understand where the delays come from.

If those delays cannot be reduced, then you should try to run your n simulations within a single and same simulation, i.e. start the simulation only once. To do this, you will have to be able to reset your models n times, e.g. when a certain event occurs. You'll also need a new simulation time function that computes the n-th simulation time from sim.getSimulationTime(). And you'll need to reset your models to an initial pose n times. In CoppeliaSim V4.3.0 you will be able to find such an example scene in teleportDynamicModel.ttt, but until release of that version, you can find it here.

Cheers

ArthurS
Posts: 28
Joined: 25 Jul 2021, 20:00

Re: Reusable simulation run

Post by ArthurS »

Thanks for the answer. I will clarify about the bottleneck. I call the SpawnPoint() function n times. This is how I generate points on the stage (each of which is a ball model).

Code: Select all

function SpawnPoint()
 
    POINTArray[#POINTArray+1]=sim.copyPasteObjects({point},0)[1]
    local position=RANDTable[#POINTArray]
    sim.setObjectPosition(POINTArray[#POINTArray],-1,position)
    --sim.switchThread()
end
With n = 100, it takes about 20-25 seconds of program runtime (simulation)
Let me remind you that I need to do 100 experiments and spawn these points 100 times. With such slowness, it can take a very long time ...

coppelia
Site Admin
Posts: 10336
Joined: 14 Dec 2012, 00:25

Re: Reusable simulation run

Post by coppelia »

Ok, now I understand a bit better. You have several possibilities.

First of all, if your script runs threaded, then you should forbit thread switches during the creation of the points:

Code: Select all

function sysCall_init()
    corout=coroutine.create(coroutineMain)
end

function sysCall_actuation()
    if coroutine.status(corout)~='dead' then
        local ok,errorMsg=coroutine.resume(corout)
        if errorMsg then
            error(debug.traceback(corout,errorMsg),2)
        end
    end
end

function SpawnPoint()
    POINTArray[#POINTArray+1]=sim.copyPasteObjects({point},0)[1]
    local position=RANDTable[#POINTArray]
    sim.setObjectPosition(POINTArray[#POINTArray],-1,position)
end

function coroutineMain()

    ...

    -- temporarily forbid automatic thread switches:
    local l=sim.setThreadAutomaticSwitch(false)
    for i=1,100,1 do
        SpawnPoint()
    end
    -- Restore the automatic thread switching:
    sim.setThreadAutomaticSwitch(l)
    
    ...
    
end
Next, you can save a copy operation at each point creation if you use sim.saveModel and sim.loadModel, saving and loading to/from buffer, e.g.:

Code: Select all

function sysCall_init()
    corout=coroutine.create(coroutineMain)
end

function sysCall_actuation()
    if coroutine.status(corout)~='dead' then
        local ok,errorMsg=coroutine.resume(corout)
        if errorMsg then
            error(debug.traceback(corout,errorMsg),2)
        end
    end
end

function SpawnPoint(modelData)
    POINTArray[#POINTArray+1]=sim.loadModel(modelData)
    local position=RANDTable[#POINTArray]
    sim.setObjectPosition(POINTArray[#POINTArray],-1,position)
end

function coroutineMain()

    local serializedModel=sim.saveModel(originalModel)

    ...

    -- temporarily forbid automatic thread switches:
    local l=sim.setThreadAutomaticSwitch(false)
    for i=1,100,1 do
        SpawnPoint(serializedModel)
    end
    -- Restore the automatic thread switching:
    sim.setThreadAutomaticSwitch(l)
    
    ...
    
end
For above to work, you will need to have a model first. You can do it via the API like:

Code: Select all

    local saveProp=sim.getModelProperty(originalObject)
    sim.setModelProperty(originalObject,0) -- this make a model from originalObject
    local serializedModel=sim.saveModel(originalObject)
    sim.setModelProperty(originalObject,saveProp)
And of course, if you simply need visualization of spheres, you can also use drawing objects:

Code: Select all

    local sphereContainer=sim.addDrawingObject(sim.drawing_spherepoints,0.1,0,-1,100,color)
    
    while true do
        sim.addDrawingObjectItem(sphereContainer,nil) -- clear the container
        for i=1,100,1 do
            local position={math.random(),math.random(),math.random()}
            sim.addDrawingObjectItem(sphereContainer,position)
        end
    end
Cheers

ArthurS
Posts: 28
Joined: 25 Jul 2021, 20:00

Re: Reusable simulation run

Post by ArthurS »

Hello. I am sending you the main scene and 3 models that are used there.
I will provide access to the folder on the google drive. https://drive.google.com/drive/folders/ ... sp=sharing
You should scale this folder to the path "C:\Program Files\CoppeliaRobotics\CoppeliaSimEdu\projects" (or otherwise change some of the loading paths in the MAIN file).
Start MAIN. It contains that problematic part of the program.
Scripts contains everything you need.

Post Reply