ojalgo BUG: constraint breaking weights while using the ConvexSolver in ojAlgo - ojalgo
I wanted to calculate the efficient frontier with equality and inequality constraints for given covariance matrix and expected returns. While doing so, the solver calculates weights that unfortunately seem to break the constraints. The overall risks and returns seem to be fine tho.
Primitive64Store tmpCOV = Primitive64Store.FACTORY.rows(new double[][]
{{0.04161599999999999,0.029532687342839998,0.027204649602,0.0077310803303999994,-1.1671155792E-4,-5.6919104880000004E-5,0.011540367260999999,-0.0011583596544,0.0016547567508,0.011887951467599998,-0.0021016779250799997,0.01217717900784,0.0354566417292},
{0.029532687342839998,0.034969,0.02882914496033,0.00673688572095,-1.5753286164E-4,-7.799020878E-5,0.008590460804089999,-0.00120903466574,0.0014109339056999999,0.0104683749489,-0.00296095338671,0.0075381371219,0.03405788034132},
{0.027204649602,0.02882914496033,0.028561000000000003,0.0095923333779,-1.4991438384E-4,-8.476063180000002E-5,0.009686536403049999,-0.0011563569438200001,9.994519899E-4,0.0082659675852,-3.1685615143000007E-4,0.00860292214782,0.03129653885267},
{0.0077310803303999994,0.00673688572095,0.0095923333779,0.011024999999999998,-5.7071784E-6,-1.6705374000000003E-5,0.0062936927109,-2.7820294949999996E-4,8.405088299999999E-5,6.61814874E-4,0.0037795989147,0.0055210432431,0.00810029447955},
{-1.1671155792E-4,-1.5753286164E-4,-1.4991438384E-4,-5.7071784E-6,1.6E-5,5.40282216E-6,-3.1099949760000004E-5,1.671549776E-5,-3.051312E-7,-7.517952359999999E-5,3.06077038E-5,1.3556312E-7,-2.345551502E-4},
{-5.6919104880000004E-5,-7.799020878E-5,-8.476063180000002E-5,-1.6705374000000003E-5,5.40282216E-6,4.0E-6,-1.7530565920000003E-5,3.13729976E-6,-4.841874E-6,-4.2240274199999995E-5,5.2011296E-6,3.44228808E-6,-1.3104418078000002E-4},
{0.011540367260999999,0.008590460804089999,0.009686536403049999,0.0062936927109,-3.1099949760000004E-5,-1.7530565920000003E-5,0.011881,4.4589363133999997E-4,9.499062894E-4,0.0036563291469,0.00251147898767,0.004586606807859999,0.0108024647181},
{-0.0011583596544,-0.00120903466574,-0.0011563569438200001,-2.7820294949999996E-4,1.671549776E-5,3.13729976E-6,4.4589363133999997E-4,0.001444,6.672796998E-4,-9.37965438E-5,0.00112122563298,-0.0011028714312,-0.0013360677697000002},
{0.0016547567508,0.0014109339056999999,9.994519899E-4,8.405088299999999E-5,-3.051312E-7,-4.841874E-6,9.499062894E-4,6.672796998E-4,9.0E-4,0.0013451552549999999,2.136913209E-4,4.8565227479999997E-4,0.0022035050261999998},
{0.011887951467599998,0.0104683749489,0.0082659675852,6.61814874E-4,-7.517952359999999E-5,-4.2240274199999995E-5,0.0036563291469,-9.37965438E-5,0.0013451552549999999,0.0081,-0.0018377539344,0.0026756272619999997,0.0138893569515},
{-0.0021016779250799997,-0.00296095338671,-3.1685615143000007E-4,0.0037795989147,3.06077038E-5,5.2011296E-6,0.00251147898767,0.00112122563298,2.136913209E-4,-0.0018377539344,0.004489000000000001,1.718834348E-4,-0.0025610649546900003},
{0.01217717900784,0.0075381371219,0.00860292214782,0.0055210432431,1.3556312E-7,3.44228808E-6,0.004586606807859999,-0.0011028714312,4.8565227479999997E-4,0.0026756272619999997,1.718834348E-4,0.023716,0.010939810005740002},
{0.0354566417292,0.03405788034132,0.03129653885267,0.00810029447955,-2.345551502E-4,-1.3104418078000002E-4,0.0108024647181,-0.0013360677697000002,0.0022035050261999998,0.0138893569515,-0.0025610649546900003,0.010939810005740002,0.051529000000000005}});
Primitive64Store tmpR = Primitive64Store.FACTORY.rows(new double[][] {{0.067},{0.045},{0.049},{0.01},{0.02},{1.0E-4},{0.034},{0.001},{0.006},{0.027},{0.005},{0.02},{0.04}});
Primitive64Store tmpEqualityMatrix = Primitive64Store.FACTORY.rows(new double[][]{{1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0}});
Primitive64Store tmpEqualityVector = Primitive64Store.FACTORY.rows(new double[][] {{1}});
Primitive64Store tmpInequalityMatrix = Primitive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0,0.0,0.0,0.0,0.0,0.0,0.0},
{0.0,0.0,0.0,0.0,0.0,0.0,-1.0,0.0,0.0,0.0,0.0,0.0,0.0},
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.0,0.0,0.0,0.0,0.0,0.0},
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.0,0.0,0.0,0.0,0.0},
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.0,0.0,0.0,0.0},
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.0,0.0,0.0},
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.0,0.0},
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.0},
{1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0},
{-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0}});
Primitive64Store tmpInequalityVector = Primitive64Store.FACTORY.rows({{0.1},{0.2},{0.2},{0.1},{0.2},{0.1},{0.1},{1.0},{1.0},{1.0},{1.0},{0.1},{1.0},{0.0},{0.0},{0.0},{0.0},{0.0},{0.0},{0.0},{0.0},{0.0}{0.0}{0.0},{0.0},{0.0},{1.0},{0.0}});
//calculating the Portfolio with the maximum return, this always seems to work fine
ConvexSolver tmpSolverMinDiversification = new ConvexSolver.Builder(tmpCOV.multiply(0), tmpR)
.equalities(tmpEqualityMatrix, tmpEqualityVector).inequalities(tmpInequalityMatrix, tmpInequalityVector).build();
Optimisation.Result tmpResultMinDiversification = tmpSolverMinDiversification.solve();
//calculating the portfolio with minimal risk, this is where the error starts for me
ConvexSolver tmpSolverMaxDiversification = new ConvexSolver.Builder(tmpCOV, tmpR.multiply(0))
.equalities(tmpEqualityMatrix, tmpEqualityVector).inequalities(tmpInequalityMatrix, tmpInequalityVector).build();
Optimisation.Result tmpResultMaxDiversification = tmpSolverMaxDiversification.solve();
The Results im getting are as follows:
Weights of the portfolio with maximum return
OPTIMAL -0.0455 # { 0.1, 0.2, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.4999999999999999 }
Weights of the portfolio with minimal risk
OPTIMAL 1.9065360317211953E-4 # { -1.3215574127953212E-12, -3.0462153636587428E-12, 4.302357005267538E-12, 0.03119627351114846, 0.19999999989250858, 0.10000000045688691, -9.548402360338467E-13, 0.1370395162618518, 0.48451020053501254, 2.5326118583080594E-12, 0.03544670257704311, 0.011807307116290258, 6.086745301734428E-13 }
If i add an additional inequality (1 new row on both) constraint and run the calculation again
Primitive64Store tmpInequalityMatrix = Primitive64Store.FACTORY.rows(new double[][] {{1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
{0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
{0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
{0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
{0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
{0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
{0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0},
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0},
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0},
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0},
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0},
{0.0,0.0,0.0,0.0,0.0,0.0,0.0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1},{0.2},{0.2},{0.1},{0.2},{0.1},{0.1},{1.0},{1.0},{1.0},{1.0},{0.1},{1.0},{0.0},{0.0},{0.0},{0.0},{0.0},{0.0},{0.0},{0.0},{0.0}{0.0}{0.0},{0.0},{0.0},{1.0},{0.0},{ -0.037349326902676616 }});
ConvexSolver tmpSolverMaxDiversification = new ConvexSolver.Builder(tmpCOV, tmpR.multiply(0))
.equalities(tmpEqualityMatrix, tmpEqualityVector).inequalities(tmpInequalityMatrix, tmpInequalityVector).build();
Optimisation.Result tmpResultEfficientPortfolio = tmpSolverMaxDiversification.solve();
i get the following result
OPTIMAL 0.004591387674786172 # { 0.10000000000095524, 0.0, 0.20000000000667748, -3.148187023285784E-12, 0.19999999988537426, -2.6699549475823382E-11, 0.09999999999830238, 3.2126828658623563E-12, 7.645985136245771E-12, 0.5204239501042672, -0.12042395010749231, 1.4422465593413845E-12, -3.1890111868219504E-12 }
Is there a way to avoid those constraint breaking weights? Should i have used an ExpressionBasedModel instead?
If you set debug(ConvexSolver.class) on an instance of Optimisation.Options and pass that to the build method of your ConvexSolver.Builder then you may get some info regarding what goes wrong.
Optimisation.Options options = new Optimisation.Options();
options.debug(ConvexSolver.class);
convexSolverBuilder.build(options);
Related
Deformable torus not showing in pydrake port of C++ deformable torus example
I tried porting the C++ deformable torus example to python but the torus is not showing up in meshcat. There are no errors and I see the "ground" but not the torus: (I didn't include the parts related to the gripper, just trying to get the torus to show up for now) def run_demo(): builder = DiagramBuilder() plant_config = MultibodyPlantConfig() plant_config.time_step = time_step plant_config.discrete_contact_solver = "sap" plant, scene_graph = AddMultibodyPlant(plant_config, builder) rigid_proximity_props = ProximityProperties() surface_friction = CoulombFriction(1.15, 1.15) rigid_proximity_props.AddProperty("hydroelastic", "resolution_hint", 1.0) AddContactMaterial(friction = surface_friction, properties = rigid_proximity_props) ground = Box(4, 4, 4) X_WG = RigidTransform(RotationMatrix(), [0, 0, -2]) plant.RegisterCollisionGeometry(plant.world_body(), X_WG, ground, "ground_collision", rigid_proximity_props) plant.RegisterVisualGeometry(plant.world_body(), X_WG, ground, "ground_visual", [0.7, 0.5, 0.4, 0.8]) owned_deformable_model = DeformableModel(plant) deformable_config = DeformableBodyConfig() deformable_config.set_youngs_modulus(E) deformable_config.set_poissons_ratio(nu) deformable_config.set_mass_density(density) deformable_config.set_stiffness_damping_coefficient(beta) scale = 0.65; torus_mesh = Mesh("./torus.vtk", scale) kL = 0.09 * scale X_WB = RigidTransform(RotationMatrix(), [0, 0, kL/2]) torus_instance = GeometryInstance(X_WB, torus_mesh, "deformable_torus") deformable_proximity_props = ProximityProperties() AddContactMaterial(friction = surface_friction, properties = deformable_proximity_props) torus_instance.set_proximity_properties(deformable_proximity_props) unused_resolution_hint = 1.0 owned_deformable_model.RegisterDeformableBody( torus_instance, deformable_config, unused_resolution_hint) plant.AddPhysicalModel(owned_deformable_model) deformable_model = owned_deformable_model plant.Finalize(); builder.Connect( deformable_model.vertex_positions_port(), scene_graph.get_source_configuration_port(plant.get_source_id())); MeshcatVisualizer.AddToBuilder(builder, scene_graph, meshcat) diagram = builder.Build() diagram_context = diagram.CreateDefaultContext() simulator = Simulator(diagram) simulator.Initialize() simulator.set_target_realtime_rate(realtime_rate) meshcat.AddButton("Stop Simulation", "Escape") while meshcat.GetButtonClicks("Stop Simulation") < 1: simulator.AdvanceTo(simulator.get_context().get_time() + 2.0) meshcat.DeleteButton("Stop Simulation") meshcat.DeleteAddedControls() I'm thinking that the problem may be in the line: plant.AddPhysicalModel(owned_deformable_model) or somewhere earlier in the process of creating the deformable model, but I'm not sure... Full project here: DeepNote project Thanks so much for your help!
The deformable body should have been added to the simulation already. You can verify this by checking deformable_model.num_bodies(). This should be a visualization issue. As the README of the C++ example states, Meldis is required to visualize deformable bodies. You should be able to visualize after launching Meldis and switching the MeshcatVisualizer to DrakeVisualizer in your diagram.
no method matching logpdf when sampling from uniform distribution
I am trying to use reinforcement learning in julia to teach a car that is constantly being accelerated backwards (but with a positive initial velocity) to apply brakes so that it gets as close to a target distance as possible before moving backwards. To do this, I am making use of POMDPs.jl and crux.jl which has many solvers (I'm using DQN). I will list what I believe to be the relevant parts of the script first, and then more of it towards the end. To define the MDP, I set the initial position, velocity, and force from the brakes as a uniform distribution over some values. #with_kw struct SliderMDP <: MDP{Array{Float32}, Array{Float32}} x0 = Distributions.Uniform(0., 80.)# Distribution to sample initial position v0 = Distributions.Uniform(0., 25.) # Distribution to sample initial velocity d0 = Distributions.Uniform(0., 2.) # Distribution to sample brake force ... end My state holds the values of (position, velocity, brake force), and the initial state is given as: function POMDPs.initialstate(mdp::SliderMDP) ImplicitDistribution((rng) -> Float32.([rand(rng, mdp.x0), rand(rng, mdp.v0), rand(rng, mdp.d0)])) end Then, I set up my DQN solver using crux.jl and called a function to solve for the policy solver_dqn = DQN(π=Q_network(), S=s, N=30000) policy_dqn = solve(solver_dqn, mdp) calling solve() gives me the error MethodError: no method matching logpdf(::Distributions.Categorical{Float64, Vector{Float64}}, ::Nothing). I am quite sure that this comes from the initial state sampling, but I am not sure why or how to fix it. I have only been learning RL from various books and online lectures for a very short time, so any help regarding the error or my the model I set up (or anything else I'm oblivious to) would be appreciated. More comprehensive code: Packages: using POMDPs using POMDPModelTools using POMDPPolicies using POMDPSimulators using Parameters using Random using Crux using Flux using Distributions Rest of it: #with_kw struct SliderMDP <: MDP{Array{Float32}, Array{Float32}} x0 = Distributions.Uniform(0., 80.)# Distribution to sample initial position v0 = Distributions.Uniform(0., 25.) # Distribution to sample initial velocity d0 = Distributions.Uniform(0., 2.) # Distribution to sample brake force m::Float64 = 1. tension::Float64 = 3. dmax::Float64 = 2. target::Float64 = 80. dt::Float64 = .05 γ::Float32 = 1. actions::Vector{Float64} = [-.1, 0., .1] end function POMDPs.gen(env::SliderMDP, s, a, rng::AbstractRNG = Random.GLOBAL_RNG) x, ẋ, d = s if x >= env.target a = .1 end if d+a >= env.dmax || d+a <= 0 a = 0. end force = (d + env.tension) * -1 ẍ = force/env.m # Simulation x_ = x + env.dt * ẋ ẋ_ = ẋ + env.dt * ẍ d_ = d + a sp = vcat(x_, ẋ_, d_) reward = abs(env.target - x) * -1 return (sp=sp, r=reward) end function POMDPs.initialstate(mdp::SliderMDP) ImplicitDistribution((rng) -> Float32.([rand(rng, mdp.x0), rand(rng, mdp.v0), rand(rng, mdp.d0)])) end POMDPs.isterminal(mdp::SliderMDP, s) = s[2] <= 0 POMDPs.discount(mdp::SliderMDP) = mdp.γ mdp = SliderMDP(); s = state_space(mdp); # Using Crux.jl function Q_network() layer1 = Dense(3, 64, relu) layer2 = Dense(64, 64, relu) layer3 = Dense(64, length(3)) return DiscreteNetwork(Chain(layer1, layer2, layer3), [-.1, 0, .1]) end solver_dqn = DQN(π=Q_network(), S=s, N=30000) # Using Crux.jl policy_dqn = solve(solver_dqn, mdp) # Error comes here Stacktrace: policy_dqn MethodError: no method matching logpdf(::Distributions.Categorical{Float64, Vector{Float64}}, ::Nothing) Closest candidates are: logpdf(::Distributions.DiscreteNonParametric, !Matched::Real) at C:\Users\name\.julia\packages\Distributions\Xrm9e\src\univariate\discrete\discretenonparametric.jl:106 logpdf(::Distributions.UnivariateDistribution{S} where S<:Distributions.ValueSupport, !Matched::AbstractArray) at deprecated.jl:70 logpdf(!Matched::POMDPPolicies.PlaybackPolicy, ::Any) at C:\Users\name\.julia\packages\POMDPPolicies\wMOK3\src\playback.jl:34 ... logpdf(::Crux.ObjectCategorical, ::Float32)#utils.jl:16 logpdf(::Crux.DistributionPolicy, ::Vector{Float64}, ::Float32)#policies.jl:305 var"#exploration#133"(::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(Crux.exploration), ::Crux.DistributionPolicy, ::Vector{Float64})#policies.jl:302 exploration#policies.jl:297[inlined] action(::Crux.DistributionPolicy, ::Vector{Float64})#policies.jl:294 var"#exploration#136"(::Crux.DiscreteNetwork, ::Int64, ::typeof(Crux.exploration), ::Crux.MixedPolicy, ::Vector{Float64})#policies.jl:326 var"#step!#173"(::Bool, ::Int64, ::typeof(Crux.step!), ::Dict{Symbol, Array}, ::Int64, ::Crux.Sampler{Main.workspace#2.SliderMDP, Vector{Float32}, Crux.DiscreteNetwork, Crux.ContinuousSpace{Tuple{Int64}}, Crux.DiscreteSpace})#sampler.jl:55 var"#steps!#174"(::Int64, ::Bool, ::Int64, ::Bool, ::Bool, ::Bool, ::typeof(Crux.steps!), ::Crux.Sampler{Main.workspace#2.SliderMDP, Vector{Float32}, Crux.DiscreteNetwork, Crux.ContinuousSpace{Tuple{Int64}}, Crux.DiscreteSpace})#sampler.jl:108 var"#fillto!#177"(::Int64, ::Bool, ::typeof(Crux.fillto!), ::Crux.ExperienceBuffer{Array}, ::Crux.Sampler{Main.workspace#2.SliderMDP, Vector{Float32}, Crux.DiscreteNetwork, Crux.ContinuousSpace{Tuple{Int64}}, Crux.DiscreteSpace}, ::Int64)#sampler.jl:156 solve(::Crux.OffPolicySolver, ::Main.workspace#2.SliderMDP)#off_policy.jl:86 top-level scope#Local: 1[inlined]
Short answer: Change your output vector to Float32 i.e. Float32[-.1, 0, .1]. Long answer: Crux creates a Distribution over your network's output values, and at some point (policies.jl:298) samples a random value from it. It then converts this value to a Float32. Later (utils.jl:15) it does a findfirst to find the index of this value in the original output array (stored as objs within the distribution), but because the original array is still Float64, this fails and returns a nothing. Hence the error. I believe this (converting the sampled value but not the objs array and/or not using approximate equality check i.e. findfirst(isapprox(x), d.objs)) to be a bug in the package, and would encourage you to raise this as an issue on Github.
Dart - Double datatype addition results in long decimal values
In the following program I am adding the list of doubles. The output I am expecting is 57.7 but it results in 57.699999999999996 void main() { List<double> list= [1.0,1.0,1.0,1.0,0.8,52.9]; double total = 0.0; list.forEach((item) { total = total + item; }); print(total); } Is this a expected behaviour?
Yes, that is expected behavior - to get the desired result use - .toStringAsFixed(1) void main() { List<double> list = [1.0, 1.0, 1.0, 1.0, 0.8, 52.9]; double total = 0.0; list.forEach((item) { total = total + item; }); print(total.toStringAsFixed(1)); } output: 57.7
Using reduce will solve the problem var total = [1.1, 2.2, 3.3].reduce((a, b) => a + b); // 6.6
This is due to floating point math. This behavior is common in many languages. If you want to know more about this I recommend this site. Some languages like Java have classes made to work with this type of precise operation, however Dart does not have official support for this, luckily to get around this problem there is a package one. I'll show you what worked for me here. Using your code would look like this: First of all to solve the main problem we must use a package called decimal, add the following dependency in your pubspec.yaml and update the packages: name: ... dependencies: decimal: ^0.3.5 ... Then your file will look like this: import 'package:decimal/decimal.dart'; void main() { List<double> list = [1.0, 1.0, 1.0, 1.0, 0.8, 52.9]; double total = list.fold(0, (total, value) { var result = Decimal.parse(total.toString()) + Decimal.parse(value.toString()); return double.parse(result.toString()); }); print(total); // 57.7 } Obs: I used the google translator.
For double type addition with round off list.map<double>((m) => double.parse(m.totalTax.toDouble().toString())).reduce((a, b) => a + b).round()
Flash Actionscript 2.0 localToGlobal co-ordinate issues
I don't know if anyone here is old enough to remember ActionScript 2.0 but unfortunately I find myself stuck with it as the game I am writing a mod for (Skyrim) still uses AS2 for UI. I am using Greensock TweenLite/TimeLineLite for my animations/tweens, but I am having difficulty with what should be some simple co-ordinate calculations. I am trying to animate icon A from it's starting position, scaling it up and moving in x and y, and finishing in exactly the same position and size as icon B. Icon A and icon B are within different movieclips two levels up from a common parent clip, so I am trying to use localToGlobal to get stage x/y for each icon, and then moving in x/y by the differences between the two sets of stage co-ordinates. So for example: Icon A Global x = 95 Icon B Global x = 150 Difference = 55 Icon A Local x + 55 should put it in the same x position on stage as Icon B right? Well that's nowhere near the result I'm getting on screen, not even close. To help visualise better this is how the widget looks in game: Example screenshot And this is how it looks in CS6: Flash workspace So for example I am attempting to move/scale leftPreselectIcon (Dragonbone War Axe in the example screenshot) into exactly the position of leftIcon (Staff of Firebolts). Here are the relevant functions: public function prepareForPreselectAnimation(): Void { //This function checks if the preselect icons are to the left or right of their main icon and sets the animate out direction to the opposite side. It also stores current main icon x/y as the target values for the preselect icons to animate to. Finally it gets and stores all necessary current scale and alpha values to ensure everything returns to the exact state it was in prior to starting the animation var leftIconLTG:Object = {x:(leftIcon._height * 0.5), y:(leftIcon._width * 0.5)}; var leftPreselectIconLTG:Object = {x:(leftPreselectIcon._height * 0.5), y:(leftPreselectIcon._width * 0.5)}; var rightIconLTG:Object = {x:(rightIcon._height * 0.5), y:(rightIcon._width * 0.5)}; var rightPreselectIconLTG:Object = {x:(rightPreselectIcon._height * 0.5), y:(rightPreselectIcon._width * 0.5)}; var shoutIconLTG:Object = {x:(shoutIcon._height * 0.5), y:(shoutIcon._width * 0.5)}; var shoutPreselectIconLTG:Object = {x:(shoutPreselectIcon._height * 0.5), y:(shoutPreselectIcon._width * 0.5)}; leftIcon.localToGlobal(leftIconLTG); leftPreselectIcon.localToGlobal(leftPreselectIconLTG); rightIcon.localToGlobal(rightIconLTG); rightPreselectIcon.localToGlobal(rightPreselectIconLTG); shoutIcon.localToGlobal(shoutIconLTG); shoutPreselectIcon.localToGlobal(shoutPreselectIconLTG); skyui.util.Debug.log("leftIconLTG - x: " + leftIconLTG.x + ", y: " + leftIconLTG.y) skyui.util.Debug.log("leftPreselectIconLTG - x: " + leftPreselectIconLTG.x + ", y: " + leftPreselectIconLTG.y) if (leftIconLTG.x > leftPreselectIconLTG.x){ leftTargetX = (leftIcon_mc._width) //If preselect icon is to the left of the main widget animate main widget out to right //leftPTargetX = (leftIconLTG.x - leftPreselectIconLTG.x) + (leftIcon._width / 2) } else { leftTargetX = -(leftIcon_mc._width) //If preselect icon is to the right of the main widget animate main widget out to left //leftPTargetX = (leftIconLTG.x - leftPreselectIconLTG.x) - (leftIcon._width) } leftPTargetX = (leftIconLTG.x - leftPreselectIconLTG.x) skyui.util.Debug.log("leftTargetX: " + leftTargetX + ", leftPTargetX: " + leftPTargetX) if (rightIconLTG.x > rightPreselectIconLTG.x){ rightTargetX = (rightIcon_mc._width) //rightPTargetX = (rightIconLTG.x - rightPreselectIconLTG.x) + (rightIcon._width / 2) } else { rightTargetX = -(rightIcon_mc._width) //rightPTargetX = (rightIconLTG.x - rightPreselectIconLTG.x) - (rightIcon._width) } rightPTargetX = (rightIconLTG.x - rightPreselectIconLTG.x) if (shoutIconLTG.x > shoutPreselectIconLTG.x){ shoutTargetX = (shoutIcon_mc._width) //shoutPTargetX = (shoutIconLTG.x - shoutPreselectIconLTG.x) + (shoutIcon._width / 2) } else { shoutTargetX = -(shoutIcon_mc._width) //shoutPTargetX = (shoutIconLTG.x - shoutPreselectIconLTG.x) - (shoutIcon._width) } shoutPTargetX = (shoutIconLTG.x - shoutPreselectIconLTG.x) leftPTargetY = leftIconLTG.y - leftPreselectIconLTG.y; skyui.util.Debug.log("leftPTargetY: " + leftPTargetY) rightPTargetY = rightIconLTG.y - rightPreselectIconLTG.y; shoutPTargetY = shoutIconLTG.y - shoutPreselectIconLTG.y; //Store current alpha and scale values ready to reapply leftIconAlpha = leftIcon_mc._alpha; leftTargetScale = ((leftIcon_mc._xscale / leftPreselectIcon_mc._xscale) * 100); //leftPTargetX = ((leftIconLTG.x - leftPreselectIconLTG.x) * (leftIcon_mc._xscale / leftPreselectIcon_mc._xscale)); leftPIconAlpha = leftPreselectIcon_mc._alpha; leftPIconScale = leftPreselectIcon._xscale; rightIconAlpha = rightIcon_mc._alpha; rightTargetScale = ((rightIcon_mc._xscale / rightPreselectIcon_mc._xscale) * 100); rightPIconAlpha = rightPreselectIcon_mc._alpha; rightPIconScale = rightPreselectIcon._xscale; shoutIconAlpha = shoutIcon_mc._alpha; shoutTargetScale = ((shoutIcon_mc._xscale / shoutPreselectIcon_mc._xscale) * 100); shoutPIconAlpha = shoutPreselectIcon_mc._alpha; shoutPIconScale = shoutPreselectIcon._xscale; leftNameAlpha = leftName_mc._alpha; leftPNameAlpha = leftPreselectName_mc._alpha; rightNameAlpha = rightName_mc._alpha; rightPNameAlpha = rightPreselectName_mc._alpha; shoutNameAlpha = shoutName_mc._alpha; shoutPNameAlpha = shoutPreselectName_mc._alpha; skse.SendModEvent("iEquip_ReadyForPreselectAnimation", null); } public function equipPreselectedItem(iSlot: Number, currIcon: String, newIcon: String, newName: String, currPIcon: String, newPIcon: String, newPName: String): Void { var iconClip: MovieClip; var iconClip_mc: MovieClip; var currIcon: String; var pIconClip: MovieClip; var pIconClip_mc: MovieClip; var currPIcon: String; var itemName_mc: MovieClip; var preselectName_mc: MovieClip; var targetX: Number; var pTargetX: Number; var pTargetY: Number; var pIconAlpha: Number; var pIconScale: Number; var pIconTargetScale: Number var iconAlpha: Number; var itemNameAlpha: Number; var preselectNameAlpha: Number; switch(iSlot) { case 0: iconClip = leftIcon; iconClip_mc = leftIcon_mc; pIconClip = leftPreselectIcon; pIconClip_mc = leftPreselectIcon_mc; itemName_mc = leftName_mc; preselectName_mc = leftPreselectName_mc; targetX = leftTargetX; pTargetX = leftPTargetX; pTargetY = leftPTargetY; pIconAlpha = leftPIconAlpha; pIconScale = leftPIconScale; iconAlpha = leftIconAlpha; pIconTargetScale = leftTargetScale; itemNameAlpha = leftNameAlpha; preselectNameAlpha = leftPNameAlpha; break case 1: iconClip = rightIcon; iconClip_mc = rightIcon_mc; pIconClip = rightPreselectIcon; pIconClip_mc = rightPreselectIcon_mc; itemName_mc = rightName_mc; preselectName_mc = rightPreselectName_mc; targetX = rightTargetX; pTargetX = rightPTargetX; pTargetY = rightPTargetY; pIconAlpha = rightPIconAlpha; pIconScale = rightPIconScale; iconAlpha = rightIconAlpha; pIconTargetScale = rightTargetScale; itemNameAlpha = rightNameAlpha; preselectNameAlpha = rightPNameAlpha; break case 2: iconClip = shoutIcon; iconClip_mc = shoutIcon_mc; pIconClip = shoutPreselectIcon; pIconClip_mc = shoutPreselectIcon_mc; itemName_mc = shoutName_mc; preselectName_mc = shoutPreselectName_mc; targetX = shoutTargetX; pTargetX = shoutPTargetX; pTargetY = shoutPTargetY; pIconAlpha = shoutPIconAlpha; pIconScale = shoutPIconScale; iconAlpha = shoutIconAlpha; pIconTargetScale = shoutTargetScale; itemNameAlpha = shoutNameAlpha; preselectNameAlpha = shoutPNameAlpha; break } var tempIcon: MovieClip = iconClip.duplicateMovieClip("tempIcon", this.getNextHighestDepth()); tempIcon.gotoAndStop(currIcon); iconClip._alpha = 0; iconClip.gotoAndStop(newIcon); var tempPIcon: MovieClip = pIconClip.duplicateMovieClip("tempPIcon", this.getNextHighestDepth()); //var iconLTG: Object = {x:(iconClip._width * 0.5), y:(iconClip._height * 0.5)} //iconClip.localToGlobal(iconLTG); //var tempPIconLTG: Object = {x:(tempPIcon._width * 0.5), y:(tempPIcon._height * 0.5)} //tempPIcon.localToGlobal(tempPIconLTG); //pTargetX = ((iconLTG.x - tempPIconLTG.x) * (iconClip_mc._xscale / pIconClip_mc._xscale)); //skyui.util.Debug.log("iconLTG.x: " + iconLTG.x + ", iconLTG.y: " + iconLTG.y + "tempPIconLTG.x: " + tempPIconLTG.x + ", tempPIconLTG.y: " + tempPIconLTG.y + ", pTargetX: " + pTargetX) tempPIcon._xscale = pIconClip_mc._xscale; tempPIcon._yscale = pIconClip_mc._yscale; tempPIcon.gotoAndStop(currPIcon); pIconClip._alpha = 0; pIconClip._xscale = 25; pIconClip._yscale = 25; pIconClip.gotoAndStop(newPIcon); var tl = new TimelineLite({paused:true, autoRemoveChildren:true, onComplete:equipPreselectedItemComplete}); tl.to(itemName_mc, 0.3, {_alpha:0, ease:Quad.easeOut}, 0) .to(preselectName_mc, 0.3, {_alpha:0, ease:Quad.easeOut}, 0) .call(updateNamesForEquipPreselect, [iSlot, newName, newPName, this]) .to(tempIcon, 0.6, {_x:targetX, _y:((tempIcon._height) / 2), _rotation:"+=90", _alpha:0, _xscale:25, _yscale:25, ease:Quad.easeOut}, 0); .to(tempPIcon, 0.6, {_x:pTargetX, _y:pTargetY, _alpha:iconAlpha, _xscale:pIconTargetScale, _yscale:pIconTargetScale, ease:Quad.easeOut}, 0) .to(iconClip, 0, {_alpha:iconAlpha, ease:Linear.easeNone}) .to(tempPIcon, 0, {_alpha:0, ease:Linear.easeNone}) .to(pIconClip, 0.4, {_alpha:pIconAlpha, _xscale:pIconScale, _yscale:pIconScale, ease:Elastic.easeOut}, 0.5) .to(itemName_mc, 0.3, {_alpha:itemNameAlpha, ease:Quad.easeOut}, 0.6) .to(preselectName_mc, 0.3, {_alpha:preselectNameAlpha, ease:Quad.easeOut}, 0.6) tl.play(); } I have also uploaded it to Pastebin HERE The straightforward Icon A X value + Global X value difference which should work results in this: Screenshot 2 And the issue is worse the greater the distance between the two icons is to start with. If I edit the starting position of Icon A to be much further away this is the result: Screenshot 3 From those two examples the logged values and calculations are: [20:12:47.970] leftIconLTG - x: 149.49916992187, y: 581.28598632812 [20:12:47.970] leftPreselectIconLTG - x: 94.587438964844, y: 577.68461914063 [20:12:47.970] leftTargetX: 39.15, leftPTargetX: 54.911730957031 [20:12:47.970] leftPTargetY: 3.6013671874999 [20:13:18.510] leftIconLTG - x: 149.49916992187, y: 581.28598632812 [20:13:18.510] leftPreselectIconLTG - x: 594.56030273438, y: 577.68461914063 [20:13:18.510] leftTargetX: -58.55, leftPTargetX: -445.0611328125 [20:13:18.510] leftPTargetY: 3.6013671874999 So as you can see the target leftIcon global (LTG) x is 149.5, and in the first screenshot the leftPreselectIcon starts at a global x of 94.6. leftPTargetX contains the difference calculated correctly at 54.9. So moving leftPreselectIcon from it's local starting x of 0 to 54.9 should in theory move it into the correct position. So what am I doing wrong here? As you can see from the commented out code I have made several other attempts including adding half/full icon widths to the calculation, and even testing whether the difference in scale has anything to do with it by multiplying the global x difference value by the scale factor going from icon A scale to icon B scale using the on screen heights for the calculation. But none of them get me close. Solved The solution was to create 'store' objects for each set of icons, and then call localToGlobal on the target icon, then globalToLocal on the icon to be moved, both calls on the same object: var leftPIconTarget:Object = {x:0, y:0}; leftIcon.localToGlobal(leftPIconTarget); leftPreselectIcon.globalToLocal(leftPIconTarget); leftPIconTarget now contains the target x and y co-ordinates for the tween on leftPreselectIcon, and continues to work no matter where on the screen I move the starting positions to. The only wierdness is that I have to make the right x negative or it goes in the wrong direction, which makes no sense at all, because if I move the left icon way over to the right so it has to animate right to left I don't have to make it negative. Go figure! Anyway, it works, and that's all I'm bothered about!
PositionConstraint goal for robot arm: Unable to construct goal representation
I have a setting of ROS indigo, Gazebo under Ubuntu 14.04. Under ROS, moveit node is running. A robot arm IRB120 is simulated and standing in Gazebo. I have a node that uses moveit (move_group node) to plan a path (trajectory) for for the destination that Bob wants. The planned trajectory will be sent to Gazebo to be shown later. There is two approaches that Bob can use to describe the destination: Angles of each joints of the arm: using an array of six numbers (for six joints of the arm), the form of each joint and shin is defined. This approach works fine. It uses the JointConstraint class: double goal_poses [] = {0.52, 0.50, 0.73, -0.02, 0.31, 6.83}; for(int i = 0 ; i < 6; i++) // iterate over joints of the arm. { moveit_msgs::JointConstraint jc; jc.weight = 1.0; jc.tolerance_above = 0.0001; jc.tolerance_below = 0.0001; jc.position = goal_poses[i]; jc.joint_name = names[i]; goal_constraint.joint_constraints.push_back(jc); } Define the location and direction of the end effector only. I can not use this approach. I have used the PositionConstraint class. Problem in short: I can describe a destination using JointConstraint class, But I don't know how to describe it in PositionConstraint class. How to describe a goal, by just pointing out where the end effector should be? How i describe the goal in PositionConstraint format: (I point out where the end effector should be and what it's orientation should be.) moveit_msgs::PositionConstraint pc; pc.weight = 1.0; geometry_msgs::Pose p; p.position.x = 0.3; // not sure if feasible position p.position.y = 0.3; // not sure if feasible position p.position.z = 0.3; // not sure if feasible position pc.link_name="tool0"; p.orientation.x = 0; p.orientation.y = 0; p.orientation.z = 0; p.orientation.w = 1; pc.constraint_region.mesh_poses.push_back(p); goal_constraint.position_constraints.push_back(pc); But When the request is sent, server responds with: [ERROR] [1527689581.951677797, 295.242000000]: Unable to construct goal representation Note: In both cases, I add the goal_constraint to the trajectory_request: trajectory_request.goal.request.goal_constraints.push_back(goal_constraint); // add other details to trajectory_request here... trajectory_request is to be sent to the move_group. (by publishing the trajectory_request on the /move_group/goal topic)
A slightly different solution solved the problem of describing goal with end-effector orientation and location: Instead of publishing the goal on a topic for another node to parse and read, we can use the moveit library function computeCartesianPath. (In this example the code to publish the trajectory is commented and partially missing) void planTo(std::vector<double> coordinate, std::vector<double> orientation){ geometry_msgs::Pose p; p.orientation.w = 1.0; p.position.x = coordinate[0]; p.position.y = coordinate[1]; p.position.z = coordinate[2]; tf::Quaternion q = tf::createQuaternionFromRPY( orientation[0],orientation[1],orientation[2]); p.orientation.x = q.getX(); p.orientation.y = q.getY(); p.orientation.z = q.getZ(); p.orientation.w = q.getW(); std::vector<geometry_msgs::Pose> goals; goals.push_back(p); moveit::planning_interface::MoveGroup mg("manipulator"); mg.setStartStateToCurrentState(); // load the path in the `trajectory` variable: moveit_msgs::RobotTrajectory trajectory; mg.computeCartesianPath(goals, 0.01, 0.0, trajectory); // publish to gazebo: // trajectory.joint_trajectory.header.stamp = ros::Time::now(); // publisher.publish(trajectory.joint_trajectory); } I solved this a few months ago and unfortunately i do not remember the exact source/tutorial.