Skip to content

Apple Silicon Support #292

@Kiguli

Description

@Kiguli

I've been playing around with NNV on my Macbook Air (Apple M2, 8GB, MacOS Sonoma v14.5, MATLAB R2024B), and notice that there are issues for NNV functionality that relies on the MPT toolbox.

Specifically, cddmex is not supported for the Apple Silicon hardware, which has knock-on effects for visualization as most visualizations try to call toPolyhedron which requires MPT which itself needs the fourier function which eventually needs cddmex.

I've provided a simple example below with the output demonstrating an issue with visualization of the set.

%% Exercise 1: Create and Visualize a Triangle Star Set
% Create a star set representing a triangle with vertices at (0,0), (2,0), (1,2).
%
% Approach:
% A triangle is a convex polytope that can be represented as a star set.
% We need to find a center point and generators that span the triangle,
% along with constraints that limit the predicate variables to form the
% triangular shape.
%
% Method: Use the vertex representation to construct the star.
% For a triangle with vertices v1, v2, v3, we can express any point as:
%   x = α1*v1 + α2*v2 + α3*v3  where α1 + α2 + α3 = 1, αi >= 0
%
% This is equivalent to:
%   x = v1 + α2*(v2-v1) + α3*(v3-v1)  where α2,α3 >= 0, α2+α3 <= 1

%% Define the triangle vertices
v1 = [0; 0];  % First vertex
v2 = [2; 0];  % Second vertex
v3 = [1; 2];  % Third vertex

%% Construct the Star Set
% Center: We use v1 as the anchor point (could also use centroid)
% Generators: (v2 - v1) and (v3 - v1)
%
% Any point in the triangle can be written as:
%   x = v1 + α1*(v2-v1) + α2*(v3-v1)
% where: α1 >= 0, α2 >= 0, α1 + α2 <= 1

center = v1;
gen1 = v2 - v1;  % [2; 0]
gen2 = v3 - v1;  % [1; 2]

% Basis matrix V = [center | gen1 | gen2]
V = [center, gen1, gen2];

% Constraints on predicate variables α = [α1; α2]:
% 1. α1 >= 0  =>  -α1 <= 0
% 2. α2 >= 0  =>  -α2 <= 0
% 3. α1 + α2 <= 1
C = [-1  0;    % -α1 <= 0
      0 -1;    % -α2 <= 0
      1  1];   % α1 + α2 <= 1
d = [0; 0; 1];

% Create the Star set
S_triangle = Star(V, C, d);

%% Verify the Star Set
fprintf('=== Exercise 1: Triangle Star Set ===\n\n');
fprintf('Triangle vertices:\n');
fprintf('  v1 = (%.1f, %.1f)\n', v1(1), v1(2));
fprintf('  v2 = (%.1f, %.1f)\n', v2(1), v2(2));
fprintf('  v3 = (%.1f, %.1f)\n', v3(1), v3(2));
fprintf('\n');

fprintf('Star set properties:\n');
fprintf('  Dimension: %d\n', S_triangle.dim);
fprintf('  Number of predicate variables: %d\n', S_triangle.nVar);
fprintf('  Is empty: %d\n', S_triangle.isEmptySet());
fprintf('\n');

% Verify bounds match the triangle's bounding box
[lb_x, ub_x] = S_triangle.getRange(1);
[lb_y, ub_y] = S_triangle.getRange(2);
fprintf('Bounding box:\n');
fprintf('  x ∈ [%.2f, %.2f] (expected: [0, 2])\n', lb_x, ub_x);
fprintf('  y ∈ [%.2f, %.2f] (expected: [0, 2])\n', lb_y, ub_y);
fprintf('\n');

%% Verify vertices are contained in the star
% Sample points at the vertices by setting predicate values
% α = [0; 0] -> v1, α = [1; 0] -> v2, α = [0; 1] -> v3
fprintf('Vertex verification (x = center + V_generators * α):\n');
fprintf('  α = [0; 0]: (%.1f, %.1f) = v1 ✓\n', center(1), center(2));
p2 = center + gen1 * 1 + gen2 * 0;
fprintf('  α = [1; 0]: (%.1f, %.1f) = v2 ✓\n', p2(1), p2(2));
p3 = center + gen1 * 0 + gen2 * 1;
fprintf('  α = [0; 1]: (%.1f, %.1f) = v3 ✓\n', p3(1), p3(2));
fprintf('\n');

%% Visualization
% Note: Star.plot() requires cddmex which may not be available on Apple Silicon.
% If plotting fails, we provide an alternative using the zonotope approximation.

fprintf('Attempting visualization...\n');
try
    figure('Name', 'Exercise 1: Triangle Star Set');
    Star.plot(S_triangle, 'b');
    xlabel('x_1');
    ylabel('x_2');
    title('Triangle Star Set: vertices (0,0), (2,0), (1,2)');
    grid on;
    axis equal;

    % Add vertex markers
    hold on;
    plot([v1(1), v2(1), v3(1), v1(1)], [v1(2), v2(2), v3(2), v1(2)], 'ro-', 'MarkerSize', 10, 'LineWidth', 2);
    legend('Star Set', 'Vertices');
    hold off;

    fprintf('Visualization successful!\n');
catch ME %% no error caught here!
    fprintf('Star.plot() failed: %s\n', ME.message);
    fprintf('\nAlternative: Plotting vertices manually...\n');

    figure('Name', 'Exercise 1: Triangle (Manual Plot)');
    fill([v1(1), v2(1), v3(1)], [v1(2), v2(2), v3(2)], 'b', 'FaceAlpha', 0.3);
    hold on;
    plot([v1(1), v2(1), v3(1), v1(1)], [v1(2), v2(2), v3(2), v1(2)], 'b-', 'LineWidth', 2);
    plot([v1(1), v2(1), v3(1)], [v1(2), v2(2), v3(2)], 'ro', 'MarkerSize', 10, 'MarkerFaceColor', 'r');
    xlabel('x_1');
    ylabel('x_2');
    title('Triangle: vertices (0,0), (2,0), (1,2)');
    grid on;
    axis equal;
    legend('Triangle Region', 'Edges', 'Vertices');
    hold off;
end

fprintf('\n=== Exercise 1 Complete ===\n');
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions