Squaring A Decision Variable in Mosel - linear-programming

in my problem, I want to minimize the sum of the square of a decision variable * LARGE_CONSTANT. The reason for square is to incentivize the solver to spread the decision variable around equally; if I have to use the panic variables, I want to use them equally across the set of locations.
Some code (admittedly not enough to reproduce), follows:
declarations
SITE: set of string
c_min_panic: array(SITE) of mpvar
c_max_panic: array(SITE) of mpvar
end-declarations
! Objectives.
Min_Panicking:= sum(s in SITE) ((c_min_panic(s) * 10000)^2)
Max_Panicking:= sum(s in SITE) ((c_max_panic(s) * 10000)^2)
However, this gives error: Mosel: E-101...Incompatible types for operator (mpvar' ^ integer' not defined).
Removing the ^ makes the problem work fine.
I am shocked why I can't square this? In fact, I see examples in the documentation that look like successful attempts to square the objective function. For example, Page 186 of the FICO Xpress User Guide seems to do it:
! Objective: minimise the total squared distance between all points
TotDist:= sum(i,j in RN | i<j) ((x(i)-x(j))^2+(y(i)-y(j))^2
What am I missing!! Tearing my hair out 😝 Thank you...

The code snippet does not show which solver module has been loaded: if it employs the Xpress Optimizer module, that is, starts with a line like
uses "mmxprs"
you also need to load the module 'mmnl' in order to extend the constraint handling capability of the Mosel language to include quadratic expressions, so
uses "mmxprs", "mmnl"
Another option would be to load the Xpress Nonlinear module:
uses "mmxnlp"

Related

Django - can I sotre and run code from database?

I build program, when in one part I have some ranking, and I would like to give users option to customize it.
In my code I have a function that gets objects and returnes them packed with points and position in ranking (for now it calculates the arithmetic mean of some object's values).
My question is is it possible to give e.g. admin chance to write this function via admin panel and use it, so if he would like to one day use harmonic mean he could without changing source code?
Yes, you could just store a string in the database and exec() it with suitable arguments...
However, you'll have to be careful – Python code can practically never be sandboxed perfectly. In the event that you accept any arbitrary Python code for this, and someone with nefarious intents gets to your admin panel to change the expression, they can do practically anything.
In other words, don't use raw Python for the code you store.

Why cosine_similarity of pretrained fasttex model is high between two sentents are not relative at all?

I am wondering to know why pre-trained 'fasttext model' with wiki(Korean) seems not to work well! :(
model = fasttext.load_model("./fasttext/wiki.ko.bin")
model.cosine_similarity("테스트 테스트 이건 테스트 문장", "지금 아무 관계 없는 글 정말로 정말로")
(in english)
model.cosine_similarity("test test this is test sentence", "now not all relative docs really really ")
0.99....??
Those sentence is not at all relative as meaning. Therefore I think that cosine-similarity must be lower. However It was 0.997383...
Is it impossive to compare lone sentents with fasttext?
So Is it only way to use doc2vec?
Which 'fasttext' code package are you using?
Are you sure its cosine_similarity() is designed to take such raw strings, and automatically tokenize/combine the words of each example to give sentence-level similarities? (Is that capability implied by its documentation or illustrative examples? Or perhaps does it expected pre-tokenized lists of words?)

Spree + Overwrite whole code base for modification and addition of new features

Just beginner for Spree Framework
I am trying to bring the whole code base to my local repository, so that I am make relevant changes as per my requirement like addition of different API calls to fill by Product tables.
Just like Devise, we can bring the code base to local code base for modification. In the same manner, I am willing to pull the code base and making changes as per requirement.
Please suggest something so that I can make changes as per my requirement.
I found Solidus as option to full fill my requirement.
Thanks
You can just override/add code as you want using class_eval. Here is example
#app/controllers/spree/admin/images_controller_decorator.rb
Spree::Admin::ImagesController.class_eval do
def index
#images = Spree::Image.all
end
end

How does one bypass SyntaxError when parsing code?

I am using openpyxl to read an excel file that will have changing values over time. The following function will take string inputs from the excel sheets to make frames for Tkinter.
def make_new_frame(strng, frame_location, frame_name, frame_list):
if not(frame_name in frame_list):
frame_list.append(frame_name)
exec("global %s" %(frame_name)) in globals()
exec("%s = Frame(%s)"%(frame_name, frame_location))
.... etc.
The code itself is quite long but I think this is enough of a snapshot to address my problem.
Now this results in the following error while parsing:
SyntaxError: function 'make_new_frame' uses import * and bare exec, which are illegal because it is a nested function
Everything in the code I included parsed and executed just fine several times, but after I added a few more lines in later versions in this function, it keeps spitting out the above error before executing the code. The error references the third line in the function, (which, I repeat, has been cleared in the past).
I added "in globals()" as recommended in another SO post, so that solution is not working.
There is a solution online here that uses setattr, which I have no idea how to use to create a widget without eventually using exec.
I would really appreciate if someone could tell me how to bypass the error while parsing or provide an alternative means for a dynamically changing set of frame names.
Quick Note:
I am aware that setting a variable as global in python is generally warned against, but I am quite certain that it will serve useful for my code
Edit 1: I have no idea why this was downvoted. If I have done something incorrectly, please let me know what it is so I can avoid doing so in the future.
I think this is an X/Y problem. You are asking for help with solution Y instead of asking for help on problem X.
If your goal is to create an unknown number of Frame objects based on external data, you can store references to the frame in a list or dictionary without having to resort to using exec and dynamically created variable names.
exec is a perfectly fine function, but is one of those things that you should never use until you fully understand why you should never use it.
Here's how to solve your actual problem without using exec:
frames = {}
def make_new_frame(strng, frame_location, frame_name, frames):
if not(frame_name in frames):
frames[frame_name] = Frame(frame_location)
return frames[frame_name]
With that, you now have a dictionary (frames) that includes a reference for every new frame by name. If you had a frame named "foo", for example, you could configure and pack it like this:
frames["foo"].configure(background="red", ...)
frames["foo"].pack(...)
If preserving the order of the frames is important you can use an OrderedDict.

General Advice to Creating a C++ Robot Template

I'm working on a project that has evolved so many times, I've really started thinking about writing a generic class that controls other nested generic classes.
The issue is, I have so many ways I could go about it that I've become completely overwhelmed. I've reviewed vectors and template class examples, but I truly have no idea how to begin.
Let me explain,
I need to be able to use this template to create an object that I control directly.
The main, top level object, is the robot I'm working on documented here (Probably not helpful to read, included as reference):
Github: MiddleMan5/Onyx-BeagleBone which pulls from:
Github: MiddleMan5/Project-Onyx-Quadruped
(New member 2-link restriction)
Which has the C++ Assembly.h file I'm working on making generic:
"Assembly File"
Now I'm sorry if my syntax is a little rusty, I'm very new to C++ in a general sense. I have great difficulty with pointers, object vectors, etc.
I'm really just looking to be guided in a general direction of study, and less a "Fix my code!" answer.
Bear with me as I may mar C++ terms:
I want to be able to create any type of robot configuration and manipulate EITHER it's individual assembly's End Effectors, or the entire chassis as a point particle physics object (if the robot is mobile).
This would allow me to use the same template to create a robot arm as well as my intended Quadruped. (Which is, in reality, a solid mobile mass that has a center and 4 "Arms"
Each initialized Robot must contain only one Body that in-itself contains at least one Base, one Joint, and one End Effector.
The creation of Top-Level object should fail if any of these things should fail to be included before Body.assemble creates the now-active robot. (Body.assemble creates Chassis Onyx)
I can now move the entire structure by saying something like Onyx.Leg(1).move(X,Y) or Onyx.rotate(X,45); //Move all legs to satisy final body rotation angle
Documented in my Brainstorming file here: Please at least skim.
This file contains diagrams and the "definitions" I'm using to prototype pseudo-code.
I only want one "Robot".h and "Robot".cpp to define actions, movements, sizes, shapes, etc. and one Main.cpp to provide entry into the code. (Maybe I'm asking a bit much) but logically to me, this seems do-able.
Includes:
Main.cpp
|_
| Onyx.h
|_Onyx.cpp //Body.move, body.rotate, body.raise
|_Assembly.h To create the body as a combination of assemblies
Onyx as a quadruped looks like this:
ONYX Complete Diagram Example:
EE EE
|| __ ||
| | | |
0__0+__+0__0
_||_
__0+_ _+ <- 0__
0 \/ 0 <-\
| | <- - "Leg" and Body are two attached assemblies inside one complete containing Robot chassis
|| || <-/
EE EE
Should be controlled like:
Onyx.EE1.moveTo(x,y);
or:
Onyx.moveTo(x,y);
Where I could further define gaits and parameters that would allow the robot ro take steps to move it's entire chassis to location (X, Y)
and has definitions for joints, links, and bases as such:
|Joint: Takes X and Y coordinates, rotates Link. ----0----
|__ End Effector: A joint combined with an attached Link. --------EE
|__ Base: A joint created that can be fixed statically through a link to another base joint
| |__ Can be attached actively to a link and a joint
| |__ Can be used as a static reference to an assembly (Such as a leg) and attached to a joint on a different plane
|
|__ Primary Joint: the joint(s) that attach directly to the Body's base(s)
|Link: A length of physical mass between a joint.
|__Segment: two or more attached in-line links (multiple links combined at different angles can specify independent shapes or masses).
Segments are useful for attaching spaced Bases. (Attach.Base(Segment1_2(End)); Segments can be attached either by their additive L1 or L2
I really am frustrated because the project has very quickly become way to complicated for me to even understand. This is why I was looking at creating a template. But it doesn't seem like accessing an object's object's method (Onyx.leg.move) is even possible in C++. Am I wrong?
If you actually read through to the end I want to give you a hand. Any advice you have on areas of study (Or advice to make my question clearer) would be GREATLY valued.
Thank You
Edit:
Okay, I went ahead and created a Class 'Assembly', which has methods to access a private subclass 'assemble' which attaches a "Leg with links n" to the assembly (god I need a different name) with subclass's Base, Link, and End_Effector. I guess I'm going to have to write individual methods and method handlers for each subclass of a subclass. This seems way to ugly and complicated with plenty of room to make errors.
The pseudo-flow as follows:
Assembly Onyx //Creates Robot: Onyx
Onyx.assemble(4, 3, 2); //Adds 4 legs each with 3 joints which includes 2 axis
for(i; 0 ---> 3)
Onyx.numleg_numL_length(i,1,114); //Defines L1 of Leg1 as 114mm
Onyx.numleg_numL_length(i,2,140); //Defines L2 of Leg1 as 114mm
and on and on and on and on.
Please tell me there's a simple trick that allows me to assign multiple variables to a named class instead of this infinite chain of methods functions and subclasses