如何修复维度错误?
How to fix DimensionError?
我有一个奇怪的错误仍然无法解决。
#include <iostream>
#include "fusion.h"
using namespace mosek::fusion;
using namespace monty;
int main(int argc, char ** argv)
{
int number_of_steps = 7;
int lambda1 = 1000;
int lambda2 = 1000;
int dim_space = 3;
auto A = new_array_ptr<double, 2>({ {-0.1504 , 0.5675 , -0.7252},
{-0.4518, 0.0456 , 0.0530},
{0.2635 , -0.1113 , -0.2546},
{-0.0495 , -0.2224 , 0.1030},
{ 0.1216, -0.2716 , 0.0503},
{ 0.2000 , -0.2788 , 0.1627},
{0.3534 , -0.2255 , 0.1733},
{ 0.2737 , -0.2614 , 0.1941},
{0.3001, 0.1014 , 0.3227},
{ 0.2473 , 0.0765 , 0.3377}});
auto b = new_array_ptr<double, 2>({{0.3597},
{0.8894},
{0.9238},
{0.9682},
{0.9534},
{0.9251},
{0.8912},
{0.9050},
{0.8919},
{0.9050}});
auto b_rep = new_array_ptr<double, 2>({{0.3597, 0.3597, 0.3597, 0.3597, 0.3597, 0.3597, 0.3597},
{0.8894, 0.8894, 0.8894, 0.8894, 0.8894, 0.8894, 0.8894},
{0.9238,0.9238, 0.9238,0.9238, 0.9238,0.9238, 0.9238},
{0.9682, 0.9682, 0.9682, 0.9682, 0.9682, 0.9682, 0.9682},
{0.9534, 0.9534, 0.9534, 0.9534, 0.9534, 0.9534, 0.9534},
{0.9251, 0.9251, 0.9251, 0.9251, 0.9251, 0.9251, 0.9251},
{0.8912, 0.8912, 0.8912, 0.8912, 0.8912, 0.8912, 0.8912},
{0.9050, 0.9050, 0.9050, 0.9050, 0.9050, 0.9050, 0.9050},
{0.8919, 0.8919, 0.8919, 0.8919, 0.8919, 0.8919, 0.8919},
{0.9050, 0.9050, 0.9050, 0.9050, 0.9050, 0.9050, 0.9050}});
Model::t M = new Model();
auto x = M->variable(new_array_ptr<int,1>({dim_space, number_of_steps}), Domain::unbounded()) ;
Matrix::t recipe = Matrix::dense(A);
// This way does not work,
// for (int i = 0; i < number_of_steps; i++)
// {
// auto x_i = Var::vstack(x->index(0,i), x->index(1, i), x->index(2, i));
// M->constraint(Expr::mul(recipe, x_i), Domain::lessThan(b));
// }
// This is working fine
M->constraint(Expr::mul(A, x), Domain::lessThan(b_rep));
}
在这里,当我使用 M->constraint(Expr::mul(A, x), Domain::lessThan(b_rep));
表达式时,它工作正常。但是同样的事情可以用下面的方法来做,但行不通,
for (int i = 0; i < number_of_steps; i++)
{
auto x_i = Var::vstack(x->index(0,i), x->index(1, i), x->index(2, i));
M->constraint(Expr::mul(recipe, x_i), Domain::lessThan(b));
}
我收到以下错误
terminate called after throwing an instance of 'mosek::fusion::DimensionError'
what(): Mismatching expression and domain
Aborted (core dumped)
b
的形状是(1,10)
,表达式的形状是(10)
。如果你这样做,它会起作用
auto b = new_array_ptr<double, 1>({0.3597,
0.8894,
0.9238,
0.9682,
0.9534,
0.9251,
0.8912,
0.9050,
0.8919,
0.9050});
或者您也可以使用与以前相同的 b
,但作为域使用
Domain::lessThan(b)->withShape(new_array_ptr<int,1>({10}))
然而,最推荐使用效果很好的矢量化变体。
我有一个奇怪的错误仍然无法解决。
#include <iostream>
#include "fusion.h"
using namespace mosek::fusion;
using namespace monty;
int main(int argc, char ** argv)
{
int number_of_steps = 7;
int lambda1 = 1000;
int lambda2 = 1000;
int dim_space = 3;
auto A = new_array_ptr<double, 2>({ {-0.1504 , 0.5675 , -0.7252},
{-0.4518, 0.0456 , 0.0530},
{0.2635 , -0.1113 , -0.2546},
{-0.0495 , -0.2224 , 0.1030},
{ 0.1216, -0.2716 , 0.0503},
{ 0.2000 , -0.2788 , 0.1627},
{0.3534 , -0.2255 , 0.1733},
{ 0.2737 , -0.2614 , 0.1941},
{0.3001, 0.1014 , 0.3227},
{ 0.2473 , 0.0765 , 0.3377}});
auto b = new_array_ptr<double, 2>({{0.3597},
{0.8894},
{0.9238},
{0.9682},
{0.9534},
{0.9251},
{0.8912},
{0.9050},
{0.8919},
{0.9050}});
auto b_rep = new_array_ptr<double, 2>({{0.3597, 0.3597, 0.3597, 0.3597, 0.3597, 0.3597, 0.3597},
{0.8894, 0.8894, 0.8894, 0.8894, 0.8894, 0.8894, 0.8894},
{0.9238,0.9238, 0.9238,0.9238, 0.9238,0.9238, 0.9238},
{0.9682, 0.9682, 0.9682, 0.9682, 0.9682, 0.9682, 0.9682},
{0.9534, 0.9534, 0.9534, 0.9534, 0.9534, 0.9534, 0.9534},
{0.9251, 0.9251, 0.9251, 0.9251, 0.9251, 0.9251, 0.9251},
{0.8912, 0.8912, 0.8912, 0.8912, 0.8912, 0.8912, 0.8912},
{0.9050, 0.9050, 0.9050, 0.9050, 0.9050, 0.9050, 0.9050},
{0.8919, 0.8919, 0.8919, 0.8919, 0.8919, 0.8919, 0.8919},
{0.9050, 0.9050, 0.9050, 0.9050, 0.9050, 0.9050, 0.9050}});
Model::t M = new Model();
auto x = M->variable(new_array_ptr<int,1>({dim_space, number_of_steps}), Domain::unbounded()) ;
Matrix::t recipe = Matrix::dense(A);
// This way does not work,
// for (int i = 0; i < number_of_steps; i++)
// {
// auto x_i = Var::vstack(x->index(0,i), x->index(1, i), x->index(2, i));
// M->constraint(Expr::mul(recipe, x_i), Domain::lessThan(b));
// }
// This is working fine
M->constraint(Expr::mul(A, x), Domain::lessThan(b_rep));
}
在这里,当我使用 M->constraint(Expr::mul(A, x), Domain::lessThan(b_rep));
表达式时,它工作正常。但是同样的事情可以用下面的方法来做,但行不通,
for (int i = 0; i < number_of_steps; i++)
{
auto x_i = Var::vstack(x->index(0,i), x->index(1, i), x->index(2, i));
M->constraint(Expr::mul(recipe, x_i), Domain::lessThan(b));
}
我收到以下错误
terminate called after throwing an instance of 'mosek::fusion::DimensionError'
what(): Mismatching expression and domain
Aborted (core dumped)
b
的形状是(1,10)
,表达式的形状是(10)
。如果你这样做,它会起作用
auto b = new_array_ptr<double, 1>({0.3597,
0.8894,
0.9238,
0.9682,
0.9534,
0.9251,
0.8912,
0.9050,
0.8919,
0.9050});
或者您也可以使用与以前相同的 b
,但作为域使用
Domain::lessThan(b)->withShape(new_array_ptr<int,1>({10}))
然而,最推荐使用效果很好的矢量化变体。