MuJoCo in Python: Changing position and spawning objects at runtime - python

I'm a student and I'm trying to use MuJoCo for a study project at my university with Reinforcement Learning.
I'm looking for some help with spawning objects and moving objects at runtime in Python.
In general, I am just wondering if it is possible to simply change the position in Python of an object or geom by overwriting the position variable of the geom inside the mjData or mjModel class at runtime?
Additionally, I wonder if it is possible to spawn or duplicate an existing object at runtime? If yes for either of those, I would be very happy if you could provide some help :)
I went through different issue threads and forum posts for hours by now, but couldn't find anything. Even if it is not possible at all, I would very much appreciate having an answer to this :))

Related

AttributeError: 'int' object has no attribute 'value' - Issue Concerning "Neural Style Audio Transfers"

Edit #1:
Thanks to the keen insights of #Chaos_is_harmony a bit of deprecated Tensorflow syntax has been discovered:
net.get_shape()
whereas it is now
net.ref()
While this resolves the question that is the topic of this post, it does prompt a new, perhaps related error:
'Reference' object is not iterable
Original post:
Hello hello StackOverflow,
I'm an amateur musician who's not particularly savvy at programming (took a couple Python courses at Uni over a decade ago and haven't gone any further, let alone engaged with it, since), but returned to it today, and have been trying all afternoon, to run Dmitri Ulyanov's "Neural Style Audio Transfer" in order to generate and iterate samples from pre-existing bits of sound design I've done, or to convolve what I've done with what others have done in styles I'm less proficient at. It seemed more or less usable without much coding so I thought appropriate to try. In any case, Dmitri's project's details may be found here: https://github.com/DmitryUlyanov/neural-style-audio-tf
That is what I'm kinda good at, not programming nor assessing code, so I must apologize for my ignorance in advance, both in my description of the issue and my ability to track/implement your responses should I receive any. Nonetheless, there were a few extant issues with running the notebook I was able to resolve by importing "tensorflow.compat.v1 as tf". But it did not fix all the issues.
I am stuck at one error in the "Optimize" portion of the notebook:
AttributeError: 'int' object has no attribute 'value'
Here's how it looks in context (I'm not sure which part of the code is relevant so as to copy it directly in here, I'm sorry...):
https://imgur.com/a/CUMynHM
Here's a video of the notebook being run, cell by cell if that's helpful:
https://streamable.com/tqzmwf
I'm not sure what else might be helpful to mention other than I'm using Python 3 as the kernel (as indicated in the video.) Hopefully I've not given you all too little to go on here, as again, I've sort of jumped into the deep end of things I really know almost nothing about. If it were more than just this one error I would have let it go and not bothered you all; but I also thought it prudent to ask this in posterity for other amateur musicians who may need some guidance in how to use ML tools for music creation and might thus come across this post in the future. All silly justification that aside, let me know if there's anything more I can detail!
Much obliged~!
According to the Tensforflow docs for get_shape(), it says it is a deprecated method and should instead be replaced with ref().

How to check if a given memory location is available for use in Python

I am at a strong beginner level in Python. Recently, I came up with a question that had put me in a deep thought:
Is there any way through which I may get to know whether a given memory address (in integer or string) is occupied or free for use in Python?
This might have been possible with languages which are designed keeping in mind a proper memory model like c and c++, but when it comes to such a dynamically typed programming language will it be possible?
While doing some research, I came to know about the id() function. So, is there any way to leverage this to find out the answer to this problem?

Single, concise desription of Python's memory model/execution environment? [duplicate]

This question already has answers here:
Python Compilation/Interpretation Process
(2 answers)
Closed 6 years ago.
When I learned C/C++ I not only learned the syntax of the language and the semantics of the language constructs but also how the program was executed by the computer. I learned stuff like:
All local variables are declared in a stack frame, which is allocated
each time the function is called. These frames are laid out on the
call stack one right after the other, and they're 'unwound' when the
function returns, thus quickly and efficiently 'destroying' the local
variables for that function
This, in turn, helped me figure out why it's a bad idea to take the address of a local variable & return it to the calling function. In other words, understanding C/C++'s memory model and the environment in which the code executes helps develop a deeper understanding of how to write correct programs.
Another example: malloc/new allocate objects in the 'heap' (and not the stack), which both explains why they live beyond the end of the function that allocated them. Further, knowing that these functions/keywords return the memory address that the object is located at helped me to understand how things like linked lists work. (And how to figure out whether I need another * or -> or not.)
So now I'm learning Python and I'm looking for a concise, clear, yet thorough explanation of how Python programs manage their memory and execution environment.
Searching online hasn't been particularly fruitful - I might be using the wrong search terms, but there appears to be very little out there in general.
I've looked through https://docs.python.org/ and found it to be an excellent resource for syntax and semantics ("if you type X, then Y will happen"), but it doesn't really describe what the computer is doing 'under the hood'.
I've found several posts here on Stack Overflow ( such as this, this, and this ) but these all seem to focus on specific situations.
Does anyone know of a resource that actually explains what Python is doing under the hood'?
EDIT: I'm getting feedback from StackOverflow that this question may
be a duplicate. The other question asks 'how does a .PY file get
compiled (to bytecode) and then executed by the VM?' What I'm asking
here is 'is there a page that explains how Python lays variables,
objects, functions, etc out in memory AND explains how that's all used
to actually run Python programs' (Sub-question: Is this an appropriate
way to address the concern about a duplicate question? Would it be
better to put this in a comment?)
With python what you want to know about what's going on under the hood straight away to be able to work efficiently with the language is quite different from C/C++ since it's a quite different language environment. What you want to get you head around is not so much the nitty-gritty of what's going on memory, but Python's Data Model.

How to make a custom element im gst-python

i have quite a lot of experience with python and gst-python, but no experience with plain gstreamer.
does anyone know (well, someone on earth probably does but...) how to create a custom element? i got as far as
class MyElement(Element):
by intuition, but i have no idea what next...
simply what i was hoping for was a "replace this function with the thing you want to happen to every unit that this element is passed", but i am pretty certain that it will be FAR more complicated than that....
If you're creating a source element, you probably want to subclass gst.BaseSrc. Then, IIRC, the main thing you need to do is implement the do_create() virtual method. Don't forget to gobject.type_register() your class; you may also need to set the time format using set_format().
I second the recommendation to look at the Pitivi source code; it contains several GStreamer elements implemented in Python.

Is it OK if objects from different classes interact with each other?

I just started to use the object oriented programming in Python. I wander if it is OK if I create a method of a class which use objects from another class. In other words, when I call a method of the first class I give an object from the second class as one of the arguments. And then, the considered methods (of the first class) can manipulate by the object from the second class (to get its attributes or use its methods). Is it allowed in Python? Is it not considered as a bad programming style?
Is it OK if I instantiate objects from the second class withing a method of the first class. In other words, if I call a method from the first class it instantiate objects fro the second class.
Thank you in advance for any help.
If you're talking about passing an instance of one object to the method of a another one, then yes of course it's allowed! And it's considered fine practice.
If you want to know more about good object oriented coding, may I offer some suggested readings:
Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, John M. Vlissides
Known as the Gang Of Four book, this lays out a number of design patterns that seem to show up in object oriented code time and time again. This is a good place to go for ideas on how to handle certain problems in a good object oriented way.
Another good one:
Refactoring: Improving the Design of Existing Code by Martin Fowler, Kent Beck, John Brant , William Opdyke, Don Roberts
This is a great book for learning what NOT to do when writing object oriented code, and how to fix it to make it better when you do encounter it. It offers a list of code smells that suggest bad object oriented code and a reference section of refactorings that provide instructions on how to fix those smells and make them more object oriented.
What you're talking about is fine. In fact most data types (string, int, boolean, etc.) in Python are objects, so pretty much every method works in the way you described.
The answer is that it os MORE than OK, it's in fact the whole point.
What is not "OK" is when objects start fiddling with the internals of each other. You can prevent this from happening accidentally, by calling things that are meant to be internal with a leading underscore (or two, which makes it internal also for subclasses). This works as a little marker for other programmers that you aren't supposed to use it, and that it's not official API and can change.
I see no problem with that, it happens all the time. Do you have a specific problem you're trying to solve or just asking a general question without a context?
The Law of Demeter is general guidance on which methods and objects you can interact with in good faith.
It is guidance. You can make code that works that doesn't follow the LoD, but it is a good guide and helps you build "structure shy systems" -- something you will appreciate later when you try to make big changes.
http://en.wikipedia.org/wiki/Law_of_Demeter
I recommend reading up on good OO practices and principles when you're not coding. Maybe a few papers or a chapter of a book each evening or every other day. Try the SOLID principles. You can find a quick reference to them here:
http://agileinaflash.blogspot.com/2009/03/solid.html

Categories

Resources